microsoft / microsoft/typespec

Mutators for data model transformation of all following emitters (composable emitters)

Open
#7,903 0 comments 0 reactions 0 assignees View on GitHub
compiler:core design:needed triaged:core
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

### Clear and concise description of the problem

Hi!

I have a use case for running a mutation on all models to transform them to a different data model. Within our company we are using a standard data model which has multiple versions. These data models are created in a backwards compatible way, which includes a mapping configuration between the version of the data model.

So you can imagine a mapping configuration like the following:

```
[{
type: 'attribute' | 'entity';
v1: string;
v2: string;
v3: string;
}]
```

Where each version represents the name of the field in that version of the data model.

What i want to achieve is that we can write our contracts in TypeSpec once (in the latest version of the data model), and transform the internal source types of the TypeSpec program to the previous versions.

I noticed that I can hack my way around this by writing an emitter which uses the `unsafe_mutateSubgraph` and modify the original types instead of the cloned types. As long as I ensure this emitter **runs first** it applies to all the following emitters. However from the internal documentation I noticed that mutating the original types in a mutator is not recommended as this would also impact other emitters, however that is exactly the intended behavior in this scenario.

I was wondering if this could be (or already is) provided from the emitter framework? I think a feature to support this use case can enable powerful composability of emitters, allowing internal data models to be transformed before "piping" them to the next emitter.

### Checklist

- [x] Follow our [Code of Conduct](https://github.com/microsoft/typespec/blob/main/CODE_OF_CONDUCT.md)
- [x] Read the [docs](https://typespec.io/docs/).
- [x] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.