MCPcopy
hub / github.com/grpc/grpc-go / parseChildPolicyConfigs

Function parseChildPolicyConfigs

balancer/rls/config.go:284–323  ·  view source on GitHub ↗

parseChildPolicyConfigs iterates through the list of child policies and picks the first registered policy and validates its config.

(childPolicies []map[string]json.RawMessage, targetFieldName string)

Source from the content-addressed store, hash-verified

282// parseChildPolicyConfigs iterates through the list of child policies and picks
283// the first registered policy and validates its config.
284func parseChildPolicyConfigs(childPolicies []map[string]json.RawMessage, targetFieldName string) (string, map[string]json.RawMessage, error) {
285 for i, config := range childPolicies {
286 if len(config) != 1 {
287 return "", nil, fmt.Errorf("rls: invalid childPolicy: entry %v does not contain exactly 1 policy/config pair: %q", i, config)
288 }
289
290 var name string
291 var rawCfg json.RawMessage
292 for name, rawCfg = range config {
293 }
294 builder := balancer.Get(name)
295 if builder == nil {
296 continue
297 }
298 parser, ok := builder.(balancer.ConfigParser)
299 if !ok {
300 return "", nil, fmt.Errorf("rls: childPolicy %q with config %q does not support config parsing", name, string(rawCfg))
301 }
302
303 // To validate child policy configs we do the following:
304 // - unmarshal the raw JSON bytes of the child policy config into a map
305 // - add an entry with key set to `target_field_name` and a dummy value
306 // - marshal the map back to JSON and parse the config using the parser
307 // retrieved previously
308 var childConfig map[string]json.RawMessage
309 if err := json.Unmarshal(rawCfg, &childConfig); err != nil {
310 return "", nil, fmt.Errorf("rls: json unmarshal failed for child policy config %q: %v", string(rawCfg), err)
311 }
312 childConfig[targetFieldName], _ = json.Marshal(dummyChildPolicyTarget)
313 jsonCfg, err := json.Marshal(childConfig)
314 if err != nil {
315 return "", nil, fmt.Errorf("rls: json marshal failed for child policy config {%+v}: %v", childConfig, err)
316 }
317 if _, err := parser.ParseConfig(jsonCfg); err != nil {
318 return "", nil, fmt.Errorf("rls: childPolicy config validation failed: %v", err)
319 }
320 return name, childConfig, nil
321 }
322 return "", nil, fmt.Errorf("rls: invalid childPolicy config: no supported policies found in %+v", childPolicies)
323}
324
325func convertDuration(d *durationpb.Duration) (time.Duration, error) {
326 if d == nil {

Callers 1

ParseConfigMethod · 0.85

Calls 5

GetFunction · 0.92
ErrorfMethod · 0.65
UnmarshalMethod · 0.65
MarshalMethod · 0.65
ParseConfigMethod · 0.65

Tested by

no test coverage detected