-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
refactor: add splitter #676
Merged
jonaslagoni
merged 88 commits into
asyncapi:next
from
jonaslagoni:feature/splitter_setup
Mar 9, 2022
+133
−0
Merged
Changes from all commits
Commits
Show all changes
88 commits
Select commit
Hold shift + click to select a range
e7fdf46
docs: add quadrrem as a contributor for code, test
allcontributors[bot] 593a6e8
ci: update global workflows
asyncapi-bot cf8c922
docs: generate java data models
mahakporwal02 d659d80
fix: model dependencies are not accurately resolved
jonaslagoni 7b6010e
fix: wrong property name in JavaCommonPresetOptions interface (#544)
kamko 3d1208a
chore(release): v0.39.10
asyncapi-bot b4770ea
docs: add mahakporwal02 as a contributor for example, test, doc (#549)
allcontributors[bot] b85e11e
docs: add kamko as a contributor for test, bug, code (#552)
allcontributors[bot] 2b48a05
docs: add OpenAPI input as example
jonaslagoni f067637
fix: correctly store already converted schemas while parsing AsyncApi…
kamko 65efaea
chore(release): v0.39.11
asyncapi-bot 99b5b44
feat: complete model rendering and file output for JavaScript generator
jonaslagoni 4cf252c
chore(release): v0.40.0 (#556)
asyncapi-bot 17f8f7a
fix: removing TypeScript exported base model
jonaslagoni 120181d
chore(release): v0.40.1
asyncapi-bot ef4c0b7
fix: special cases in enum values for C# generator
ron-debajyoti 8d9b7ff
docs: add ron-debajyoti as a contributor for code, test
allcontributors[bot] a897e07
chore(release): v0.40.2
asyncapi-bot 11cc2c1
feat: add file output for C# generator (#537)
jonaslagoni d4ed0e3
chore(release): v0.41.0
asyncapi-bot 5cbe362
feat: add file output for TypeScript generator (#536)
jonaslagoni 623dcb2
chore(release): v0.42.0
asyncapi-bot 7b5966e
ci: update global workflows
asyncapi-bot 49a534c
docs: generate go data model (#568)
mahakporwal02 dc68646
chore: fixing sonarcloud duplication problems (#570)
jonaslagoni cb81099
fix: model dependencies not needed for C# generator
jonaslagoni b203752
chore(release): v0.42.1 (#571)
asyncapi-bot d84b8b5
fix: non-required properties are required by constructor (#553)
mahakporwal02 e1fb4d7
chore(release): v0.42.2 (#572)
asyncapi-bot 07bcb3f
docs: example on how to include a custom function in the data model
mahakporwal02 dd3163c
docs: add mahakporwal02 as a contributor for code
allcontributors[bot] cafc1ff
feat: complete model rendering and file output for Go generator (#540)
jonaslagoni b06c242
docs: example of csharp example serializer
ron-debajyoti acaea00
chore(release): v0.43.0 (#574)
asyncapi-bot 1dc565e
docs: add ron-debajyoti as a contributor for doc, example (#576)
allcontributors[bot] e22c136
docs: generate C# models (#578)
ron-debajyoti 928daa0
docs: generate Java data models that overwrite equal method (#577)
mahakporwal02 d9266db
docs: generate Java data models that overwrite hashCode method (#580)
mahakporwal02 17273c1
docs: model to change collection types for Java models (#579)
ron-debajyoti 2a058d6
ci: update global workflows
asyncapi-bot 51ae271
ci: update global workflows (#584)
asyncapi-bot ef07448
ci: update global workflows (#585)
asyncapi-bot ffc0cd8
docs: add missing documentation for indentation example (#541)
jonaslagoni 1c684c5
chore: added nvmrc (#582)
ritik307 18564f8
docs: add ritik307 as a contributor for doc (#588)
allcontributors[bot] 8ff4bb9
docs: linked to advance language usage (#589)
mahakporwal02 5622923
feat: assign optional arguments in the constructor for JS generator (…
mahakporwal02 5dc200e
chore(release): v0.44.0 (#590)
asyncapi-bot 88cf92d
ci: fix coverage problems with checkout (#592)
jonaslagoni 57948b5
ci: update global workflows (#593)
asyncapi-bot b2683cd
feat: un/marshal helper function for JS (#591)
Samridhi-98 9341e33
chore(release): v0.45.0 (#600)
asyncapi-bot 017d07b
chore(deps): bump node-fetch from 2.6.5 to 2.6.7 (#602)
dependabot[bot] 810d607
ci: update global workflows (#603)
asyncapi-bot 079db79
docs: added jackson annotation example (#601)
Samridhi-98 bc2f3ef
feat: support AsyncAPI v2.3.0 (#563)
jonaslagoni 71ac65a
chore(release): v0.46.0 (#606)
asyncapi-bot 51062c1
fix: missing namespace for Type in C# JsonSerializerPreset (#597)
jonaslagoni 7b2a33f
chore(release): v0.46.1 (#611)
asyncapi-bot 8818321
ci: update global workflows (#617)
asyncapi-bot 388495e
feat: exclude underscore from TS key enum rendering (#613)
Ishan-Saini b819c6f
chore(release): v0.47.0 (#618)
asyncapi-bot e3aeeb1
ci: update global workflows (#620)
asyncapi-bot b82a78b
docs: add Ishan-Saini as a contributor for code, test (#619)
allcontributors[bot] 8a79fc0
ci: update global workflows (#631)
asyncapi-bot e943a71
docs: added dependency info for C# serializer preset (#630)
Samridhi-98 b3c10bf
ci: only run workflows when necessary (#624)
jonaslagoni afe46b1
feat: add un/marshal JSON preset for Java (#596)
ritik307 9f596f8
docs: add ritik307 as a contributor for code, test, example (#632)
allcontributors[bot] 594e7f1
chore(release): v0.48.0 (#633)
asyncapi-bot 5567586
docs: add javascript marshaling example (#635)
Samridhi-98 c9980fc
feat: add generate example preset for JS (#629)
Samridhi-98 c773f4c
feat: new `accessor` preset hook for C# class renderer (#626)
jonaslagoni 0ff2e89
chore(release): v0.49.0 (#650)
asyncapi-bot 9e4add4
docs: add Samridhi-98 as a contributor for code, test, doc, example (…
allcontributors[bot] 10aca3b
docs: add java un/marshalling reference to docs (#645)
ritik307 b0a41ca
Added models
jonaslagoni e4abbe6
Merge branch 'next' into feature/introduce_models
jonaslagoni ca0a1b2
fixed lint errors
jonaslagoni 6bc51a9
Switched to inline modifiers for constructor
jonaslagoni 842aad3
Changed naming of models
jonaslagoni 2c2e3ca
Added export of models
jonaslagoni aeec94e
Removed unnecessary redefining of properties
jonaslagoni 29f9337
Merge remote-tracking branch 'origin/feature/introduce_models' into f…
jonaslagoni d1056f9
Added splitter
jonaslagoni 6cbf37b
Added test
jonaslagoni ed3fe21
Merge branch 'next' into feature/splitter_setup
jonaslagoni 9921e90
Fixed lint
jonaslagoni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { MetaModel, ReferenceModel, EnumModel, UnionModel, ArrayModel, TupleModel, StringModel, IntegerModel, FloatModel, BooleanModel, ObjectModel, DictionaryModel} from '../models'; | ||
|
||
export type SplitOptions = { | ||
splitEnum?: boolean, | ||
splitUnion?: boolean, | ||
splitArray?: boolean, | ||
splitTuple?: boolean, | ||
splitString?: boolean, | ||
splitInteger?: boolean, | ||
splitFloat?: boolean, | ||
splitBoolean?: boolean, | ||
splitObject?: boolean, | ||
splitDictionary?: boolean | ||
} | ||
|
||
/** | ||
* Try split the model | ||
* @param model | ||
* @param options | ||
* @param models | ||
* @returns whether the new or old MetaModel to use. | ||
*/ | ||
// eslint-disable-next-line sonarjs/cognitive-complexity | ||
const trySplitModel = (model: MetaModel, options: SplitOptions, models: MetaModel[]): MetaModel => { | ||
const shouldSplit = options.splitEnum === true && model instanceof EnumModel || | ||
options.splitUnion === true && model instanceof UnionModel || | ||
options.splitArray === true && model instanceof ArrayModel || | ||
options.splitTuple === true && model instanceof TupleModel || | ||
options.splitString === true && model instanceof StringModel || | ||
options.splitInteger === true && model instanceof IntegerModel || | ||
options.splitFloat === true && model instanceof FloatModel || | ||
options.splitBoolean === true && model instanceof BooleanModel || | ||
options.splitObject === true && model instanceof ObjectModel || | ||
options.splitDictionary === true && model instanceof DictionaryModel; | ||
|
||
if (shouldSplit) { | ||
models.push(model); | ||
return new ReferenceModel(model.name, model.originalInput, model); | ||
} | ||
return model; | ||
}; | ||
|
||
/** | ||
* Overwrite the nested models with references where required. | ||
* | ||
* @param model | ||
* @param options | ||
* @param models | ||
* @returns an array of all the split models | ||
*/ | ||
export const split = (model: MetaModel, options: SplitOptions, models: MetaModel[] = [model]): MetaModel[] => { | ||
if (model instanceof ObjectModel) { | ||
for (const [prop, propModel] of Object.entries(model.properties)) { | ||
model.properties[String(prop)] = trySplitModel(propModel, options, models); | ||
split(propModel, options, models); | ||
} | ||
} else if (model instanceof UnionModel) { | ||
for (let index = 0; index < model.union.length; index++) { | ||
const unionModel = model.union[Number(index)]; | ||
model.union[Number(index)] = trySplitModel(unionModel, options, models); | ||
split(unionModel, options, models); | ||
} | ||
} else if (model instanceof ArrayModel) { | ||
const valueModel = model.valueModel; | ||
model.valueModel = trySplitModel(valueModel, options, models); | ||
split(valueModel, options, models); | ||
} else if (model instanceof TupleModel) { | ||
for (const tuple of model.tuple) { | ||
const tupleModel = tuple.value; | ||
tuple.value = trySplitModel(tupleModel, options, models); | ||
split(tupleModel, options, models); | ||
} | ||
} else if (model instanceof DictionaryModel) { | ||
const keyModel = model.key; | ||
const valueModel = model.value; | ||
model.key = trySplitModel(keyModel, options, models); | ||
model.value = trySplitModel(valueModel, options, models); | ||
split(keyModel, options, models); | ||
split(valueModel, options, models); | ||
} | ||
return models; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { split, SplitOptions } from '../../src/helpers'; | ||
import { ObjectModel, ReferenceModel, StringModel } from '../../src/models'; | ||
describe('Splitter', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test('should split models when asked for it', () => { | ||
const stringModel = new StringModel('testString', undefined); | ||
const model = new ObjectModel('testObj', undefined, { | ||
test: stringModel | ||
}); | ||
const options: SplitOptions = { | ||
splitString: true | ||
}; | ||
const splittedModels = split(model, options); | ||
|
||
const expectedObjectModel = model; | ||
expectedObjectModel.properties['test'] = new ReferenceModel(stringModel.name, stringModel.originalInput, stringModel); | ||
|
||
expect(splittedModels.length).toEqual(2); | ||
expect(splittedModels[0] instanceof ObjectModel).toEqual(true); | ||
expect(splittedModels[0]).toEqual(expectedObjectModel); | ||
expect(splittedModels[1] instanceof StringModel).toEqual(true); | ||
}); | ||
test('should not split models when not asked for', () => { | ||
const stringModel = new StringModel('testString', undefined); | ||
const model = new ObjectModel('testObj', undefined, { | ||
test: stringModel | ||
}); | ||
const options: SplitOptions = { | ||
splitString: false | ||
}; | ||
const splittedModels = split(model, options); | ||
expect(splittedModels.length).toEqual(1); | ||
|
||
expect(splittedModels[0]).toEqual(model); | ||
}); | ||
test('should not split models when asked for something else', () => { | ||
const stringModel = new StringModel('testString', undefined); | ||
const model = new ObjectModel('testObj', undefined, { | ||
test: stringModel | ||
}); | ||
const options: SplitOptions = { | ||
splitBoolean: true | ||
}; | ||
const splittedModels = split(model, options); | ||
expect(splittedModels.length).toEqual(1); | ||
expect(splittedModels[0]).toEqual(model); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You can create helper for that:
and then in code:
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.
True 😆
I think I like the explicit current style though 🤔
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 always change it later if we agree otherwise.