microsoft / microsoft/typespec
Add "combine" or "merge" operator for models
- Dominant language
- Java
- Stars
- 5.9k
- Forks
- 394
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 104
Description
Something that I have missed is the ability to combine models outside of spread operators. One example of a (unrelated) language is [jsonnet's](https://jsonnet.org/ref/language.html) object combine operator (see inheritance and nested field inheritance the sections).
To illustrate a bit what I'm thinking:
```cadl
model Base {
id: string;
etag?: string;
apiVersion: string;
}
model Overrides {
id: int32;
description?: string;
apiVersion: never;
}
model Combined is Base + Overrides;
/*
// Resulting model:
model Combined {
id: int32;
etag?: string;
apiVersion: never;
description?: string;
}
*/
```
As indicated, the resulting model is not a subtype of either operands, the order of attributes could be preserved (although there is an argument to be made that overriding attributes should move later), decorators (not shown) are not merged etc.
I can think of a couple of places in azure core where this would be useful and simplify our life.
[Playground Link](https://cadlplayground.z22.web.core.windows.net/?c=bW9kZWwgQmFzZSB7CiAgaWQ6IHN0cmluZzsKICBldGFnP8wRYXBpVmVyc2lvbsoWfQoKxkVPdmVycmlkZXPJSmludDMyxDhkZXNjcmlwdGlvbtlQbmV2ZXLLT0NvbWJpbmVkIGlz5gCgK8piOwoKLyoKLy8gUmVzdWx0aW5nIMU1OtE98QCL%2FQDU5wCE%2BACxfQoqLwoK)
Contributor guide
Assessment
This issue has not been assessed yet.