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

[AERIE-1858] Facilitate recommended pattern for computed attributes #188

Merged

Conversation

mattdailis
Copy link
Collaborator

@mattdailis mattdailis commented May 26, 2022

  • Tickets addressed: AERIE-1858
  • Review: By commit
  • Merge strategy: Merge (no squash)

Description

The goal of this PR is to demonstrate a recommended pattern for declaring computed attributes. We think that the most idiomatic way to declare computed attributes is with a record, like so:

@EffectModel
public ComputedAttributes run(final Mission mission) {
final var bigBiteSize = biteSize > 1.0;
final var newFlag = bigBiteSize ? Flag.B : Flag.A;
mission.flag.set(newFlag);
mission.fruit.subtract(biteSize);
return new ComputedAttributes(bigBiteSize, newFlag);
}
@AutoValueMapper
public record ComputedAttributes(boolean biteSizeWasBig, Flag newFlag) {}

Records are a low-boilerplate mechanism for expressing sets of key-value pairs, where the keys are fixed (as opposed to Maps, which describe sets key-value pairs where the keys are dynamic).

Prior to this PR, it is still definitely possible to use records for computed attributes. However, every new record type would need a corresponding ValueMapper to be added to the value mapper factory.

In the interest of making our "recommended practice" as easy as possible for users, this PR adds an @AutoValueMapper annotation. This annotation can only be applied to records, and it will generate an AutoValueMappers.java file that contains a method that looks like this:

@Generated("gov.nasa.jpl.aerie.merlin.processor.MissionModelProcessor")
public final class AutoValueMappers {
  public static ValueMapper<BiteBananaActivity.ComputedAttributes> gov_nasa_jpl_aerie_banananation_activities_BiteBananaActivity_ComputedAttributes(
      final ValueMapper<Boolean> biteSizeWasBigValueMapper,
      final ValueMapper<Flag> newFlagValueMapper) {
    return new RecordValueMapper<>(
      gov.nasa.jpl.aerie.banananation.activities.BiteBananaActivity.ComputedAttributes.class,
      List.of(
        new RecordValueMapper.Component<>(
          "biteSizeWasBig",
          gov.nasa.jpl.aerie.banananation.activities.BiteBananaActivity.ComputedAttributes::biteSizeWasBig,
        biteSizeWasBigValueMapper),
        new RecordValueMapper.Component<>(
          "newFlag",
          gov.nasa.jpl.aerie.banananation.activities.BiteBananaActivity.ComputedAttributes::newFlag,
        newFlagValueMapper)
    ));
  }
}

It will simultaneously create a TypeRule that would have been the result of parsing that factory method, had that method been defined prior to annotation processing.

Verification

I have added unit tests for RecordValueMapper. I've also done some visual spot checking. What I would still like to do prior to merging this PR is some end-to-end tests, just as a sanity check that the serialization/deserialization doesn't hit any snags

  • manual e2e test where AutoValueMapper is used for computed attributes
Value Schema
{
    "type": "struct",
    "items": {
        "newFlag": {
            "type": "variant",
            "variants": [
                {
                    "key": "A",
                    "label": "A"
                },
                {
                    "key": "B",
                    "label": "B"
                }
            ]
        },
        "biteSizeWasBig": {
            "type": "boolean"
        }
    }
}

- [ ] manual e2e test where AutoValueMapper is used as a parameter

Documentation

  • The existing documentation already recommends using a Map. We should just update it to recommend a record instead.

  • Perhaps an extra section on @AutoValueMapper should be added to our ValueMapper docs.

Future work

  • Test in the UI
  • Some of this AutoValueMapper stuff may be reworked if/when we refactor the annotation processing code

@Twisol
Copy link
Contributor

Twisol commented Jun 4, 2022

@mattdailis, I haven't had a chance to look too deeply at this yet, but I have a quick question. If a user opts not to use @AutoValueMapper, they can still provide their own custom mapper, correct? I think they would just add the value mapper to a family in one of the package's @MissionModel.WithMappers annotations, right?

@mattdailis mattdailis force-pushed the feature/AERIE-1858--demonstrate-computed-attributes branch 2 times, most recently from 396ff16 to b424c9b Compare June 23, 2022 13:30
@mattdailis mattdailis force-pushed the feature/AERIE-1858--demonstrate-computed-attributes branch 2 times, most recently from 9faed61 to 353fce5 Compare July 26, 2022 15:56
@mattdailis mattdailis force-pushed the feature/AERIE-1858--demonstrate-computed-attributes branch from 353fce5 to 8814983 Compare July 26, 2022 21:46
@pcrosemurgy
Copy link
Contributor

