added unmarshal

This commit is contained in:
2025-04-16 20:48:25 +01:00
parent cc4ffc9abc
commit 6eff475c6f
4 changed files with 135 additions and 2 deletions
+44
View File
@@ -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
}
+75
View File
@@ -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)
}