github.com/lyft/flytestdlib@v0.3.12-0.20210213045714-8cdd111ecda1/config/accessor_test.go (about)

     1  package config
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"path/filepath"
     7  )
     8  
     9  func Example() {
    10  	// This example demonstrates basic usage of config sections.
    11  
    12  	//go:generate pflags OtherComponentConfig
    13  
    14  	type OtherComponentConfig struct {
    15  		DurationValue Duration `json:"duration-value"`
    16  		URLValue      URL      `json:"url-value"`
    17  		StringValue   string   `json:"string-value"`
    18  	}
    19  
    20  	// Each component should register their section in package init() or as a package var
    21  	section := MustRegisterSection("other-component", &OtherComponentConfig{})
    22  
    23  	// Override configpath to look for a custom location.
    24  	configPath := filepath.Join("testdata", "config.yaml")
    25  
    26  	// Initialize an accessor.
    27  	var accessor Accessor
    28  	// e.g.
    29  	// accessor = viper.NewAccessor(viper.Options{
    30  	//		StrictMode:   true,
    31  	//		SearchPaths: []string{configPath, configPath2},
    32  	// })
    33  
    34  	// Optionally bind to Pflags.
    35  	// accessor.InitializePflags(flags)
    36  
    37  	// Parse config from file or pass empty to rely on env variables and PFlags
    38  	err := accessor.UpdateConfig(context.Background())
    39  	if err != nil {
    40  		fmt.Printf("Failed to validate config from [%v], error: %v", configPath, err)
    41  		return
    42  	}
    43  
    44  	// Get parsed config value.
    45  	parsedConfig := section.GetConfig().(*OtherComponentConfig)
    46  	fmt.Printf("Config: %v", parsedConfig)
    47  }
    48  
    49  func Example_nested() {
    50  	// This example demonstrates registering nested config sections dynamically.
    51  
    52  	//go:generate pflags OtherComponentConfig
    53  
    54  	type OtherComponentConfig struct {
    55  		DurationValue Duration `json:"duration-value"`
    56  		URLValue      URL      `json:"url-value"`
    57  		StringValue   string   `json:"string-value"`
    58  	}
    59  
    60  	// Each component should register their section in package init() or as a package var
    61  	Section := MustRegisterSection("my-component", &MyComponentConfig{})
    62  
    63  	// Other packages can register their sections at the root level (like the above line) or as nested sections of other
    64  	// sections (like the below line)
    65  	NestedSection := Section.MustRegisterSection("nested", &OtherComponentConfig{})
    66  
    67  	// Override configpath to look for a custom location.
    68  	configPath := filepath.Join("testdata", "nested_config.yaml")
    69  
    70  	// Initialize an accessor.
    71  	var accessor Accessor
    72  	// e.g.
    73  	// accessor = viper.NewAccessor(viper.Options{
    74  	//		StrictMode:   true,
    75  	//		SearchPaths: []string{configPath, configPath2},
    76  	// })
    77  
    78  	// Optionally bind to Pflags.
    79  	// accessor.InitializePflags(flags)
    80  
    81  	// Parse config from file or pass empty to rely on env variables and PFlags
    82  	err := accessor.UpdateConfig(context.Background())
    83  	if err != nil {
    84  		fmt.Printf("Failed to validate config from [%v], error: %v", configPath, err)
    85  		return
    86  	}
    87  
    88  	// Get parsed config value.
    89  	parsedConfig := NestedSection.GetConfig().(*OtherComponentConfig)
    90  	fmt.Printf("Config: %v", parsedConfig)
    91  }