@@ -50,7 +50,7 @@ impl Display for GoField {
50
50
self . ty,
51
51
self . rust_name
52
52
) ?;
53
- if self . ty . is_nullable {
53
+ if let Nullability :: OmitEmpty | Nullability :: Nullable = self . ty . nullability {
54
54
f. write_str ( ",omitempty" ) ?;
55
55
}
56
56
f. write_str ( "\" `" )
@@ -60,10 +60,19 @@ impl Display for GoField {
60
60
pub struct GoType {
61
61
/// The name of the type in Go
62
62
pub name : String ,
63
- /// Whether the type should be nullable
64
- /// This will add `omitempty` to the json tag and use a pointer type if
65
- /// the type is not a basic type
66
- pub is_nullable : bool ,
63
+ /// Whether the type should be nullable / omitempty / etc.
64
+ pub nullability : Nullability ,
65
+ }
66
+
67
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
68
+ pub enum Nullability {
69
+ /// The type should be nullable
70
+ /// In Go, this will use a pointer type and add `omitempty` to the json tag
71
+ Nullable ,
72
+ /// The type should not be nullable, use the type as is
73
+ NonNullable ,
74
+ /// The type should be nullable by omitting it from the json object if it is empty
75
+ OmitEmpty ,
67
76
}
68
77
69
78
impl GoType {
@@ -95,7 +104,7 @@ impl GoType {
95
104
96
105
impl Display for GoType {
97
106
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
98
- if self . is_nullable && !self . is_basic_type ( ) {
107
+ if self . nullability == Nullability :: Nullable && !self . is_basic_type ( ) {
99
108
// if the type is nullable and not a basic type, use a pointer
100
109
f. write_char ( '*' ) ?;
101
110
}
@@ -122,23 +131,23 @@ mod tests {
122
131
fn go_type_display_works ( ) {
123
132
let ty = GoType {
124
133
name : "string" . to_string ( ) ,
125
- is_nullable : true ,
134
+ nullability : Nullability :: Nullable ,
126
135
} ;
127
136
let ty2 = GoType {
128
137
name : "string" . to_string ( ) ,
129
- is_nullable : false ,
138
+ nullability : Nullability :: NonNullable ,
130
139
} ;
131
140
assert_eq ! ( format!( "{}" , ty) , "string" ) ;
132
141
assert_eq ! ( format!( "{}" , ty2) , "string" ) ;
133
142
134
143
let ty = GoType {
135
144
name : "FooBar" . to_string ( ) ,
136
- is_nullable : true ,
145
+ nullability : Nullability :: Nullable ,
137
146
} ;
138
147
assert_eq ! ( format!( "{}" , ty) , "*FooBar" ) ;
139
148
let ty = GoType {
140
149
name : "FooBar" . to_string ( ) ,
141
- is_nullable : false ,
150
+ nullability : Nullability :: NonNullable ,
142
151
} ;
143
152
assert_eq ! ( format!( "{}" , ty) , "FooBar" ) ;
144
153
}
@@ -150,7 +159,7 @@ mod tests {
150
159
docs : None ,
151
160
ty : GoType {
152
161
name : "string" . to_string ( ) ,
153
- is_nullable : true ,
162
+ nullability : Nullability :: Nullable ,
154
163
} ,
155
164
} ;
156
165
assert_eq ! (
@@ -163,7 +172,7 @@ mod tests {
163
172
docs : None ,
164
173
ty : GoType {
165
174
name : "string" . to_string ( ) ,
166
- is_nullable : false ,
175
+ nullability : Nullability :: NonNullable ,
167
176
} ,
168
177
} ;
169
178
assert_eq ! ( format!( "{}" , field) , "FooBar string `json:\" foo_bar\" `" ) ;
@@ -173,7 +182,7 @@ mod tests {
173
182
docs : None ,
174
183
ty : GoType {
175
184
name : "FooBar" . to_string ( ) ,
176
- is_nullable : true ,
185
+ nullability : Nullability :: Nullable ,
177
186
} ,
178
187
} ;
179
188
assert_eq ! (
@@ -189,7 +198,7 @@ mod tests {
189
198
docs : Some ( "foo_bar is a test field" . to_string ( ) ) ,
190
199
ty : GoType {
191
200
name : "string" . to_string ( ) ,
192
- is_nullable : true ,
201
+ nullability : Nullability :: Nullable ,
193
202
} ,
194
203
} ;
195
204
assert_eq ! (
@@ -208,7 +217,7 @@ mod tests {
208
217
docs: None ,
209
218
ty: GoType {
210
219
name: "string" . to_string( ) ,
211
- is_nullable : true ,
220
+ nullability : Nullability :: Nullable ,
212
221
} ,
213
222
} ] ,
214
223
} ;
@@ -225,7 +234,7 @@ mod tests {
225
234
docs: None ,
226
235
ty: GoType {
227
236
name: "string" . to_string( ) ,
228
- is_nullable : true ,
237
+ nullability : Nullability :: Nullable ,
229
238
} ,
230
239
} ] ,
231
240
} ;
0 commit comments