//LoadReader uses l's settings and a encoding/json.Decoder to parse Values from in. //If the call to encoding/json.Decoder.Decode() returns an error, then that error //is returned with nil *Values. //in must represent a JSON encoded object. Any other type will error. // //Notice that LoadReader is a config.ReaderFuncLoader and it is used in this manner //in the examples. func (l *Loader) LoadReader(in io.Reader) (*config.Values, error) { object, err := parseJson(in) if err != nil { return nil, err } values := config.NewValues() l.loadMapIntoValues(config.Key(nil), values, object) return values, nil }
func TestLoader_Load(t *testing.T) { l := New("") l.FlagSet.String("a", "", "") l.Args = []string{"-a", "value"} want := config.NewValues() want.Put(config.NewKey("a"), "value") testLoadWithWantedValues(t, l, want) }
func TestLoader_LoadString_discardNull(t *testing.T) { in := `{ "a": null }` l := &Loader{ DiscardNull: true, } want := config.NewValues() testLoadStringWithWantedValues(t, l, in, want) }
func (p *prefixParserLoader) Load() (*config.Values, error) { values := config.NewValues() environment := os.Environ() for _, envVar := range environment { key, value := p.loadPossibleEnvironmentVariable(envVar) if !key.IsEmpty() { values.Put(key, value) } } return values, nil }
func TestLoader_LoadString_numberAsString(t *testing.T) { in := `{ "a": 12 }` l := &Loader{ NumberAsString: true, } want := config.NewValues() want.Put(config.NewKey("a"), "12") testLoadStringWithWantedValues(t, l, in, want) }
func TestLoader_LoadString_keySuffix(t *testing.T) { in := `{ "a": { "b": "b" }, "c": { "d": "d" } }` l := &Loader{ KeySuffix: config.NewKey("d"), } want := config.NewValues() want.Put(config.NewKey("c", "d"), "d") testLoadStringWithWantedValues(t, l, in, want) }
//Load is the config.Loader required method. //It calls l.FlagSet.Parse(l.Args) if l.FlagSet.Parsed() is false. //It then calls one of the flaglib.FlagSet.Visit*() methods depending on the value //of l.LoadDefaults, and parses each flag's Name or alias and inserts it into the //returned Values. func (l *Loader) Load() (*config.Values, error) { if !l.FlagSet.Parsed() { args := l.Args err := l.FlagSet.Parse(args) if err != nil { if err != flaglib.ErrHelp { return nil, err } } } v := config.NewValues() visit := l.FlagSet.Visit if l.LoadDefaults { visit = l.FlagSet.VisitAll } visit(func(f *flaglib.Flag) { l.putFlagIntoValues(v, f) }) return v, nil }