diff --git a/langs/ios/ios.go b/langs/ios/ios.go index cb9c943..da68716 100644 --- a/langs/ios/ios.go +++ b/langs/ios/ios.go @@ -42,6 +42,7 @@ var ( "filterFieldsNonEnumsOnly": filterFieldsNonEnumsOnly, "filterFieldsStructsOnly": filterFieldsStructsOnly, "filterFieldsStructArraysOnly": filterFieldsStructArraysOnly, + "filterFieldsEnumArraysOnly": filterFieldsEnumArraysOnly, "requiresCustomEncodeDecode": requiresCustomEncodeDecode, "firestoreModelName": firestoreModelName, }). @@ -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: @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/testfixtures/firemodel/TestFiremodelFromSchema/swift/Firemodel.swift b/testfixtures/firemodel/TestFiremodelFromSchema/swift/Firemodel.swift index 5daf878..70faf8f 100644 --- a/testfixtures/firemodel/TestFiremodelFromSchema/swift/Firemodel.swift +++ b/testfixtures/firemodel/TestFiremodelFromSchema/swift/Firemodel.swift @@ -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 } @@ -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 }