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

toRepresentation/fromRepresentation for a single field? #1710

Open
timotheecour opened this issue Mar 14, 2017 · 3 comments
Open

toRepresentation/fromRepresentation for a single field? #1710

timotheecour opened this issue Mar 14, 2017 · 3 comments

Comments

@timotheecour
Copy link

timotheecour commented Mar 14, 2017

@s-ludwig
Can we define a toRepresentation/fromRepresentation for a single field?
eg if I have a single (or a few) problematic field but the other ones should follow the default serialization/deserialization rules

ideally it'd work as an UDA:

struct Foo{
 int normal1;
 string normal2;
 string fun1(){...}
 auto fun2(string repr){...}
 @toRepresentationCustom!fun1
 @fromRepresentationCustom!fun2
 byte[]bar;
}
@s-ludwig
Copy link
Member

A way to achieve this without UDA support is to use properties:

struct Foo {
    @ignore byte[] bar;
    @property @name("bar") {
        string barString() const { return ...; }
        void barString(string) { bar = ...; }
    }
}

toRepresentation would require a custom type, similar to this:

struct Bar {
    byte[] data;
    alias data this;
    string toRepresentation() { ... }
    static void fromRepresentation() {}
}

struct Foo {
    Bar bar;
}

@timotheecour
Copy link
Author

@s-ludwig Thanks!

  • how about if we want to override to/from Bson conversion (instead of to/from string), eg to map DateTime to Bson's DateTime (but more generally than that example)?
  • what if I have little control over Foo (eg it is auto generated such as via dproto), how would define barString outside of Foo (again, in the single field case) ?

Sorry for the noob questions ...

@timotheecour
Copy link
Author

ping on this, especially the 2nd point: what if I have little control over Foo (eg generated via dproto)

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

No branches or pull requests

2 participants