Exemplo n.º 1
0
func ExampleNewPrefixLowerUnderscoreLoader() {
	prefix := "ANW8THWNFQ4IWH874H__" //does not matter at all what this is. just don't want conflicts while testing.

	os.Setenv(prefix+"ONE_Two", "value")
	os.Setenv("not-my-prefix", "do not want")

	loader := NewPrefixLowerUnderscoreLoader(prefix)

	c := config.New()

	_, err := c.MergeLoaders(loader)
	if err != nil {
		fmt.Println(err)
	}

	want := config.New()
	want.Put("one.two", "value")

	if !c.EqualValues(want) {
		fmt.Println("we didn't get what we wanted")
	}

	fmt.Println(c.GetStringOk("one.two"))
	//Output:
	//value true
}
Exemplo n.º 2
0
func Example_keyPartTransform() {
	input := `{
		"foo": {
			"bar": "value"
		}
	}`

	inputReader := strings.NewReader(input)

	jsonLoader := &Loader{
		KeyPartTransform: strings.ToUpper,
	}

	loader := config.NewReaderFuncLoader(
		jsonLoader.LoadReader,
		inputReader,
	)

	c := config.New()
	_, err := c.MergeLoaders(loader)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(c.GetStringOk("FOO.BAR"))

	_, ok := c.GetOk("foo.bar")
	fmt.Println(ok)
	//Output:
	//value true
	//false
}
Exemplo n.º 3
0
func Example() {
	loader := New("")

	boolVar := false

	loader.FlagSet.String("s", "", "")
	loader.FlagSet.Int("i", 0, "")
	loader.FlagSet.BoolVar(&boolVar, "b", false, "")
	loader.FlagSet.Float64("f", 0, "")
	loader.FlagSet.String("withdefault", "default", "")
	loader.FlagSet.String("one-two-three", "", "")
	loader.FlagSet.String("aliased", "", "")

	loader.AddAlias("aliased", "a")

	//This means that "withdefault" -> "default" will be inserted in the resulting Config.
	//Leaving this blank would not insert that association because the -withdefault
	//flag is not set below.
	loader.LoadDefaults = true

	loader.Args = []string{
		"-s", "sValue",
		"-i", "12",
		"-b=true",
		"-f", "1.2",
		//notice no withdefault flag set
		"-one-two-three", "three",
		"-aliased", "aValue",
	}

	c := config.New()

	_, err := c.MergeLoaders(loader)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(c.GetStringOk("s"))
	fmt.Println(c.GetInt64Ok("i"))
	fmt.Println(c.GetBoolOk("b"))
	fmt.Println(c.GetFloat64Ok("f"))
	fmt.Println(c.GetStringOk("withdefault"))
	fmt.Println(c.GetStringOk("one.two.three"))
	fmt.Println(c.GetStringOk("a")) //notice the use of the "a" key - aliased above.
	//Output:
	//sValue true
	//12 true
	//true true
	//1.2 true
	//default true
	//three true
	//aValue true
}
Exemplo n.º 4
0
func Example() {
	input := `{
		"string": "foo bar",
		"bool": false,
		"int": 12345678,
		"float": 1234.5678,
		"nested1": {
			"nested2": {
				"nested3": "..."
			}
		},
		"nope": null
	}`

	inputReader := strings.NewReader(input)

	loader := config.NewReaderFuncLoader(
		(&Loader{}).LoadReader,
		inputReader,
	)

	c := config.New()
	_, err := c.MergeLoaders(loader)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(c.GetStringOk("string"))
	fmt.Println(c.GetBoolOk("bool"))
	fmt.Println(c.GetInt64Ok("int"))
	fmt.Println(c.GetFloat64Ok("float"))
	fmt.Println(c.GetStringOk("nested1.nested2.nested3"))
	fmt.Println(c.GetOk("nope"))

	fmt.Println()
	fmt.Println(c.GetInt64Ok("bool"))
	fmt.Println(c.GetOk("not present"))
	//Output:
	//foo bar true
	//false true
	//12345678 true
	//1234.5678 true
	//... true
	//<nil> true
	//
	//0 false
	//<nil> false
}