updated proto

This commit is contained in:
2025-08-26 06:53:10 +01:00
parent 0c635f7140
commit 3a36b73698
4 changed files with 494 additions and 250 deletions
+30 -1
View File
@@ -1,6 +1,9 @@
package apiconfig
import "encoding/json"
import (
"encoding/json"
"git.servflow.io/servflow/definitions/proto"
)
type RequestType string
@@ -121,6 +124,32 @@ type ResponseObject struct {
Fields map[string]ResponseObject `json:"fields" yaml:"fields"`
}
func (o *ResponseObject) ToProto() *proto.ResponseObject {
resp := proto.ResponseObject{
Value: o.Value,
Fields: make(map[string]*proto.ResponseObject),
}
for k, v := range o.Fields {
resp.Fields[k] = v.ToProto()
}
return &resp
}
func ResponseObjectFromProto(o *proto.ResponseObject) ResponseObject {
resp := ResponseObject{
Value: o.Value,
Fields: make(map[string]ResponseObject),
}
for k, v := range o.Fields {
resp.Fields[k] = ResponseObjectFromProto(v)
}
return resp
}
type DatasourceConfig struct {
ID string `json:"id" yaml:"id"`
Config json.RawMessage `json:"config" yaml:"-"`
+50 -5
View File
@@ -2,17 +2,18 @@ package apiconfig
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestUnmarshalActionYAML(t *testing.T) {
yamlData := `
type: some_type
config:
key1: value1
key2: value2
key1: value1
key2: value2
next: next_action
fail: fail_action
`
@@ -44,8 +45,8 @@ func TestUnmarshalDatasourceConfigYAML(t *testing.T) {
id: some_id
type: some_type
config:
key3: value3
key4: value4
key3: value3
key4: value4
`
var datasourceConfig DatasourceConfig
err := yaml.Unmarshal([]byte(yamlData), &datasourceConfig)
@@ -73,3 +74,47 @@ config:
assert.Equal(t, expectedConfig, config)
}
func TestResponseObjectToProtoAndBack(t *testing.T) {
original := ResponseObject{
Value: "root_value",
Fields: map[string]ResponseObject{
"field1": {
Value: "field1_value",
Fields: map[string]ResponseObject{
"nested1": {
Value: "nested1_value",
Fields: map[string]ResponseObject{},
},
},
},
"field2": {
Value: "field2_value",
Fields: map[string]ResponseObject{},
},
},
}
// Convert to proto
protoObj := original.ToProto()
// Convert back from proto
result := ResponseObjectFromProto(protoObj)
// Verify the result matches the original
assert.Equal(t, original.Value, result.Value, "Root value should match")
assert.Equal(t, len(original.Fields), len(result.Fields), "Should have same number of fields")
// Check field1
assert.Equal(t, original.Fields["field1"].Value, result.Fields["field1"].Value, "field1 value should match")
assert.Equal(t, len(original.Fields["field1"].Fields), len(result.Fields["field1"].Fields), "field1 should have same number of nested fields")
// Check nested field
assert.Equal(t, original.Fields["field1"].Fields["nested1"].Value, result.Fields["field1"].Fields["nested1"].Value, "nested1 value should match")
// Check field2
assert.Equal(t, original.Fields["field2"].Value, result.Fields["field2"].Value, "field2 value should match")
// Complete equality check
assert.Equal(t, original, result, "The converted object should be identical to the original")
}