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

java backend should generate factories/jsonbindings for type aliases #63

Open
timbod7 opened this issue Aug 28, 2018 · 0 comments
Open

Comments

@timbod7
Copy link
Collaborator

timbod7 commented Aug 28, 2018

There are circumstances where we need the type expression for the alias (eg inside dynamics).
Possibly emit a java class for the type alias, but which only has static factory and jsonbinding methods.

For reference the workaround is a helper function like:

  public static <T> JsonBinding<T> typeAliasJsonBinding(String moduleName, String typeAliasName, JsonBinding<T> jb) {
    return new JsonBinding<T>() {
      public Factory<T> factory() {
        return new Factory<T>() {
          @Override
          public T create() {
            return jb.factory().create();
          }
          @Override
          public T create(T other) {
            return jb.factory().create(other);
          }
          @Override
          public TypeExpr typeExpr() {
            return new TypeExpr(TypeRef.reference(new ScopedName(moduleName, typeAliasName)), new ArrayList<>(); }
        };
      }

      @Override
      public JsonElement toJson(T value) {
        return jb.toJson(value);
      }

      @Override
      public T fromJson(JsonElement json) {
        return jb.fromJson(json);
      }
    };
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant