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)
| 47 | // ServiceConfig. It returns a type that should be passed to |
| 48 | // UpdateClientConnState in the BalancerConfig field. |
| 49 | func 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 | } |