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

Add ability to make Java models extend a class and implement interfaces. #205

Closed

Conversation

RicoYao
Copy link
Contributor

@RicoYao RicoYao commented May 14, 2019

Example:
plank --java_extends=BaseModel --java_implements="ModelInterfaceA,ModelInterfaceB"

Generates:
class Board extends BaseModel implements ModelInterfaceA, ModelInterfaceB { ...

In another diff, I'm adding the ability to add custom annotations and imports, so developers can also add @Override and import the extended class and implemented interfaces if necessary.

Copy link
Collaborator

@rahul-malik rahul-malik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we discuss this particular solution a bit more and its purpose? Sorry if we've covered this in another thread (it's been a while!). It seems a bit strange to have this be a global configuration setting IMO and I'd like to make sure we're going down a path that would be suitable as plank adopts more languages

@RicoYao
Copy link
Contributor Author

RicoYao commented May 15, 2019

Sure, perhaps we can work off of a specific example:

The Plank-generated model generates a mergeFrom method. We may need to call that method without knowing the specific model type in layers of our codebase that deal with Models in a more generic fashion (e.g. persistence layers). So my thought is to allow for defining an interface that the Plank-generated model happens to implement. We may also need something similar to get the ID of the model.

Model.java (not Plank-generated)

interface Model {
  public String getId();
}

MergeableModel.java (not Plank-generated)

interface MergeableModel<M: Model> {
  public M mergeFrom(M model);
}

ModelStorage.java (Not Plank-generated. This isn't really how our code is set up, it's just an example.)

class ModelStorage {
  public void updateModelStorage(Model model) {
    // Merge fields from previous version of the model before updating the storage layer
    if (model instanceof MergeableModel) {
      MergeableModel oldModel = getModelFromStorage(model.getClass(), model.getId());
      model = oldModel.mergeFrom(model);
    }
    storeModel(model);
  }
}

Board.java (Plank-generated)

public class Board implements MergeableModel<Board> {
  @Override
  public Board mergeFrom(board) { ... }

  @Override
  public String getId() { ... }
}

In terms of this being a "global configuration", are you referring to it applying to other languages? I had implemented it as a Java-specific config parameter (--java_extends and --java_implements) as I wasn't confident it would translate easily to other languages.

@RicoYao RicoYao force-pushed the java_extends_implements_master branch from d593f94 to 7e0c742 Compare May 16, 2019 17:24
@RicoYao
Copy link
Contributor Author

RicoYao commented Jun 6, 2019

Replaced by #217

@RicoYao RicoYao closed this Jun 6, 2019
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

Successfully merging this pull request may close these issues.

2 participants