diff --git a/json/json.go b/json/json.go index aa3edf5..907b2ff 100644 --- a/json/json.go +++ b/json/json.go @@ -79,9 +79,6 @@ const ( messageStartInlineMap = "message %s {\n" // 内联message体开始 messageStartMap = "%s %s%s `%s:\"%s\"`" // 拆开结构体开始, TODO messageEndMap = "}\n" // 拆开结构体结束, TODO - messageEmptyMap = "%s struct {" + - "} `%s:\"%s\"`" + - "}" //messageNilFmt = "%s any `%s:\"%s\"`" //TODO protobuf 暂时忽略 messageStringFmt = "%sstring %s = %d;" //repeated? int32 id = 2; messageBoolFmt = "%sbool %s = %d;" //repeated? int32 id = 2; @@ -97,8 +94,7 @@ const ( endInlineMap = "} `%s:\"%s\"`" // 内联结构体结束 startMap = "%s %s%s `%s:\"%s\"`" // 拆开结构体开始 emptyMap = "%s struct {" + - "} `%s:\"%s\"`" + - "}" + "} `%s:\"%s\"`" defStructName = "AutoGenerated" nilFmt = "%s interface{} `%s:\"%s\"`" stringFmt = "%s %sstring `%s:\"%s\"`" @@ -458,7 +454,7 @@ func (f *JSON) marshalValue(key string, obj any, fromArray bool, depth int, buf case []any: if len(v) == 0 { if !f.IsProtobuf { - buf.WriteString(fmt.Sprintf("%s interface{} `json:\"%s\"`", fieldName, key)) + buf.WriteString(fmt.Sprintf("%s interface{} `%s:\"%s\"`", fieldName, f.Tag, key)) } return } diff --git a/testdata/data0.yaml b/testdata/data0.yaml new file mode 100644 index 0000000..76707ae --- /dev/null +++ b/testdata/data0.yaml @@ -0,0 +1,3 @@ +payload: + ext: {} + bodies: [] diff --git a/testdata/need0.yaml.0.struct b/testdata/need0.yaml.0.struct new file mode 100644 index 0000000..5903b93 --- /dev/null +++ b/testdata/need0.yaml.0.struct @@ -0,0 +1,6 @@ +type reqName struct { + Payload struct { + Bodies interface{} `yaml:"bodies"` + Ext struct{} `yaml:"ext"` + } `yaml:"payload"` +} diff --git a/testdata/need0.yaml.1.struct b/testdata/need0.yaml.1.struct new file mode 100644 index 0000000..ae01942 --- /dev/null +++ b/testdata/need0.yaml.1.struct @@ -0,0 +1,8 @@ +type reqName struct { + Payload Payload `yaml:"payload"` +} + +type Payload struct { + Bodies interface{} `yaml:"bodies"` + Ext struct{} `yaml:"ext"` +} diff --git a/yaml/yaml_test.go b/yaml/yaml_test.go index c893aa5..90a34da 100644 --- a/yaml/yaml_test.go +++ b/yaml/yaml_test.go @@ -49,3 +49,41 @@ f: assert.Equal(t, string(bytes.TrimSpace(need)), string(bytes.TrimSpace(all))) } } + +// 测试文件以 data编号命名 +// 生成后的结构体文件以 need编码.struct +const ( + dataFormat = "../testdata/data%d.yaml" + needFormat1 = "../testdata/need%d.yaml.0.struct" + needFormat2 = "../testdata/need%d.yaml.1.struct" +) + +func Test_Gen_Obj_YAML2(t *testing.T) { + for i := 0; i < 1; i++ { + data, err := os.ReadFile(fmt.Sprintf(dataFormat, i)) + assert.NoError(t, err) + + need1, err := os.ReadFile(fmt.Sprintf(needFormat1, i)) + assert.NoError(t, err) + + need2, err := os.ReadFile(fmt.Sprintf(needFormat2, i)) + assert.NoError(t, err) + + var out bytes.Buffer + got1, err := Marshal(data, option.WithStructName("reqName"), option.WithOutputFmtBefore(&out)) + assert.NoError(t, err, out.String()) + out.Reset() + + got2, err := Marshal(data, option.WithStructName("reqName"), option.WithNotInline(), option.WithOutputFmtBefore(&out)) + assert.NoError(t, err, out.String()) + + need1 = bytes.TrimSpace(need1) + need2 = bytes.TrimSpace(need2) + + got1 = bytes.TrimSpace(got1) + got2 = bytes.TrimSpace(got2) + + assert.Equal(t, need1, got1) + assert.Equal(t, need2, got2) + } +}