-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
130 lines (106 loc) · 3.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/go-faker/faker/v4"
"github.com/iancoleman/strcase"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/reflect/protoreflect"
)
const SpaceCharacterNum = 4
type ProtoMessage struct {
messages []protoreflect.FullName
}
func (protoMessage *ProtoMessage) Generate(plugin *protogen.Plugin) error {
protoFiles := plugin.Files
for _, file := range protoFiles {
for _, message := range file.Messages {
fileName := message.GoIdent.GoName
generatedFileName := fileName + "_fake.ts"
generatedFilePath := protogen.GoImportPath(file.Desc.Path())
t := plugin.NewGeneratedFile(generatedFileName, generatedFilePath)
var code []string
code = append(code, protoMessage.GenerateFakeDataClass(message)...)
t.P(strings.Join(code[:], "\n"))
}
}
return nil
}
func (protoMessage *ProtoMessage) GenerateFakeDataClass(message *protogen.Message) []string {
var code = []string{
fmt.Sprintf("export const %s = {", strcase.ToLowerCamel(string(message.Desc.Name()))),
}
str := protoMessage.GenerateStructForFaker(message).Interface()
err := faker.FakeData(&str)
if err != nil {
fmt.Println(err)
}
strValue := reflect.ValueOf(str)
strType := reflect.TypeOf(str)
for i := 0; i < strValue.NumField(); i++ {
field := strType.Field(i)
fieldValue := strValue.Field(i)
// TODO: Fix condition
if !fieldValue.CanInt() && !fieldValue.CanFloat() && !fieldValue.CanUint() {
v := fmt.Sprintf("\"%s\"", fieldValue.String())
code = append(code, fmt.Sprintf("%s%s: %v,", strings.Repeat(" ", SpaceCharacterNum), strcase.ToLowerCamel(field.Name), v))
} else {
code = append(code, fmt.Sprintf("%s%s: %v,", strings.Repeat(" ", SpaceCharacterNum), strcase.ToLowerCamel(field.Name), fieldValue))
}
}
code = append(code, "}")
return code
}
func (protoMessage *ProtoMessage) GenerateStructForFaker(message *protogen.Message) reflect.Value {
var fields []reflect.StructField
for _, field := range message.Fields {
str, err := mapProtoKindToGoTypes(field)
if err != nil {
// TODO: error handling
continue
}
fields = append(fields, str)
}
structDef := reflect.New(reflect.StructOf(fields))
return structDef.Elem()
}
func mapProtoKindToGoTypes(field *protogen.Field) (reflect.StructField, error) {
switch field.Desc.Kind() {
case protoreflect.BoolKind:
return reflect.StructField{
Name: field.GoName,
Type: reflect.TypeOf(true),
Tag: "",
}, nil
case protoreflect.Int64Kind, protoreflect.Int32Kind:
return reflect.StructField{
Name: field.GoName,
Type: reflect.TypeOf(0),
Tag: "",
}, nil
case protoreflect.FloatKind, protoreflect.DoubleKind:
return reflect.StructField{
Name: field.GoName,
Type: reflect.TypeOf(0.0),
Tag: "",
}, nil
case protoreflect.StringKind:
return reflect.StructField{
Name: field.GoName,
Type: reflect.TypeOf(""),
Tag: "",
}, nil
// TODO: Add more kinds
default:
return reflect.StructField{}, errors.New("invalid protoreflect type deteceted")
}
}
func main() {
var g = ProtoMessage{}
protogen.Options{
// TODO: Add some reguralization
ParamFunc: nil,
}.Run(g.Generate)
}