Skip to content

Commit

Permalink
Add test cases for structs with optional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Sep 13, 2023
1 parent d86b5b6 commit 666c010
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 3 deletions.
3 changes: 0 additions & 3 deletions internal/jennies/typescript/rawtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ func formatField(def ast.StructField, typesPkg string) []byte {
}

func formatType(def ast.Type, typesPkg string) string {
// todo: handle nullable
// maybe if nullable, append | null to the type?
switch def.Kind {
case ast.KindDisjunction:
return formatDisjunction(def.AsDisjunction(), typesPkg)
Expand Down Expand Up @@ -236,7 +234,6 @@ func formatScalar(val any) string {
items = append(items, formatScalar(item))
}

// TODO: we can't assume a list of strings
return fmt.Sprintf("[%s]", strings.Join(items, ", "))
}

Expand Down
21 changes: 21 additions & 0 deletions testdata/jennies/rawtypes/disjunctions.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@
}
},

{
"Name": "StringOrNull",
"Type": {
"Kind": "disjunction",
"Disjunction": {
"Branches": [
{
"Kind": "scalar",
"Scalar": {"ScalarKind": "string"}
},
{
"Kind": "scalar",
"Scalar": {"ScalarKind": "null"}
}
]
}
}
},

{
"Name": "SomeStruct",
"Type": {
Expand Down Expand Up @@ -124,6 +143,8 @@
// Refresh rate or disabled.
export type StringOrBool = string | boolean;

export type StringOrNull = string | null;

export interface SomeStruct {
fieldAny: any;
}
Expand Down
127 changes: 127 additions & 0 deletions testdata/jennies/rawtypes/struct_with_optional_fields.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Basic struct with scalar fields, all required.
-- ir.json --
{
"Package": "struct_optional_fields",
"Definitions": [
{
"Name": "SomeStruct",
"Type": {
"Kind": "struct",
"Struct": {
"Fields": [
{
"Name": "FieldRef",
"Required": false,
"Type": {
"Kind": "ref",
"Ref": {"ReferredType": "SomeOtherStruct"}
}
},
{
"Name": "FieldString",
"Required": false,
"Type": {
"Kind": "scalar",
"Scalar": {"ScalarKind": "string"}
}
},
{
"Name": "FieldAnonymousEnum",
"Required": false,
"Type": {
"Kind": "enum",
"Enum": {
"Values": [
{
"Name": "GreaterThan",
"Type": {
"Kind": "scalar",
"Scalar": {"ScalarKind": "string"}
},
"Value": ">"
},
{
"Name": "LessThan",
"Type": {
"Kind": "scalar",
"Scalar": {"ScalarKind": "string"}
},
"Value": "<"
}
]
}
}
},
{
"Name": "FieldArrayOfStrings",
"Required": false,
"Type": {
"Kind": "array",
"Array": {
"ValueType": {
"Kind": "scalar",
"Scalar": {"ScalarKind": "string"}
}
}
}
},
{
"Name": "FieldAnonymousStruct",
"Required": false,
"Type": {
"Kind": "struct",
"Struct": {
"Fields": [
{
"Name": "FieldAny",
"Required": true,
"Type": {
"Kind": "scalar",
"Scalar": {"ScalarKind": "any"}
}
}
]
}
}
}
]
}
}
},

{
"Name": "SomeOtherStruct",
"Type": {
"Kind": "struct",
"Struct": {
"Fields": [
{
"Name": "FieldAny",
"Required": true,
"Type": {
"Kind": "scalar",
"Scalar": {"ScalarKind": "any"}
}
}
]
}
}
}
]
}
-- out/jennies/TypescriptRawTypes --
== types/struct_optional_fields/types_gen.ts
export interface SomeStruct {
fieldRef?: SomeOtherStruct;
fieldString?: string;
fieldAnonymousEnum?: ">" | "<";
fieldArrayOfStrings?: string[];
fieldAnonymousStruct?: {
fieldAny: any;
};
}

export interface SomeOtherStruct {
fieldAny: any;
}

3 changes: 3 additions & 0 deletions testdata/jennies/rawtypes/struct_with_scalar_fields.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
{
"Name": "FieldAny",
"Required": true,
"Comments": ["Anything can go in there.", "Really, anything."],
"Type": {
"Kind": "scalar",
"Scalar": {"ScalarKind": "any"}
Expand Down Expand Up @@ -139,6 +140,8 @@
// a
// comment
export interface SomeStruct {
// Anything can go in there.
// Really, anything.
fieldAny: any;
fieldBool: boolean;
fieldBytes: string;
Expand Down

0 comments on commit 666c010

Please sign in to comment.