Skip to content

Commit 0c82632

Browse files
committedSep 29, 2020
added Swift type names
1 parent 9461014 commit 0c82632

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed
 

‎parser/parser.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ type FieldType struct {
114114
Package string `json:"package"`
115115
IsObject bool `json:"isObject"`
116116
JSType string `json:"jsType"`
117+
SwiftType string `json:"swiftType"`
117118
}
118119

119120
// Parser parses Oto Go definition packages.
@@ -378,20 +379,26 @@ func (p *Parser) parseFieldType(pkg *packages.Package, obj types.Object) (FieldT
378379
ftype.TypeID = pkgPath + "." + ftype.ObjectName
379380
if ftype.IsObject {
380381
ftype.JSType = "object"
382+
ftype.SwiftType = "Any"
381383
} else {
382384
switch ftype.TypeName {
383385
case "interface{}":
384386
ftype.JSType = "any"
387+
ftype.SwiftType = "Any"
385388
case "map[string]interface{}":
386389
ftype.JSType = "object"
390+
ftype.SwiftType = "Any"
387391
case "string":
388392
ftype.JSType = "string"
393+
ftype.SwiftType = "String"
389394
case "bool":
390395
ftype.JSType = "boolean"
396+
ftype.SwiftType = "Bool"
391397
case "int", "int16", "int32", "int64",
392398
"uint", "uint16", "uint32", "uint64",
393399
"float32", "float64":
394400
ftype.JSType = "number"
401+
ftype.SwiftType = "Double"
395402
}
396403
}
397404

@@ -407,8 +414,9 @@ func (p *Parser) addOutputFields() error {
407414
NameLowerCamel: "error",
408415
Comment: "Error is string explaining what went wrong. Empty if everything was fine.",
409416
Type: FieldType{
410-
TypeName: "string",
411-
JSType: "string",
417+
TypeName: "string",
418+
JSType: "string",
419+
SwiftType: "String",
412420
},
413421
Metadata: map[string]interface{}{},
414422
Example: "something went wrong",

‎parser/parser_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,18 @@ You will love it.`)
133133
is.Equal(welcomeInputObject.Fields[1].OmitEmpty, false)
134134
is.Equal(welcomeInputObject.Fields[1].Type.TypeName, "string")
135135
is.Equal(welcomeInputObject.Fields[1].Type.JSType, "string")
136+
is.Equal(welcomeInputObject.Fields[1].Type.SwiftType, "String")
136137
is.Equal(welcomeInputObject.Fields[1].Type.Multiple, false)
137138
is.Equal(welcomeInputObject.Fields[1].Type.Package, "")
138139
is.Equal(welcomeInputObject.Fields[1].Example, "John Smith")
139140

140141
is.Equal(welcomeInputObject.Fields[2].Example, float64(3))
141142
is.Equal(welcomeInputObject.Fields[2].Type.JSType, "number")
143+
is.Equal(welcomeInputObject.Fields[2].Type.SwiftType, "Double")
142144

143145
is.Equal(welcomeInputObject.Fields[3].Example, true)
144146
is.Equal(welcomeInputObject.Fields[3].Type.JSType, "boolean")
147+
is.Equal(welcomeInputObject.Fields[3].Type.SwiftType, "Bool")
145148

146149
welcomeOutputObject, err := def.Object(def.Services[1].Methods[0].OutputObject.TypeName)
147150
is.NoErr(err)
@@ -160,6 +163,8 @@ You will love it.`)
160163
is.Equal(welcomeOutputObject.Fields[1].Type.TypeName, "string")
161164
is.Equal(welcomeOutputObject.Fields[1].Type.Multiple, false)
162165
is.Equal(welcomeOutputObject.Fields[1].Type.Package, "")
166+
is.Equal(welcomeOutputObject.Fields[1].Type.JSType, "string")
167+
is.Equal(welcomeOutputObject.Fields[1].Type.SwiftType, "String")
163168
is.True(welcomeOutputObject.Metadata != nil)
164169

165170
is.Equal(len(def.Objects), 8)

0 commit comments

Comments
 (0)
Please sign in to comment.