Skip to content

Commit ac9d84b

Browse files
committed
Revert "Add optional json annotations for custom types"
This reverts commit 669361d.
1 parent 669361d commit ac9d84b

File tree

4 files changed

+27
-96
lines changed

4 files changed

+27
-96
lines changed

packages/go-gen/src/go.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ impl Display for GoField {
5050
self.ty,
5151
self.rust_name
5252
)?;
53-
if let Some(annotations) = &self.ty.json_annotations {
54-
write!(f, ",{}", annotations)?;
55-
}
5653
if self.ty.is_nullable {
5754
f.write_str(",omitempty")?;
5855
}
@@ -67,8 +64,6 @@ pub struct GoType {
6764
/// This will add `omitempty` to the json tag and use a pointer type if
6865
/// the type is not a basic type
6966
pub is_nullable: bool,
70-
/// Additional json annotations, if any
71-
pub json_annotations: Option<String>,
7267
}
7368

7469
impl GoType {
@@ -128,26 +123,22 @@ mod tests {
128123
let ty = GoType {
129124
name: "string".to_string(),
130125
is_nullable: true,
131-
json_annotations: None,
132126
};
133127
let ty2 = GoType {
134128
name: "string".to_string(),
135129
is_nullable: false,
136-
json_annotations: None,
137130
};
138131
assert_eq!(format!("{}", ty), "string");
139132
assert_eq!(format!("{}", ty2), "string");
140133

141134
let ty = GoType {
142135
name: "FooBar".to_string(),
143136
is_nullable: true,
144-
json_annotations: None,
145137
};
146138
assert_eq!(format!("{}", ty), "*FooBar");
147139
let ty = GoType {
148140
name: "FooBar".to_string(),
149141
is_nullable: false,
150-
json_annotations: None,
151142
};
152143
assert_eq!(format!("{}", ty), "FooBar");
153144
}
@@ -160,7 +151,6 @@ mod tests {
160151
ty: GoType {
161152
name: "string".to_string(),
162153
is_nullable: true,
163-
json_annotations: None,
164154
},
165155
};
166156
assert_eq!(
@@ -174,7 +164,6 @@ mod tests {
174164
ty: GoType {
175165
name: "string".to_string(),
176166
is_nullable: false,
177-
json_annotations: None,
178167
},
179168
};
180169
assert_eq!(format!("{}", field), "FooBar string `json:\"foo_bar\"`");
@@ -185,7 +174,6 @@ mod tests {
185174
ty: GoType {
186175
name: "FooBar".to_string(),
187176
is_nullable: true,
188-
json_annotations: None,
189177
},
190178
};
191179
assert_eq!(
@@ -202,28 +190,12 @@ mod tests {
202190
ty: GoType {
203191
name: "string".to_string(),
204192
is_nullable: true,
205-
json_annotations: None,
206193
},
207194
};
208195
assert_eq!(
209196
format!("{}", field),
210197
"// foo_bar is a test field\nFooBar string `json:\"foo_bar,omitempty\"`"
211198
);
212-
213-
// now with additional json annotations
214-
let field = GoField {
215-
rust_name: "foo_bar".to_string(),
216-
docs: None,
217-
ty: GoType {
218-
name: "uint64".to_string(),
219-
is_nullable: true,
220-
json_annotations: Some("string".to_string()),
221-
},
222-
};
223-
assert_eq!(
224-
format!("{}", field),
225-
"FooBar uint64 `json:\"foo_bar,string,omitempty\"`"
226-
);
227199
}
228200

229201
#[test]
@@ -237,7 +209,6 @@ mod tests {
237209
ty: GoType {
238210
name: "string".to_string(),
239211
is_nullable: true,
240-
json_annotations: None,
241212
},
242213
}],
243214
};
@@ -255,7 +226,6 @@ mod tests {
255226
ty: GoType {
256227
name: "string".to_string(),
257228
is_nullable: true,
258-
json_annotations: None,
259229
},
260230
}],
261231
};

packages/go-gen/src/main.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,7 @@ pub fn build_enum_variant(
175175
);
176176
// we can unwrap here, because we checked the length above
177177
let (name, schema) = properties.first_key_value().unwrap();
178-
let GoType {
179-
name: ty,
180-
json_annotations,
181-
..
182-
} = schema_object_type(
178+
let GoType { name: ty, .. } = schema_object_type(
183179
schema.object()?,
184180
TypeContext::new(enum_name, name),
185181
additional_structs,
@@ -191,7 +187,6 @@ pub fn build_enum_variant(
191187
ty: GoType {
192188
name: ty,
193189
is_nullable: true, // always nullable
194-
json_annotations,
195190
},
196191
})
197192
}
@@ -529,26 +524,4 @@ mod tests {
529524
"#,
530525
);
531526
}
532-
533-
#[test]
534-
fn timestamp_works() {
535-
use cosmwasm_std::Timestamp;
536-
537-
#[cw_serde]
538-
struct A {
539-
a: Timestamp,
540-
b: Option<Timestamp>,
541-
}
542-
543-
let code = generate_go(cosmwasm_schema::schema_for!(A)).unwrap();
544-
assert_code_eq(
545-
code,
546-
r#"
547-
type A struct {
548-
A uint64 `json:"a,string"`
549-
B uint64 `json:"b,string,omitempty"`
550-
}
551-
"#,
552-
);
553-
}
554527
}

