Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom de-/serialization of models: Are serialize and deserialize functions in FirestoreAttribute annotation possible? #33

Open
lukas-h opened this issue Jul 29, 2020 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@lukas-h
Copy link
Member

lukas-h commented Jul 29, 2020

class FakeEnum{
    String _val;
    FakeEnum._(this._val);
    factory FakeEnum.fromString(String val){
        switch(val) {
            case 'option1':
                return FakeEnum._(val);
                break;
            default:
                throw UnsupportedError('illegal value');
        }
    }
    factory FakeEnum.option1() => FakeEnum._('option1');
    
    @override
    toString() => _val;
}

@FirestoreDocument
class Model {
    @FirestoreAttribute(serialize: (FakeEnum val) => val.toString(), deserialize: (String value) => FakeEnum(String val))
    FakeEnum fakeEnum;
@lukas-h lukas-h self-assigned this Jul 29, 2020
@lukas-h lukas-h added the enhancement New feature or request label Jul 29, 2020
@lukas-h
Copy link
Member Author

lukas-h commented Jul 29, 2020

Another possibe way to write this:

class FakeEnum implements FirestoreSerializableHelper {
    final String _val;
    FakeEnum._(this._val);

    // demanded constructor by code generated for Model
    factory FakeEnum.deserialize(String val) => FakeEnum._(val);
    
    // demanded function by code generated for Model
   @override 
    String serialize() => _val;
}

@FirestoreDocument
class Model {
    @FirestoreAttribute(customSerialization: true)
    FakeEnum fakeEnum;

-> code generator uses by deserialize constructor and serialize function by default, if type has is not ignored, of supported type or not annotated with @FirestoreDocument.

@lukas-h lukas-h changed the title Are serialize and deserialize functions in FirestoreAttribute annotation possible? Custom de-/serialization of models: Are serialize and deserialize functions in FirestoreAttribute annotation possible? Jul 29, 2020
@lukas-h
Copy link
Member Author

lukas-h commented Jul 29, 2020

idea: introduce new annotation to mark a model with custom serialization

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant