fix(generator): inherit nested schema across operation params
This commit is contained in:
@@ -94,11 +94,15 @@ func AlignParamTypes(params []types.Param, schema []types.Param) []types.Param {
|
||||
schemaByCode[strings.ToLower(strings.TrimSpace(p.Code))] = p
|
||||
}
|
||||
for i, p := range params {
|
||||
if !p.HasSubParams {
|
||||
key := strings.ToLower(strings.TrimSpace(p.Code))
|
||||
sp, ok := schemaByCode[key]
|
||||
if !ok || !sp.HasSubParams {
|
||||
continue
|
||||
}
|
||||
key := strings.ToLower(strings.TrimSpace(p.Code))
|
||||
if sp, ok := schemaByCode[key]; ok && len(sp.SubParams) > 0 {
|
||||
params[i].HasSubParams = true
|
||||
if len(p.SubParams) == 0 {
|
||||
params[i].SubParams = append([]types.Param(nil), sp.SubParams...)
|
||||
} else if len(sp.SubParams) > 0 {
|
||||
params[i].SubParams = AlignParamTypes(p.SubParams, sp.SubParams)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package params
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"resource-generator/internal/types"
|
||||
)
|
||||
|
||||
func TestAlignParamTypesInheritsNestedStructure(t *testing.T) {
|
||||
schema := []types.Param{{
|
||||
Code: "jsonEnv",
|
||||
Type: "string",
|
||||
HasSubParams: true,
|
||||
SubParams: []types.Param{{
|
||||
Code: "DB_PASS",
|
||||
Type: "string",
|
||||
}},
|
||||
}}
|
||||
modify := []types.Param{{
|
||||
Code: "jsonEnv",
|
||||
Type: "map",
|
||||
}}
|
||||
|
||||
got := AlignParamTypes(modify, schema)
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("expected one parameter, got %d", len(got))
|
||||
}
|
||||
if !got[0].HasSubParams {
|
||||
t.Fatal("expected nested structure to be inherited")
|
||||
}
|
||||
if len(got[0].SubParams) != 1 || got[0].SubParams[0].Code != "DB_PASS" {
|
||||
t.Fatalf("expected schema sub-params to be inherited, got %#v", got[0].SubParams)
|
||||
}
|
||||
if got[0].Type != "string" {
|
||||
t.Fatalf("expected type to align with schema, got %q", got[0].Type)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user