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

Function ParseConfig

internal/balancer/gracefulswitch/config.go:49–84  ·  view source on GitHub ↗

ParseConfig parses a child config list and returns a LB config for the gracefulswitch Balancer. cfg is expected to be a json.RawMessage containing a JSON array of LB policy names + configs as the format of the "loadBalancingConfig" field in ServiceConfig. It returns a type that should be passed to

(cfg json.RawMessage)

Source from the content-addressed store, hash-verified

47// ServiceConfig. It returns a type that should be passed to
48// UpdateClientConnState in the BalancerConfig field.
49func ParseConfig(cfg json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
50 var lbCfg []map[string]json.RawMessage
51 if err := json.Unmarshal(cfg, &lbCfg); err != nil {
52 return nil, err
53 }
54 for i, e := range lbCfg {
55 if len(e) != 1 {
56 return nil, fmt.Errorf("expected a JSON struct with one entry; received entry %v at index %d", e, i)
57 }
58
59 var name string
60 var jsonCfg json.RawMessage
61 for name, jsonCfg = range e {
62 }
63
64 builder := balancer.Get(name)
65 if builder == nil {
66 // Skip unregistered balancer names.
67 continue
68 }
69
70 parser, ok := builder.(balancer.ConfigParser)
71 if !ok {
72 // This is a valid child with no config.
73 return &lbConfig{childBuilder: builder}, nil
74 }
75
76 cfg, err := parser.ParseConfig(jsonCfg)
77 if err != nil {
78 return nil, fmt.Errorf("error parsing config for policy %q: %v", name, err)
79 }
80 return &lbConfig{childBuilder: builder, childConfig: cfg}, nil
81 }
82
83 return nil, fmt.Errorf("no supported policies found in config: %v", string(cfg))
84}

Callers 5

parseServiceConfigFunction · 0.92
lbConfigForFunction · 0.92
UpdateClientConnStateMethod · 0.92
ParseConfigFunction · 0.92
UpdateClientConnStateMethod · 0.92

Calls 4

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

Tested by 1

lbConfigForFunction · 0.74