Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Feb 24, 2023
1 parent eec06a1 commit d37bf9c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
8 changes: 2 additions & 6 deletions json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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\"`"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions testdata/data0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
payload:
ext: {}
bodies: []
6 changes: 6 additions & 0 deletions testdata/need0.yaml.0.struct
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type reqName struct {
Payload struct {
Bodies interface{} `yaml:"bodies"`
Ext struct{} `yaml:"ext"`
} `yaml:"payload"`
}
8 changes: 8 additions & 0 deletions testdata/need0.yaml.1.struct
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type reqName struct {
Payload Payload `yaml:"payload"`
}

type Payload struct {
Bodies interface{} `yaml:"bodies"`
Ext struct{} `yaml:"ext"`
}
38 changes: 38 additions & 0 deletions yaml/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit d37bf9c

Please sign in to comment.