Direct access to structure fields
It would be "cool" (and probably useful in a bunch of ways) to have access to a particular Field instance, knowing the member name.
For example:
structure Hello {
@required foo: String = "woop"
}
println(Hello.fields.foo.getDefaultValue: Option[String])
This would sort of mirror how you can already access the individual alts of a generated union shape.
We need this feature in our project to avoid duplicating default values in the code base having a smithy with default values
@Baccata, thoughts on having this? POC in #1683.
Can either of you elaborate with a real usecase behind this ask, as in one that can't be easily solved via schema visitors, etc ? Like, why do you need the default value out of the context of serialisation, for instance ?
For example, we have content with a default maxSongs value defined for a page. This value is also included as a parameter in our domain events, and the UI app allows for increasing/decreasing this value. When we generate the event as a producer, we need to know how many items to calculate if the user hasn’t overridden the default.
In this case, our producer application would need to hardcode the default maxSongs value in its own codebase, rather than relying on the latest Smithy contract and its constraints. This duplication introduces potential risk and inconsistency
@meldmy maybe this is a bit of an XY problem - the root of what you're trying to do is having a way to use the default value of the parameter, which is already present in the code, and maybe even has a default value in the constructor:
structure Foo {
@required requiredParam: String
@required requiredWithDefault: String = "welp"
}
case class Foo(requiredParam: String, requiredWithDefault: String = "welp")
Would it be good enough for you to have an additional constructor that you can call, which will take None as "use the default if present"? In other words, what if we generated these additionally:
//object Foo {
def applyWithDefaults(requiredParam: String, requiredWithDefault: Option[String] = None): Foo =
Foo(requiredParam, requiredWithDefault.getOrElse("welp"))
Then, when you want to use the default value you'd pass None, and otherwise you'd pass whatever value you intended to pass.