-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_fields.go
117 lines (112 loc) · 4.05 KB
/
gen_fields.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
package fauxrpc
import (
"github.com/sudorandom/fauxrpc/private/registry"
"google.golang.org/protobuf/reflect/protoreflect"
)
var kindToGenerator = map[protoreflect.Kind]func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value{
protoreflect.BoolKind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfBool(Bool(fd, opts))
return &v
},
protoreflect.EnumKind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfEnum(Enum(fd, opts))
return &v
},
protoreflect.StringKind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfString(String(fd, opts))
return &v
},
protoreflect.BytesKind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfBytes(Bytes(fd, opts))
return &v
},
protoreflect.Int32Kind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfInt32(Int32(fd, opts))
return &v
},
protoreflect.Sint32Kind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfInt32(SInt32(fd, opts))
return &v
},
protoreflect.Sfixed32Kind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfInt32(SFixed32(fd, opts))
return &v
},
protoreflect.Uint32Kind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfUint32(UInt32(fd, opts))
return &v
},
protoreflect.Fixed32Kind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfUint32(Fixed32(fd, opts))
return &v
},
protoreflect.Int64Kind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfInt64(Int64(fd, opts))
return &v
},
protoreflect.Sint64Kind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfInt64(SInt64(fd, opts))
return &v
},
protoreflect.Sfixed64Kind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfInt64(SFixed64(fd, opts))
return &v
},
protoreflect.Uint64Kind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfUint64(UInt64(fd, opts))
return &v
},
protoreflect.Fixed64Kind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfUint64(Fixed64(fd, opts))
return &v
},
protoreflect.FloatKind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfFloat32(Float32(fd, opts))
return &v
},
protoreflect.DoubleKind: func(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
v := protoreflect.ValueOfFloat64(Float64(fd, opts))
return &v
},
}
func FieldValue(fd protoreflect.FieldDescriptor, opts GenOptions) *protoreflect.Value {
if opts.MaxDepth <= 0 {
return nil
}
switch fd.Kind() {
case protoreflect.MessageKind:
switch string(fd.Message().FullName()) {
case "google.protobuf.Duration":
if val := GoogleDuration(fd, opts); val != nil {
v := protoreflect.ValueOf(val.ProtoReflect())
return &v
}
case "google.protobuf.Timestamp":
if val := GoogleTimestamp(fd, opts); val != nil {
v := protoreflect.ValueOf(val.ProtoReflect())
return &v
}
case "google.protobuf.Any":
return nil
case "google.protobuf.Value":
if val := GoogleValue(fd, opts); val != nil {
v := protoreflect.ValueOf(val.ProtoReflect())
return &v
}
}
nested := registry.NewMessage(fd.Message())
_ = setDataOnMessage(nested.Interface(), opts.nested())
v := protoreflect.ValueOf(nested)
return &v
case protoreflect.GroupKind:
nested := registry.NewMessage(fd.Message())
_ = setDataOnMessage(nested.Interface(), opts.nested())
v := protoreflect.ValueOf(nested)
return &v
}
fn, ok := kindToGenerator[fd.Kind()]
if !ok {
return nil
}
return fn(fd, opts.nested())
}