smithy icon indicating copy to clipboard operation
smithy copied to clipboard

Add trait code generation

Open mtdowling opened this issue 5 years ago • 2 comments

The code used to represent typed values for traits are ripe for code generation-- they're mostly the same thing and formulaic depending on the type of trait.

  • Example of Annotation trait: https://github.com/awslabs/smithy/blob/master/smithy-model/src/main/java/software/amazon/smithy/model/traits/HttpLabelTrait.java
  • Example of String trait: https://github.com/awslabs/smithy/blob/master/smithy-model/src/main/java/software/amazon/smithy/model/traits/OutputEventStreamTrait.java

I pushed up a proof of concept branch to kick start the idea. It needs work and has lots of todos. I probably wont have the cycles to finish this for the next few weeks, so anyone is free to take it over.

https://github.com/awslabs/smithy/tree/smithy-codegen-traits/codegen/smithy-codegen-traits/src/main/java/software/amazon/smithy/codegen/traits

Notes and considerations:

  • While it would be nice to generate the traits in the smithy-model package, it might not be worth it at this point since it would require pulling a ton of code out into a lower-level package that doesn't depend on code generation so that the smithy-model package could.
  • It would be nice if this could be an exercise to prove out the design of CodeWriter, but if that proves insufficient, feel free to pull in a dependency on JavaPoet.
  • Traits that target structure shapes should inline the members of the targeted structure into the trait itself rather than require a structure value to be passed into the code generated trait.

mtdowling avatar Jul 15 '19 02:07 mtdowling

Would it fit better to create a meta-trait that could connect a trait to the structure declaration that defines its value shape? These implicit shapes would become explicit, and this problem would be mostly solved by existing code generation logic. There's probably some user understanding benefit here, and some deeper dogfooding of the IDL.

robin-aws avatar Feb 04 '21 22:02 robin-aws

I think we basically did what you're suggesting. You define traits in the model by adding the trait trait to a shape. I think the issue description here may have been written before we made that change, so the last bullet point doesn't make much sense anymore.

mtdowling avatar Feb 04 '21 22:02 mtdowling