-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: properly implement constants in c# (#1801)
- Loading branch information
Showing
12 changed files
with
231 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,53 @@ | ||
import { | ||
ConstrainedEnumModel, | ||
ConstrainedMetaModel, | ||
ConstrainedMetaModelOptionsConst, | ||
ConstrainedReferenceModel, | ||
ConstrainedStringModel | ||
} from '../../../models'; | ||
import { CSharpConstantConstraint } from '../CSharpGenerator'; | ||
|
||
const getConstrainedEnumModelConstant = (args: { | ||
constrainedMetaModel: ConstrainedMetaModel; | ||
constrainedEnumModel: ConstrainedEnumModel; | ||
constOptions: ConstrainedMetaModelOptionsConst; | ||
}) => { | ||
const constrainedEnumValueModel = args.constrainedEnumModel.values.find( | ||
(value) => value.originalInput === args.constOptions.originalInput | ||
); | ||
|
||
if (constrainedEnumValueModel) { | ||
return `${args.constrainedMetaModel.type}.${constrainedEnumValueModel.key}`; | ||
} | ||
}; | ||
|
||
export function defaultConstantConstraints(): CSharpConstantConstraint { | ||
return () => { | ||
return ({ constrainedMetaModel }) => { | ||
const constOptions = constrainedMetaModel.options.const; | ||
|
||
if (!constOptions) { | ||
return undefined; | ||
} | ||
|
||
if ( | ||
constrainedMetaModel instanceof ConstrainedReferenceModel && | ||
constrainedMetaModel.ref instanceof ConstrainedEnumModel | ||
) { | ||
return getConstrainedEnumModelConstant({ | ||
constrainedMetaModel, | ||
constrainedEnumModel: constrainedMetaModel.ref, | ||
constOptions | ||
}); | ||
} else if (constrainedMetaModel instanceof ConstrainedEnumModel) { | ||
return getConstrainedEnumModelConstant({ | ||
constrainedMetaModel, | ||
constrainedEnumModel: constrainedMetaModel, | ||
constOptions | ||
}); | ||
} else if (constrainedMetaModel instanceof ConstrainedStringModel) { | ||
return `"${constOptions.originalInput}"`; | ||
} | ||
|
||
return undefined; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.