packages/go-gen/src/schema.rs

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ pub fn schema_object_type(
3939
let mut is_nullable = is_null(schema);
4040

4141
// if it has a title, use that
42-
let (ty, json_annotations) = if let Some(title) =
43-
schema.metadata.as_ref().and_then(|m| m.title.as_ref())
44-
{
42+
let ty = if let Some(title) = schema.metadata.as_ref().and_then(|m| m.title.as_ref()) {
4543
replace_custom_type(title)
4644
} else if let Some(reference) = &schema.reference {
4745
// if it has a reference, strip the path and use that
@@ -52,25 +50,17 @@ pub fn schema_object_type(
5250
.expect("split should always return at least one item"),
5351
))
5452
} else if let Some(t) = &schema.instance_type {
55-
(
56-
type_from_instance_type(schema, type_context, t, additional_structs)?,
57-
None,
58-
)
53+
type_from_instance_type(schema, type_context, t, additional_structs)?
5954
} else if let Some(subschemas) = schema.subschemas.as_ref().and_then(|s| s.any_of.as_ref()) {
6055
// check if one of them is null
61-
let nullable: Option<&SchemaObject> = nullable_type(subschemas)?;
56+
let nullable = nullable_type(subschemas)?;
6257
if let Some(non_null) = nullable {
6358
ensure!(subschemas.len() == 2, "multiple subschemas in anyOf");
6459
is_nullable = true;
6560
// extract non-null type
66-
let GoType {
67-
name,
68-
json_annotations,
69-
..
70-
} = schema_object_type(non_null, type_context, additional_structs)?;
71-
// let (ty, annotations) = replace_custom_type(&name);
72-
// (ty, annotations.or(json_annotations))
73-
(name, json_annotations)
61+
let GoType { name, .. } =
62+
schema_object_type(non_null, type_context, additional_structs)?;
63+
replace_custom_type(&name)
7464
} else {
7565
subschema_type(subschemas, type_context, additional_structs)
7666
.context("failed to get type of anyOf subschemas")?
@@ -89,7 +79,6 @@ pub fn schema_object_type(
8979
Ok(GoType {
9080
name: ty,
9181
is_nullable,
92-
json_annotations, // TODO: implement
9382
})
9483
}
9584

@@ -208,11 +197,11 @@ pub fn type_from_instance_type(
208197
// for nullable array item types, we have to use a pointer type, even for basic types,
209198
// so we can pass null as elements
210199
// otherwise they would just be omitted from the array
211-
if item_type.is_nullable {
200+
replace_custom_type(&if item_type.is_nullable {
212201
format!("[]*{}", item_type.name)
213202
} else {
214203
format!("[]{}", item_type.name)
215-
}
204+
})
216205
} else {
217206
unreachable!("instance type should be one of the above")
218207
})
@@ -244,7 +233,7 @@ pub fn subschema_type(
244233
subschemas: &[Schema],
245234
type_context: TypeContext,
246235
additional_structs: &mut Vec<GoStruct>,
247-
) -> Result<(String, Option<String>)> {
236+
) -> Result<String> {
248237
ensure!(
249238
subschemas.len() == 1,
250239
"multiple subschemas are not supported"
@@ -268,27 +257,26 @@ pub fn documentation(schema: &SchemaObject) -> Option<String> {
268257

269258
/// Maps special types to their Go equivalents.
270259
/// If the given type is not a special type, returns `None`.
271-
/// Otherwise, returns a tuple of the Go type name and additional json annotations.
272-
pub fn custom_type_of(ty: &str) -> Option<(&str, Option<&str>)> {
260+
pub fn custom_type_of(ty: &str) -> Option<&str> {
273261
match ty {
274-
"Uint64" => Some(("string", None)),
275-
"Uint128" => Some(("string", None)),
276-
"Int64" => Some(("string", None)),
277-
"Int128" => Some(("string", None)),
278-
"Binary" => Some(("[]byte", None)),
279-
"HexBinary" => Some(("Checksum", None)),
280-
"Addr" => Some(("string", None)),
281-
"Decimal" => Some(("string", None)),
282-
"Decimal256" => Some(("string", None)),
283-
"SignedDecimal" => Some(("string", None)),
284-
"SignedDecimal256" => Some(("string", None)),
285-
"Timestamp" => Some(("uint64", Some("string"))),
262+
"Uint64" => Some("string"),
263+
"Uint128" => Some("string"),
264+
"Int64" => Some("string"),
265+
"Int128" => Some("string"),
266+
"Binary" => Some("[]byte"),
267+
"HexBinary" => Some("Checksum"),
268+
"Addr" => Some("string"),
269+
"Decimal" => Some("string"),
270+
"Decimal256" => Some("string"),
271+
"SignedDecimal" => Some("string"),
272+
"SignedDecimal256" => Some("string"),
273+
"Timestamp" => Some("uint64"),
286274
_ => None,
287275
}
288276
}
289277

290-
pub fn replace_custom_type(ty: &str) -> (String, Option<String>) {
278+
pub fn replace_custom_type(ty: &str) -> String {
291279
custom_type_of(ty)
292-
.map(|(ty, json_annotations)| (ty.to_string(), json_annotations.map(String::from)))
293-
.unwrap_or_else(|| (ty.to_string(), None))
280+
.map(|ty| ty.to_string())
281+
.unwrap_or_else(|| ty.to_string())
294282
}

packages/go-gen/tests/cosmwasm_std__IbcMsg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type Coin struct {
3030
type IBCTimeout struct {
3131
Block *IBCTimeoutBlock `json:"block,omitempty"` // in wasmvm, this does not have "omitempty"
3232
// Nanoseconds since UNIX epoch
33-
Timestamp uint64 `json:"timestamp,string,omitempty"` // wasmvm has a "string" in here too
33+
Timestamp uint64 `json:"timestamp,omitempty"` // wasmvm has a "string" in here too
3434
}
3535

3636
// IBCTimeoutBlock Height is a monotonically increasing data type

0 commit comments

Comments
 (0)