-
-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add additionalProperties preset hooks #71
chore: add additionalProperties preset hooks #71
Conversation
@magicmatatjahu how should we handle |
…es_rendering # Conflicts: # test/generators/typescript/TypeScriptGenerator.spec.ts
@magicmatatjahu I have fully added the typescript rendering can you check it out in regards to presets and the setup there? Also the class encapsulation for the additionProperties should they have presets? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good, but I have an another idea how to use additionalProperties
:) It's a more generic.
@@ -77,6 +77,11 @@ ${lines.map(line => ` * ${line}`).join('\n')} | |||
const rendererProperty = await this.runPropertyPreset(propertyName, property, this.model); | |||
content.push(rendererProperty); | |||
} | |||
|
|||
if (this.model.additionalProperties !== undefined && this.model.additionalProperties instanceof CommonModel) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (this.model.additionalProperties !== undefined && this.model.additionalProperties instanceof CommonModel) { | |
if (this.model.additionalProperties instanceof CommonModel) { |
@@ -52,6 +52,12 @@ ${this.indent(this.renderBlock(content, 2))} | |||
content.push(this.renderBlock([getter, setter])); | |||
} | |||
|
|||
if (this.model.additionalProperties !== undefined && this.model.additionalProperties instanceof CommonModel) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (this.model.additionalProperties !== undefined && this.model.additionalProperties instanceof CommonModel) { | |
if (this.model.additionalProperties instanceof CommonModel) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like in TS -> to additionalContent
preset
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean by this? I don't use additionalContent
in TypeScript
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean that this part should be moved to additionalContent
preset like I suggest in another comment for TS :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you mean this for JS - #71 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, sorry 😄
src/models/Preset.ts
Outdated
export interface AdditionalPropertiesArgs { | ||
parentModel: CommonModel; | ||
additionalProperties: CommonModel; | ||
} | ||
export interface ClassPreset<R extends AbstractRenderer, O extends object = any> extends CommonPreset<R, O> { | ||
ctor?: (args: PresetArgs<R, O>) => Promise<string> | string; | ||
property?: (args: PresetArgs<R, O> & PropertyArgs) => Promise<string> | string; | ||
additionalProperties?: (args: PresetArgs<R, O> & AdditionalPropertiesArgs) => Promise<string> | string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
additionalProperties
should have only PresetArgs
arguments, why? parentModel
is not needed - it's my fault and in next PR I will remove it - we have model
property for that (it is the same 😅). Also additionalProperties
field is hardcoded for additionalProperties
inferred from model
so you have also possibility to use it by model
. additionalProperties
preset should have also possibility to render patternProperties
etc, so hardcoded for only one field is very bad idea.
additionalProperties?: (args: PresetArgs<R, O>) => Promise<string> | string;
@@ -77,6 +77,11 @@ ${lines.map(line => ` * ${line}`).join('\n')} | |||
const rendererProperty = await this.runPropertyPreset(propertyName, property, this.model); | |||
content.push(rendererProperty); | |||
} | |||
|
|||
if (this.model.additionalProperties !== undefined && this.model.additionalProperties instanceof CommonModel) { | |||
const additionalProperty = await this.runAdditionalPropertiesPreset(this.model.additionalProperties, this.model); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
additionalProperties
preset should also render another properties like patternProperties - also passed by options to preset - so this part should look like:
// from this
if (this.model.additionalProperties !== undefined && this.model.additionalProperties instanceof CommonModel) {
const additionalProperty = await this.runAdditionalPropertiesPreset(this.model.additionalProperties, this.model);
content.push(additionalProperty);
}
// to
const additionalProperties = await this.runAdditionalPropertiesPreset();
if (additionalProperties) {
content.push(additionalProperties);
}
we shouldn't hardcoded only for additionalProperties
from model.
if (this.model.additionalProperties !== undefined && this.model.additionalProperties instanceof CommonModel) { | ||
const getter = 'getAdditionalProperty(key){ return _additionalProperties[key]; }'; | ||
const setter = 'setAdditionalProperty(key, value) { _additionalProperties[key] = value; }'; | ||
content.push(this.renderBlock([getter, setter])); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be rendered inside additionalContent
preset:
{
class: {
additionalContent({ renderer, model, content }) {
if (!(this.model.additionalProperties instanceof CommonModel)) return content;
const getter = 'getAdditionalProperty(key){ return _additionalProperties[key]; }';
const setter = 'setAdditionalProperty(key, value) { _additionalProperties[key] = value; }';
this.renderBlock([content, getter, setter]);
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you mean the extra functions that are not really encapsulations right? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, for that additionalContent
exists - for extra function. Encapsulation is a problem, but you can create separate presets to make your encapsulation
new TypeScriptGenerator({ presets: [ADDITIONALPROPETIES, PATTERN_PROPERTES] })
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can make separate presets for everything, argument, type signature etc. but then it would be hard to use.
@@ -36,6 +36,11 @@ ${content} | |||
content.push(rendererProperty); | |||
} | |||
|
|||
if (this.model.additionalProperties !== undefined && this.model.additionalProperties instanceof CommonModel) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add it like in JS -> to additionalContent
preset
Co-authored-by: Maciej Urbańczyk <urbanczyk.maciej.95@gmail.com>
Co-authored-by: Maciej Urbańczyk <urbanczyk.maciej.95@gmail.com>
…onalProperties_rendering # Conflicts: # src/generators/typescript/TypeScriptRenderer.ts # src/generators/typescript/renderers/ClassRenderer.ts # test/generators/typescript/TypeScriptGenerator.spec.ts
…es_rendering # Conflicts: # src/generators/typescript/TypeScriptGenerator.ts
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
Gonna provide a clean implementation here, gonna use most of the comments already given to solve it. Thanks for the review @magicmatatjahu 🙇 |
Description
This PR adds additionalProperties preset hooks.
Related issue(s)
solves #68