Skip to content

Commit

Permalink
Dbd/encode decode enums with no structs (#32)
Browse files Browse the repository at this point in the history
* Add a test case for a bug we found on Mobile where Objects with an Enum but no Struct did not convert properly.

* Add missing files from prev commit.

* Add solution for new case that prev commit just addressed.
  • Loading branch information
danielbdavis authored Oct 3, 2018
1 parent b21c6f0 commit b93202d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 36 deletions.
10 changes: 7 additions & 3 deletions firemodel.example.firemodel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
option ts.namespace = "example";

enum TestDirection {
enum TestEnum {
left,
right,
up,
Expand Down Expand Up @@ -31,13 +31,13 @@ model TestModel {
reference<TestModel> friend;
geopoint location;
array<string> colors;
array<TestDirection> directions;
array<TestEnum> directions;
array<TestStruct> models;
array<reference> refs;
array<reference<TestTimestamps>> model_refs;
map meta;
map<string> meta_strs;
TestDirection direction;
TestEnum direction;
File test_file;
URL url;
TestStruct nested;
Expand All @@ -48,3 +48,7 @@ model TestTimestamps {
option firestore.path = "timestamps/{test_timestamps_id}";
option firestore.autotimestamp = true;
}

model Test {
TestEnum direction;
}
8 changes: 4 additions & 4 deletions langs/ios/ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var (
"filterFieldsEnumsOnly": filterFieldsEnumsOnly,
"filterFieldsNonEnumsOnly": filterFieldsNonEnumsOnly,
"filterFieldsStructsOnly": filterFieldsStructsOnly,
"hasFieldsOrStructs": hasFieldsOrStructs,
"requiresCustomEncodeDecode": requiresCustomEncodeDecode,
"firestorePath": firestorePath,
}).
Parse(file),
Expand All @@ -52,8 +52,8 @@ var (
_ = template.Must(tpl.New("struct").Parse(structTpl))
)

func hasFieldsOrStructs(in []*firemodel.SchemaField) bool {
if len(filterFieldsStructsOnly(in)) > 0 {
func requiresCustomEncodeDecode(in []*firemodel.SchemaField) bool {
if len(filterFieldsEnumsOnly(in)) > 0 {
return true
}
if len(filterFieldsStructsOnly(in)) > 0 {
Expand Down Expand Up @@ -245,7 +245,7 @@ import Pring
{{- end}}
dynamic var {{.Name | toLowerCamel}}: Pring.NestedCollection<{{.Type.Name}}> = []
{{- end}}
{{- if .Fields | hasFieldsOrStructs }}
{{- if .Fields | requiresCustomEncodeDecode }}
override func encode(_ key: String, value: Any?) -> Any? {
switch key {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// DO NOT EDIT - Code generated by firemodel (dev).

package firemodel

// TODO: Add comment to Test in firemodel schema.
type Test struct {
// TODO: Add comment to Test.direction.
Direction TestEnum `firestore:"direction"`
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// DO NOT EDIT - Code generated by firemodel (dev).

package firemodel

// TODO: Add comment to TestEnum in firemodel schema.
type TestEnum string

const (
// TODO: Add comment to TestEnum_LEFT in firemodel schema.
TestEnum_LEFT TestEnum = "LEFT"
// TODO: Add comment to TestEnum_RIGHT in firemodel schema.
TestEnum_RIGHT TestEnum = "RIGHT"
// TODO: Add comment to TestEnum_UP in firemodel schema.
TestEnum_UP TestEnum = "UP"
// TODO: Add comment to TestEnum_DOWN in firemodel schema.
TestEnum_DOWN TestEnum = "DOWN"
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type TestModel struct {
// TODO: Add comment to TestModel.colors.
Colors []string `firestore:"colors"`
// TODO: Add comment to TestModel.directions.
Directions []TestDirection `firestore:"directions"`
Directions []TestEnum `firestore:"directions"`
// TODO: Add comment to TestModel.models.
Models []*TestStruct `firestore:"models"`
// TODO: Add comment to TestModel.refs.
Expand All @@ -45,7 +45,7 @@ type TestModel struct {
// TODO: Add comment to TestModel.meta_strs.
MetaStrs map[string]string `firestore:"metaStrs"`
// TODO: Add comment to TestModel.direction.
Direction TestDirection `firestore:"direction"`
Direction TestEnum `firestore:"direction"`
// TODO: Add comment to TestModel.test_file.
TestFile *runtime.File `firestore:"testFile"`
// TODO: Add comment to TestModel.url.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import Foundation
import Pring

// TODO: Add documentation to TestDirection in firemodel schema.
@objc enum TestDirection: Int {
// TODO: Add documentation to TestEnum in firemodel schema.
@objc enum TestEnum: Int {
// TODO: Add documentation to Left in firemodel schema.
case left
// TODO: Add documentation to Right in firemodel schema.
Expand All @@ -15,7 +15,7 @@ import Pring
case down
}

extension TestDirection: CustomDebugStringConvertible {
extension TestEnum: CustomDebugStringConvertible {
init?(firestoreValue value: Any?) {
guard let value = value as? String else {
return nil
Expand Down Expand Up @@ -82,7 +82,7 @@ static var testModelId: String = ""
// TODO: Add documentation to colors in firemodel schema.
dynamic var colors: [String]?
// TODO: Add documentation to directions in firemodel schema.
dynamic var directions: [TestDirection]?
dynamic var directions: [TestEnum]?
// TODO: Add documentation to models in firemodel schema.
dynamic var models: [TestStruct]?
// TODO: Add documentation to refs in firemodel schema.
Expand All @@ -94,7 +94,7 @@ static var testModelId: String = ""
// TODO: Add documentation to metaStrs in firemodel schema.
dynamic var metaStrs: [String: String] = [:]
// TODO: Add documentation to direction in firemodel schema.
dynamic var direction: TestDirection?
dynamic var direction: TestEnum?
// TODO: Add documentation to testFile in firemodel schema.
dynamic var testFile: Pring.File?
// TODO: Add documentation to url in firemodel schema.
Expand All @@ -119,7 +119,7 @@ static var testModelId: String = ""
override func decode(_ key: String, value: Any?) -> Bool {
switch key {
case "direction":
self.direction = TestDirection(firestoreValue: value)
self.direction = TestEnum(firestoreValue: value)
case "nested":
if let value = value as? [String: Any] {
self.nested = TestStruct(id: self.id, value: value)
Expand All @@ -137,3 +137,30 @@ static var testModelId: String = ""
static var testTimestampsId: String = ""
override class var path: String { return "timestamps/\(testTimestampsId)" }
}

// TODO: Add documentation to Test in firemodel schema.
@objcMembers class Test: Pring.Object {

// TODO: Add documentation to direction in firemodel schema.
dynamic var direction: TestEnum?

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

override func decode(_ key: String, value: Any?) -> Bool {
switch key {
case "direction":
self.direction = TestEnum(firestoreValue: value)
default:
break
}
return false
}
}
14 changes: 10 additions & 4 deletions testfixtures/firemodel/TestFiremodelFromSchema/ts/firemodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ export namespace example {
name: string;
}

/** TODO: Add documentation to TestDirection in firemodel schema. */
export enum TestDirection {
/** TODO: Add documentation to TestEnum in firemodel schema. */
export enum TestEnum {
/** TODO: Add documentation to left in firemodel schema. */
left = 'LEFT',
/** TODO: Add documentation to right in firemodel schema. */
Expand Down Expand Up @@ -176,7 +176,7 @@ export namespace example {
/** TODO: Add documentation to colors in firemodel schema. */
colors?: string[];
/** TODO: Add documentation to directions in firemodel schema. */
directions?: TestDirection[];
directions?: TestEnum[];
/** TODO: Add documentation to models in firemodel schema. */
models?: ITestStruct[];
/** TODO: Add documentation to refs in firemodel schema. */
Expand All @@ -188,7 +188,7 @@ export namespace example {
/** TODO: Add documentation to meta_strs in firemodel schema. */
metaStrs?: { [key: string]: string; };
/** TODO: Add documentation to direction in firemodel schema. */
direction?: TestDirection;
direction?: TestEnum;
/** TODO: Add documentation to test_file in firemodel schema. */
testFile?: IFile;
/** TODO: Add documentation to url in firemodel schema. */
Expand All @@ -210,4 +210,10 @@ export namespace example {
/** Record update timestamp. */
updatedAt?: firestore.Timestamp;
}

/** TODO: Add documentation to Test in firemodel schema. */
export interface ITest {
/** TODO: Add documentation to direction in firemodel schema. */
direction?: TestEnum;
}
}

0 comments on commit b93202d

Please sign in to comment.