smithy-lang/smithy

Using `@idRef`s as map shape keys

milesziemer opened this issue · 1 comments

When the @idRef trait is used on a map key, you have to use the fully qualified shape id within quotes:

@trait
map withIdRefOnKey {
    @idRef
    key: String
    value: String
}

@withIdRefOnKey({
    "com.foo#Ref": "Bar"
})
structure Struct {}

structure Ref {}

instead of:

@trait
map withIdRefOnKey {
    @idRef
    key: String
    value: String
}

@withIdRefOnKey({
    Ref: "Bar" // [ERROR] Invalid shape ID: Ref
})
structure Struct {}

structure Ref {}

which you can do for any other case (see docs examples).

This is because syntactically NodeObjectKey can't be a shape id, whereas NodeStringValue is interpreted as a shape id if it isn't surrounded by "", with the parser filling in the namespace.

I don't think it is possible to change NodeObjectKey to allow shape ids the same way NodeStringValue does because of the ambiguity it would create (how would we know if the key is just an Identifier, or a ShapeId?), plus I'm not sure we'd want that anyways. But is is a rough edge with using idRef.

This was a tradeoff between allowing the syntactic shape id in map keys vs not having to quote map keys. Closing this now, but if in the future we have some demand for making this better somehow, we can revisit it.