...
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 package vanity
30
31 import (
32 "github.com/gogo/protobuf/gogoproto"
33 "github.com/gogo/protobuf/proto"
34 descriptor "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
35 )
36
37 func FieldHasBoolExtension(field *descriptor.FieldDescriptorProto, extension *proto.ExtensionDesc) bool {
38 if field.Options == nil {
39 return false
40 }
41 value, err := proto.GetExtension(field.Options, extension)
42 if err != nil {
43 return false
44 }
45 if value == nil {
46 return false
47 }
48 if value.(*bool) == nil {
49 return false
50 }
51 return true
52 }
53
54 func SetBoolFieldOption(extension *proto.ExtensionDesc, value bool) func(field *descriptor.FieldDescriptorProto) {
55 return func(field *descriptor.FieldDescriptorProto) {
56 if FieldHasBoolExtension(field, extension) {
57 return
58 }
59 if field.Options == nil {
60 field.Options = &descriptor.FieldOptions{}
61 }
62 if err := proto.SetExtension(field.Options, extension, &value); err != nil {
63 panic(err)
64 }
65 }
66 }
67
68 func TurnOffNullable(field *descriptor.FieldDescriptorProto) {
69 if field.IsRepeated() && !field.IsMessage() {
70 return
71 }
72 SetBoolFieldOption(gogoproto.E_Nullable, false)(field)
73 }
74
75 func TurnOffNullableForNativeTypes(field *descriptor.FieldDescriptorProto) {
76 if field.IsRepeated() || field.IsMessage() {
77 return
78 }
79 SetBoolFieldOption(gogoproto.E_Nullable, false)(field)
80 }
81
82 func TurnOffNullableForNativeTypesWithoutDefaultsOnly(field *descriptor.FieldDescriptorProto) {
83 if field.IsRepeated() || field.IsMessage() {
84 return
85 }
86 if field.DefaultValue != nil {
87 return
88 }
89 SetBoolFieldOption(gogoproto.E_Nullable, false)(field)
90 }
91
View as plain text