Skip to content

Commit

Permalink
ios: Add support for enum arrays (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeyreiss authored Mar 20, 2019
1 parent a15118b commit f9482a5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
34 changes: 34 additions & 0 deletions langs/ios/ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
"filterFieldsNonEnumsOnly": filterFieldsNonEnumsOnly,
"filterFieldsStructsOnly": filterFieldsStructsOnly,
"filterFieldsStructArraysOnly": filterFieldsStructArraysOnly,
"filterFieldsEnumArraysOnly": filterFieldsEnumArraysOnly,
"requiresCustomEncodeDecode": requiresCustomEncodeDecode,
"firestoreModelName": firestoreModelName,
}).
Expand Down Expand Up @@ -113,6 +114,21 @@ func filterFieldsStructArraysOnly(in []*firemodel.SchemaField) []*firemodel.Sche
return out
}

func filterFieldsEnumArraysOnly(in []*firemodel.SchemaField) []*firemodel.SchemaField {
var out []*firemodel.SchemaField
for _, i := range in {
t, ok := i.Type.(*firemodel.Array)
if !ok {
continue
}
if _, ok := t.T.(*firemodel.Enum); !ok {
continue
}
out = append(out, i)
}
return out
}

func toSwiftType(root bool, firetype firemodel.SchemaFieldType) string {
switch firetype := firetype.(type) {
case *firemodel.Boolean:
Expand Down Expand Up @@ -282,6 +298,10 @@ override class var path: String { return "{{firestoreModelName . }}" }
case "{{.Name | toLowerCamel}}":
return self.{{.Name | toLowerCamel}}?.rawValue
{{- end}}
{{- range .Fields | filterFieldsEnumArraysOnly}}
case "{{.Name | toLowerCamel}}":
return self.{{.Name | toLowerCamel}}?.map { $0.firestoreValue }
{{- end}}
default:
break
}
Expand All @@ -307,6 +327,11 @@ override class var path: String { return "{{firestoreModelName . }}" }
return true
}
{{- end}}
{{- range .Fields | filterFieldsEnumArraysOnly}}
case "{{.Name | toLowerCamel}}":
self.{{.Name | toLowerCamel}} = (value as? [String])?.compactMap { {{.Type.T | toSwiftType false }}(firestoreValue: $0) }
return true
{{- end}}
default:
break
}
Expand Down Expand Up @@ -390,6 +415,10 @@ extension {{.Name | toCamel}}: CustomDebugStringConvertible {
case "{{.Name | toLowerCamel}}":
return self.{{.Name | toLowerCamel}}?.rawValue
{{- end}}
{{- range .Fields | filterFieldsEnumArraysOnly}}
case "{{.Name | toLowerCamel}}":
return self.{{.Name | toLowerCamel}}?.map { $0.firestoreValue }
{{- end}}
default:
break
}
Expand All @@ -415,6 +444,11 @@ extension {{.Name | toCamel}}: CustomDebugStringConvertible {
return true
}
{{- end}}
{{- range .Fields | filterFieldsEnumArraysOnly}}
case "{{.Name | toLowerCamel}}":
self.{{.Name | toLowerCamel}} = (value as? [String])?.compactMap { {{.Type.T | toSwiftType false }}(firestoreValue: $0) }
return true
{{- end}}
default:
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ override class var path: String { return "test_models" }
return self.models2?.map { $0.rawValue }
case "nested":
return self.nested?.rawValue
case "directions":
return self.directions?.map { $0.firestoreValue }
default:
break
}
Expand All @@ -165,6 +167,9 @@ override class var path: String { return "test_models" }
self.nested = TestStruct(id: "\(0)", value: value)
return true
}
case "directions":
self.directions = (value as? [String])?.compactMap { TestEnum(firestoreValue: $0) }
return true
default:
break
}
Expand Down

0 comments on commit f9482a5

Please sign in to comment.