microsoft / typespec

https://typespec.io/
MIT License
4.47k stars 210 forks source link

Ability to decorator model expression #4927

Open MaryGao opened 21 hours ago

MaryGao commented 21 hours ago

Currently we can add decorators on model declarations but can't decorate on model expressions which would be useful to add decorators for inline/anonymous models.

playground

model Foo {
  prop: {
    sub: string;
  };
  props: {
    sub: string;
  }[];
  propDict: Record<{
    sub: string;
  }>;
}

// decorate named model
@@doc(Foo, "document for Foo");

// decorate inline model
@@doc(Foo.prop::type, "document for Foo");

// decorate model expression including array
@@doc(Foo.props::type::indexerValue, "document for props");

@@doc(Foo.propDict::type::indexerValue, "document for propDict");
timotheeguerin commented 12 hours ago

For context this is not something that is fixed by the symbol refactor as its by design right now that model expression cannot be decoratored. I have a follow up PR to the symbol that makes it an error so its not silently ignored https://github.com/microsoft/typespec/pull/4926. There is no technical limitation to supporting this(we are just not calling checkDecorators in the checker by design for model expression)

The question here is should we allow model expression to be decorated and if so what is the syntax to do it inline. As we don't really want augment decorator to be the only way to decorate something.

model Foo {
  prop: @doc("Doc for this model Exp") {
    sub: string;
  };

could just have something like that, though it might be ambiguous/need more wrapping for array cases

model Foo {
  props: (@doc("Doc for this model Exp") {
    sub: string;
  })[];
  propDict: Record<@doc("Doc for this model Exp") {
    sub: string;
  }>;
}

there is also the maybe confusion on why you cannot do that

model Foo {
  prop: @doc("Doc for this") string;
  array: (@doc("Doc for this") string)[];
}