added unmarshal
This commit is contained in:
parent
cc4ffc9abc
commit
6eff475c6f
|
@ -35,6 +35,28 @@ type Action struct {
|
|||
Fail string `json:"fail" yaml:"fail"`
|
||||
}
|
||||
|
||||
func (a *Action) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
var tmp struct {
|
||||
Type string `yaml:"type"`
|
||||
NewConfig map[string]interface{} `yaml:"config"`
|
||||
Next string `yaml:"next"`
|
||||
Fail string `yaml:"fail"`
|
||||
}
|
||||
if err := unmarshal(&tmp); err != nil {
|
||||
return err
|
||||
}
|
||||
config, err := json.Marshal(tmp.NewConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.Type = tmp.Type
|
||||
a.Config = config
|
||||
a.NewConfig = tmp.NewConfig
|
||||
a.Next = tmp.Next
|
||||
a.Fail = tmp.Fail
|
||||
return nil
|
||||
}
|
||||
|
||||
type ConditionalExpressions struct {
|
||||
Value string `json:"value" yaml:"value"`
|
||||
Type string `json:"type" yaml:"type"`
|
||||
|
@ -65,3 +87,25 @@ type DatasourceConfig struct {
|
|||
NewConfig map[string]interface{} `yaml:"config"`
|
||||
Type string `json:"type" yaml:"type"`
|
||||
}
|
||||
|
||||
func (d *DatasourceConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
var tmp struct {
|
||||
Type string `yaml:"type"`
|
||||
NewConfig map[string]interface{} `yaml:"config"`
|
||||
ID string `yaml:"id"`
|
||||
}
|
||||
if err := unmarshal(&tmp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := json.Marshal(tmp.NewConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.Type = tmp.Type
|
||||
d.Config = data
|
||||
d.ID = tmp.ID
|
||||
d.NewConfig = tmp.NewConfig
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
package apiconfig
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUnmarshalActionYAML(t *testing.T) {
|
||||
yamlData := `
|
||||
type: some_type
|
||||
config:
|
||||
key1: value1
|
||||
key2: value2
|
||||
next: next_action
|
||||
fail: fail_action
|
||||
`
|
||||
var action Action
|
||||
err := yaml.Unmarshal([]byte(yamlData), &action)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to unmarshal Action: %v", err)
|
||||
}
|
||||
|
||||
if action.Type != "some_type" {
|
||||
t.Errorf("Expected Type to be 'some_type', but got '%s'", action.Type)
|
||||
}
|
||||
|
||||
expectedConfig := map[string]interface{}{
|
||||
"key1": "value1",
|
||||
"key2": "value2",
|
||||
}
|
||||
|
||||
var config map[string]interface{}
|
||||
if err := json.Unmarshal(action.Config, &config); err != nil {
|
||||
t.Fatalf("Failed to unmarshal action.Config: %v", err)
|
||||
}
|
||||
|
||||
assert.Equal(t, expectedConfig, config)
|
||||
}
|
||||
|
||||
func TestUnmarshalDatasourceConfigYAML(t *testing.T) {
|
||||
yamlData := `
|
||||
id: some_id
|
||||
type: some_type
|
||||
config:
|
||||
key3: value3
|
||||
key4: value4
|
||||
`
|
||||
var datasourceConfig DatasourceConfig
|
||||
err := yaml.Unmarshal([]byte(yamlData), &datasourceConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to unmarshal DatasourceConfig: %v", err)
|
||||
}
|
||||
|
||||
if datasourceConfig.ID != "some_id" {
|
||||
t.Errorf("Expected ID to be 'some_id', but got '%s'", datasourceConfig.ID)
|
||||
}
|
||||
|
||||
if datasourceConfig.Type != "some_type" {
|
||||
t.Errorf("Expected Type to be 'some_type', but got '%s'", datasourceConfig.Type)
|
||||
}
|
||||
|
||||
expectedConfig := map[string]interface{}{
|
||||
"key3": "value3",
|
||||
"key4": "value4",
|
||||
}
|
||||
|
||||
var config map[string]interface{}
|
||||
if err := json.Unmarshal(datasourceConfig.Config, &config); err != nil {
|
||||
t.Fatalf("Failed to unmarshal datasourceConfig.Config: %v", err)
|
||||
}
|
||||
|
||||
assert.Equal(t, expectedConfig, config)
|
||||
}
|
4
go.mod
4
go.mod
|
@ -5,9 +5,13 @@ go 1.22.4
|
|||
require (
|
||||
google.golang.org/grpc v1.67.1
|
||||
google.golang.org/protobuf v1.35.1
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/testify v1.10.0 // indirect
|
||||
golang.org/x/net v0.28.0 // indirect
|
||||
golang.org/x/sys v0.24.0 // indirect
|
||||
golang.org/x/text v0.17.0 // indirect
|
||||
|
|
14
go.sum
14
go.sum
|
@ -1,3 +1,11 @@
|
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
|
||||
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
|
||||
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
|
||||
|
@ -8,7 +16,9 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 h1:
|
|||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
|
||||
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
|
||||
google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
|
||||
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
|
||||
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
||||
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
|
||||
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
Loading…
Reference in New Issue