updated proto
This commit is contained in:
+33
-1
@@ -1,6 +1,9 @@
|
||||
package apiconfig
|
||||
|
||||
import "encoding/json"
|
||||
import (
|
||||
"encoding/json"
|
||||
"git.servflow.io/servflow/definitions/proto"
|
||||
)
|
||||
|
||||
type RequestType string
|
||||
|
||||
@@ -121,6 +124,35 @@ 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 {
|
||||
if o == nil {
|
||||
return 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:"-"`
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -63,12 +63,23 @@ func ProtoConfigToAPIConfig(configs []*proto.WorkflowConfig) []*APIConfig {
|
||||
}
|
||||
|
||||
for key := range protoConfig.Responses {
|
||||
builderType := ""
|
||||
switch protoConfig.Responses[key].GetBuilderType() {
|
||||
case proto.ResponseBuilderType_TYPE_JSON:
|
||||
builderType = "json"
|
||||
case proto.ResponseBuilderType_TYPE_OBJECT:
|
||||
builderType = "object"
|
||||
default:
|
||||
builderType = "json"
|
||||
}
|
||||
resp := protoConfig.Responses[key]
|
||||
a.Responses[key] = ResponseConfig{
|
||||
Code: int(resp.GetCode()),
|
||||
Type: resp.Type,
|
||||
Template: string(resp.Template),
|
||||
ShouldStream: resp.ShouldStream,
|
||||
BuilderType: builderType,
|
||||
Object: ResponseObjectFromProto(resp.GetResponseObject()),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user