github.com/yandex/pandora@v0.5.32/components/providers/scenario/vs/vs_json.go (about) 1 package vs 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/spf13/afero" 8 ) 9 10 type VariableSourceJSON struct { 11 Name string 12 File string 13 fs afero.Fs 14 store any 15 } 16 17 func (v *VariableSourceJSON) GetName() string { 18 return v.Name 19 } 20 21 func (v *VariableSourceJSON) GetVariables() any { 22 return v.store 23 } 24 25 func (v *VariableSourceJSON) Init() (err error) { 26 const op = "VariableSourceJSON.Init" 27 var file afero.File 28 file, err = v.fs.Open(v.File) 29 if err != nil { 30 return fmt.Errorf("%s fs.Open %w", op, err) 31 } 32 defer func() { 33 closeErr := file.Close() 34 if closeErr != nil { 35 if err != nil { 36 err = fmt.Errorf("%s multiple errors faced: %w, with close err: %s", op, err, closeErr) 37 } else { 38 err = fmt.Errorf("%s, %w", op, closeErr) 39 } 40 } 41 }() 42 decoder := json.NewDecoder(file) 43 var data any 44 err = decoder.Decode(&data) 45 if err != nil { 46 return fmt.Errorf("%s decoder.Decode %w", op, err) 47 } 48 v.store = data 49 50 return nil 51 } 52 53 func NewVSJson(cfg VariableSourceJSON, fs afero.Fs) (VariableSource, error) { 54 cfg.fs = fs 55 return &cfg, nil 56 } 57 58 var _ VariableSource = (*VariableSourceJSON)(nil)