add json provider
Some checks failed
continuous-integration/drone/pr Build was killed
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-01-25 21:14:06 +03:00
parent b9af510e71
commit 59f97e1681
7 changed files with 211 additions and 1 deletions

View File

@@ -0,0 +1,55 @@
package json_test
import (
"context"
"fmt"
"log"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/json"
"gitoa.ru/go-4devs/config/test"
)
func ExampleClient_Value() {
ctx := context.Background()
// read json config
jsonConfig, jerr := fixture.ReadFile("fixture/config.json")
if jerr != nil {
log.Printf("failed load file:%v", jerr)
return
}
config, err := config.New(
json.New(jsonConfig),
)
if err != nil {
log.Print(err)
return
}
title, err := config.Value(ctx, "app.name.title")
if err != nil {
log.Print("app.name.title", err)
return
}
cfgValue, err := config.Value(ctx, "cfg")
if err != nil {
log.Print("cfg ", err)
return
}
cfg := test.Config{}
_ = cfgValue.Unmarshal(&cfg)
fmt.Printf("title from json: %v\n", title.String())
fmt.Printf("struct from json: %+v\n", cfg)
// Output:
// title from json: config title
// struct from json: {Duration:21m0s Enabled:true}
}