@mattdailis Sorry if I'm just out of the loop but what's the state of this PR? Is it in kind of a do-not-merge place right now?

@mattdailis
Copy link
Collaborator Author

@mattdailis Sorry if I'm just out of the loop but what's the state of this PR? Is it in kind of a do-not-merge place right now?

It's available for review! I probably need to rebase/resolve merge conflicts, I can do that tomorrow morning 👍

@pcrosemurgy pcrosemurgy removed their assignment Sep 20, 2022
@pcrosemurgy
Copy link
Contributor

@mattdailis let me know if you want/need to pair at all to get this PR through 👌 The conflicts with develop are likely from changes I've pushed through since this PR was opened.

@mattdailis mattdailis temporarily deployed to e2e-test October 1, 2022 01:34 Inactive
@mattdailis mattdailis force-pushed the feature/AERIE-1858--demonstrate-computed-attributes branch from 36e43ab to c420b0a Compare October 4, 2022 14:58
@mattdailis mattdailis temporarily deployed to e2e-test October 4, 2022 14:58 Inactive
Copy link
Contributor

@pcrosemurgy pcrosemurgy left a comment

Choose a reason for hiding this comment

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

Great work Matt!

Last question/comment I have is about the @AutoValueMapper. I'm wondering if this should be called RecordValueMapper or something that indicates it's only supported for computed attribute return types.

Maybe the thinking is that this annotation will eventually support auto value mappers for other parts of mission model? If not then I could see it causing some confusion if developers think it's truly an auto value mapper (without context).

@Twisol
Copy link
Contributor

Twisol commented Oct 4, 2022

Maybe the thinking is that this annotation will eventually support auto value mappers for other parts of mission model? If not then I could see it causing some confusion if developers think it's truly an auto value mapper (without context).

I can definitely see this being extended to activity types in the future -- I'm imagining this coming in when we extract resource registration out of model initialization. At that point we should probably move activity type registration into the same place that resource registration ends up, which would mean @ActivityType's behavior will reduce almost entirely to @AutoValueMapper's behavior.

@mattdailis mattdailis force-pushed the feature/AERIE-1858--demonstrate-computed-attributes branch from c420b0a to 6d06027 Compare October 6, 2022 23:15
@mattdailis mattdailis temporarily deployed to e2e-test October 6, 2022 23:16 Inactive
@mattdailis mattdailis requested review from adrienmaillard and removed request for camargo October 7, 2022 15:30
Copy link
Contributor

@adrienmaillard adrienmaillard left a comment

Choose a reason for hiding this comment

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

It's all great. Thank you for the tour, it definitely helped. I only have two minor non-blocking comments.

@mattdailis mattdailis force-pushed the feature/AERIE-1858--demonstrate-computed-attributes branch from 6d06027 to 2768d3f Compare October 11, 2022 14:53
@mattdailis mattdailis temporarily deployed to e2e-test October 11, 2022 14:53 Inactive
@mattdailis mattdailis temporarily deployed to e2e-test October 11, 2022 14:53 Inactive
@mattdailis mattdailis force-pushed the feature/AERIE-1858--demonstrate-computed-attributes branch from e971a57 to a0ec898 Compare October 11, 2022 14:57
@mattdailis mattdailis temporarily deployed to e2e-test October 11, 2022 14:57 Inactive
@mattdailis mattdailis force-pushed the feature/AERIE-1858--demonstrate-computed-attributes branch from a0ec898 to a86e100 Compare October 12, 2022 16:28
@mattdailis mattdailis temporarily deployed to e2e-test October 12, 2022 16:28 Inactive
@mattdailis mattdailis force-pushed the feature/AERIE-1858--demonstrate-computed-attributes branch from a86e100 to 0bedf45 Compare October 13, 2022 13:50
@mattdailis mattdailis temporarily deployed to e2e-test October 13, 2022 13:50 Inactive
@mattdailis mattdailis merged commit 252e70e into develop Oct 13, 2022
@mattdailis mattdailis deleted the feature/AERIE-1858--demonstrate-computed-attributes branch October 13, 2022 14:06
@mattdailis mattdailis mentioned this pull request Apr 17, 2023
1 task
@mattdailis mattdailis mentioned this pull request Oct 31, 2023
3 tasks
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.

None yet

4 participants