Skip to content

Commit

Permalink
开始支持protobuf (#5)
Browse files Browse the repository at this point in the history
* 开始支持protobuf

* 完善生成protobuf

* 优化下protobuf的缩进

* 修复问题
  • Loading branch information
guonaihong authored Feb 7, 2023
1 parent ccc4a98 commit eec06a1
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 38 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ require (
github.com/antlabs/gstl v0.0.5
github.com/gobeam/stringy v0.0.5
github.com/stretchr/testify v1.8.1
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
5 changes: 4 additions & 1 deletion internal/guesstype/guesstype.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ func IsBool(s string) bool {
return err == nil
}

func TypeOf(s string) string {
func TypeOf(s string, isprotobuf bool) string {
if IsFloat(s) {
return "float64"
}

if IsInt(s) {
if isprotobuf {
return "int32"
}
return "int"
}

Expand Down
2 changes: 1 addition & 1 deletion internal/guesstype/guesstype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ func TestTypeOf(t *testing.T) {
{"true", "bool"},
{"false", "bool"},
} {
assert.Equal(t, TypeOf(tc.data), tc.need)
assert.Equal(t, TypeOf(tc.data, false), tc.need)
}
}
58 changes: 51 additions & 7 deletions internal/map2struct/map2struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,49 @@ import (
"github.com/antlabs/tostruct/option"
)

func getStructStart(buf *bytes.Buffer, opt option.Option) {
if opt.IsProtobuf {
fmt.Fprintf(buf, "message %s {\n", opt.StructName)
return
}

fmt.Fprintf(buf, "type %s struct {", opt.StructName)
}

func getEmptyField(buf *bytes.Buffer, opt option.Option, fieldName string, tagStr string, id int) {

if opt.IsProtobuf {
fmt.Fprintf(buf, " string %s = %d;\n", fieldName, id)
return
}

fmt.Fprintf(buf, "%s string %s\n", fieldName, tagStr)
}

func getSliceField(buf *bytes.Buffer, opt option.Option, fieldName string, v string, tagStr string, id int) {

if opt.IsProtobuf {
fmt.Fprintf(buf, " repeated %s %s = %d;\n", guesstype.TypeOf(v, opt.IsProtobuf), fieldName, id)
return
}

fmt.Fprintf(buf, "%s []%s %s\n", fieldName, guesstype.TypeOf(v, opt.IsProtobuf), tagStr)
}

func getField(buf *bytes.Buffer, opt option.Option, fieldName string, v string, tagStr string, id int) {
if opt.IsProtobuf {
fmt.Fprintf(buf, " %s %s = %d;\n", guesstype.TypeOf(v, opt.IsProtobuf), fieldName, id)
return
}

fmt.Fprintf(buf, "%s %s %s\n", fieldName, guesstype.TypeOf(v, opt.IsProtobuf), tagStr)
}

func MapGenStruct(m map[string][]string, opt option.Option) (res []byte, err error) {
var out bytes.Buffer

structName := opt.StructName
tag := opt.Tag
fmt.Fprintf(&out, "type %s struct {", structName)
getStructStart(&out, opt)
var ks []string
for k := range m {
ks = append(ks, k)
Expand All @@ -27,6 +64,7 @@ func MapGenStruct(m map[string][]string, opt option.Option) (res []byte, err err
// 排序
sort.StringSlice(ks).Sort()

id := 1
for _, k := range ks {
v := m[k]
if opt.GetRawValue != nil {
Expand All @@ -44,16 +82,18 @@ func MapGenStruct(m map[string][]string, opt option.Option) (res []byte, err err

tagStr := fmt.Sprintf("`%s:%q`", tag, tagName)
if len(v) == 0 {
fmt.Fprintf(&out, "%s string %s\n", fieldName, tagStr)
continue
getEmptyField(&out, opt, fieldName, tagStr, id)
goto next
}

if len(v) > 1 {
fmt.Fprintf(&out, "%s []%s %s\n", fieldName, guesstype.TypeOf(v[0]), tagStr)
continue
getSliceField(&out, opt, fieldName, v[0], tagStr, id)
goto next
}

fmt.Fprintf(&out, "%s %s %s\n", fieldName, guesstype.TypeOf(v[0]), tagStr)
getField(&out, opt, fieldName, v[0], tagStr, id)
next:
id++
}

fmt.Fprint(&out, "}")
Expand All @@ -62,6 +102,10 @@ func MapGenStruct(m map[string][]string, opt option.Option) (res []byte, err err
opt.OutputFmtBefore.Write(out.Bytes())
}

if opt.IsProtobuf {
return out.Bytes(), nil
}

src, err := format.Source(out.Bytes())
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit eec06a1

Please sign in to comment.