Skip to content

Commit

Permalink
ios: Add custom encode/decode to struct classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mickey Reiss committed Oct 4, 2018
1 parent cc99e72 commit 8d5cfcf
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 13 deletions.
3 changes: 2 additions & 1 deletion firemodel.example.firemodel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum TestEnum {
struct TestStruct {
string where;
integer how_much;
TestEnum some_enum;
}

// A Test is a test model.
Expand Down Expand Up @@ -51,4 +52,4 @@ model TestTimestamps {

model Test {
TestEnum direction;
}
}
61 changes: 53 additions & 8 deletions langs/ios/ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,59 @@ extension {{.Name | toCamel}}: CustomDebugStringConvertible {
// TODO: Add documentation to {{.Name}} in firemodel schema.
{{- end}}
@objcMembers class {{.Name | toCamel }}: Pring.Object {
{{- range .Fields}}
{{- if .Comment}}
// {{.Comment}}
{{- else}}
// TODO: Add documentation to {{.Name}} in firemodel schema.
{{- end}}
var {{.Name | toLowerCamel -}}: {{.Type | toSwiftType true}}
{{- end}}
{{- range .Fields}}
{{- if .Comment}}
// {{.Comment}}
{{- else}}
// TODO: Add documentation to {{.Name}} in firemodel schema.
{{- end}}
var {{.Name | toLowerCamel -}}: {{.Type | toSwiftType true}}
{{- end}}
{{- if .Fields | requiresCustomEncodeDecode }}
override func encode(_ key: String, value: Any?) -> Any? {
switch key {
{{- range .Fields | filterFieldsEnumsOnly}}
case "{{.Name | toLowerCamel}}":
return self.{{.Name | toLowerCamel}}?.firestoreValue
{{- end}}
{{- range .Fields | filterFieldsStructArraysOnly}}
case "{{.Name | toLowerCamel}}":
return self.{{.Name | toLowerCamel}}?.map { $0.rawValue }
{{- end}}
{{- range .Fields | filterFieldsStructsOnly}}
case "{{.Name | toLowerCamel}}":
return self.{{.Name | toLowerCamel}}?.rawValue
{{- end}}
default:
break
}
return nil
}
override func decode(_ key: String, value: Any?) -> Bool {
switch key {
{{- range .Fields | filterFieldsEnumsOnly}}
case "{{.Name | toLowerCamel}}":
self.{{.Name | toLowerCamel}} = {{.Type | toSwiftType false }}(firestoreValue: value)
{{- end}}
{{- range .Fields | filterFieldsStructArraysOnly}}
case "{{.Name | toLowerCamel}}":
self.{{.Name | toLowerCamel}} = (value as? [[String: Any]])?.map { {{.Type.T | toSwiftType false }}(id: self.id, value: $0) }
{{- end}}
{{- range .Fields | filterFieldsStructsOnly}}
case "{{.Name | toLowerCamel}}":
if let value = value as? [String: Any] {
self.{{.Name | toLowerCamel}} = {{.Type | toSwiftType false}}(id: self.id, value: value)
return true
}
{{- end}}
default:
break
}
return false
}
{{- end}}
}
`
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ type TestStruct struct {
Where string `firestore:"where"`
// TODO: Add comment to TestStruct.how_much.
HowMuch int64 `firestore:"howMuch"`
// TODO: Add comment to TestStruct.some_enum.
SomeEnum TestEnum `firestore:"someEnum"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,32 @@ extension TestEnum: CustomDebugStringConvertible {

// TODO: Add documentation to TestStruct in firemodel schema.
@objcMembers class TestStruct: Pring.Object {
// TODO: Add documentation to where in firemodel schema.
var where: String?
// TODO: Add documentation to how_much in firemodel schema.
var howMuch: Int = 0
// TODO: Add documentation to where in firemodel schema.
var where: String?
// TODO: Add documentation to how_much in firemodel schema.
var howMuch: Int = 0
// TODO: Add documentation to some_enum in firemodel schema.
var someEnum: TestEnum?

override func encode(_ key: String, value: Any?) -> Any? {
switch key {
case "someEnum":
return self.someEnum?.firestoreValue
default:
break
}
return nil
}

override func decode(_ key: String, value: Any?) -> Bool {
switch key {
case "someEnum":
self.someEnum = TestEnum(firestoreValue: value)
default:
break
}
return false
}
}

// A Test is a test model.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ export namespace example {
where?: string;
/** TODO: Add documentation to how_much in firemodel schema. */
howMuch?: number;
/** TODO: Add documentation to some_enum in firemodel schema. */
someEnum?: TestEnum;
}

/** A Test is a test model. */
Expand Down

0 comments on commit 8d5cfcf

Please sign in to comment.