Skip to content

Commit

Permalink
More examples to Value.Populate and NewProviderGroup functions (#46)
Browse files Browse the repository at this point in the history
Add more examples to Value.Populate and NewProviderGroup functions.
  • Loading branch information
Alex authored Jul 31, 2017
1 parent bd07901 commit 351a34e
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 0 deletions.
72 changes: 72 additions & 0 deletions example_merge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package config_test

import (
"fmt"
"log"

"go.uber.org/config"
)

func ExampleNewProviderGroup() {
base, err := config.NewYAMLProviderFromBytes([]byte(`
config:
name: fx
pool: development
ports:
- 80
- 8080
`))

if err != nil {
log.Fatal(err)
}

prod, err := config.NewYAMLProviderFromBytes([]byte(`
config:
pool: production
ports:
- 443
`))

if err != nil {
log.Fatal(err)
}

// Provider is going to keep name from the base provider,
// but ports and pool values will be overridden by prod.
p := config.NewProviderGroup("merge", base, prod)

var c struct {
Name string
Pool string
Ports []uint
}

if err := p.Get("config").Populate(&c); err != nil {
log.Fatal(err)
}

fmt.Printf("%+v\n", c)
// Output:
// {Name:fx Pool:production Ports:[443]}
}
126 changes: 126 additions & 0 deletions example_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package config_test

import (
"fmt"
"log"

"go.uber.org/config"
)

func ExampleValue_Populate() {
p, err := config.NewStaticProvider(map[string]interface{}{
"example": map[string]interface{}{
"name": "uber",
"founded": 2009,
},
})

if err != nil {
log.Fatal(err)
}

var c struct {
Name string
Founded int
}

if err := p.Get("example").Populate(&c); err != nil {
log.Fatal(err)
}

fmt.Println(c)
// Output:
// {uber 2009}
}

func ExampleValue_Populate_slice() {
p, err := config.NewYAMLProviderFromBytes([]byte(`
slice:
- 1
- 2
- 3
`))

if err != nil {
log.Fatal(err)
}

var s []int
if err := p.Get("slice").Populate(&s); err != nil {
log.Fatal(err)
}

fmt.Println(s)
// Output:
// [1 2 3]
}

func ExampleValue_Populate_error() {
p, err := config.NewYAMLProviderFromBytes([]byte(`
bool: notBool
`))

if err != nil {
log.Fatal(err)
}

var b bool
fmt.Println(p.Get("bool").Populate(&b))
// Output:
// for key "bool": strconv.ParseBool: parsing "notBool": invalid syntax
}

func ExampleValue_WithDefault() {
p, err := config.NewYAMLProviderFromBytes([]byte(`
example:
override: fromYAML
`))

if err != nil {
log.Fatal(err)
}

type example struct {
Base string
Override string
}

d := example{Override: "default", Base: "default"}
fmt.Printf("Default value:\n%+v\n", d)

v, err := p.Get("example").WithDefault(d)
if err != nil {
log.Fatal(err)
}

if err := v.Populate(&d); err != nil {
log.Fatal(err)
}

fmt.Printf("Populate:\n%+v\n", d)
// Output:
// Default value:
// {Base:default Override:default}
// Populate:
// {Base:default Override:fromYAML}
}

0 comments on commit 351a34e

Please sign in to comment.