-
Notifications
You must be signed in to change notification settings - Fork 108
Code generator scripts refactor; Add credential, role, and schedule construct schemas with Go models #629
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
Merged
Merged
Code generator scripts refactor; Add credential, role, and schedule construct schemas with Go models #629
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
57995df
Add credential, role, schedule, and capability construct schemas with…
marblom007 965fd1d
Address PR review feedback
marblom007 06a8abf
Merge branch 'master' into add-credential-role-schedule-models
leecalcote 3e6a8ce
refactor of code generator
leecalcote 4031549
enhance helpers
leecalcote fc39486
enhance helpers
leecalcote d875206
refactor
leecalcote cf896e0
Merge branch 'add-credential-role-schedule-models' of https://github.…
leecalcote c13a2b4
Create validation-package.md for schema validation design
leecalcote 288e9ef
Add credential, role, schedule, and capability construct schemas with…
marblom007 9d5a844
Address PR review feedback
marblom007 f64ba70
refactor of code generator
leecalcote 82ccc3c
enhance helpers
leecalcote 1419357
enhance helpers
leecalcote 55d22c7
refactor
leecalcote 400dc5b
Bump minimatch from 10.1.1 to 10.2.4
dependabot[bot] b8db6a9
Generate build artifacts from schemas
fitzergerald 35671ec
Update OpenAPI docs path in workflow configuration
CodexRaunak f661456
Update OpenAPI schemas to include versioning, descriptions, and conta…
fitzergerald 16f5cee
Generate build artifacts from schemas
leecalcote bc01ad6
Update schema
fitzergerald 52a1400
Merge branch 'add-credential-role-schedule-models' of https://github.…
marblom007 4c8d99b
incorporate review feedback
marblom007 80765c5
generated build
marblom007 ca79cc2
ensure db: persistence
marblom007 b52964e
generate-golang enhancement
marblom007 672017a
regenerate models
marblom007 b1b124f
Potential fix for pull request finding
fitzergerald d6dabb1
avoiding pointer-to-copy pattern
fitzergerald 698031f
Merge branch 'add-credential-role-schedule-models' of https://github.…
fitzergerald c2d3e37
add skills; regenerate code
fitzergerald 8443a2d
Fix generator regressions from PR review
fitzergerald 8bb054c
Handle composed schema tag inheritance in Go generator
fitzergerald a5056df
Remove coding agent and skill-related files from this PR
fitzergerald ed6dc8d
Address remaining PR review feedback
fitzergerald f1040c7
Add relationship compatibility aliases
fitzergerald 397e333
Restore explicit Go type aliases for generated schemas
fitzergerald 1271434
Move OpenAPI schema skill into separate PR
fitzergerald bc489e4
Simplify model version schema ref
fitzergerald 7b717ec
Refactor and generate helper methods for various models; remove depre…
fitzergerald 1de84b9
Fix remaining PR review issues
fitzergerald bb77ef4
Fix Go generator schema tag overrides for component modelId
miacycle 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,192 @@ | ||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
|
|
||
| const GENERATED_HELPER_SPECS = { | ||
| "v1beta1/academy": { | ||
| mapStructTypes: ["Quiz", "QuizSubmission", "QuizEvaluationResult"], | ||
| }, | ||
| "v1beta1/connection": { | ||
| eventCategories: { | ||
| Connection: "connection", | ||
| MesheryInstance: "connection", | ||
| }, | ||
| }, | ||
| "v1beta1/credential": { | ||
| eventCategories: { | ||
| Credential: "credential", | ||
| }, | ||
| }, | ||
| "v1beta1/environment": { | ||
| eventCategories: { | ||
| Environment: "environment", | ||
| }, | ||
| }, | ||
| "v1beta1/key": { | ||
| eventCategories: { | ||
| Key: "key", | ||
| }, | ||
| }, | ||
| "v1beta1/keychain": { | ||
| eventCategories: { | ||
| Keychain: "keychain", | ||
| }, | ||
| }, | ||
| "v1beta1/organization": { | ||
| eventCategories: { | ||
| Organization: "organization", | ||
| }, | ||
| jsonTypes: ["OrgMetadata"], | ||
| }, | ||
| "v1beta1/plan": { | ||
| eventCategories: { | ||
| Plan: "plan", | ||
| }, | ||
| }, | ||
| "v1beta1/role": { | ||
| eventCategories: { | ||
| Role: "role", | ||
| }, | ||
| }, | ||
| "v1beta1/schedule": { | ||
| eventCategories: { | ||
| Schedule: "schedule", | ||
| }, | ||
| }, | ||
| "v1beta1/subscription": { | ||
| eventCategories: { | ||
| Subscription: "subscription", | ||
| }, | ||
| }, | ||
| "v1beta1/team": { | ||
| eventCategories: { | ||
| Team: "team", | ||
| }, | ||
| }, | ||
| "v1beta1/user": { | ||
| eventCategories: { | ||
| User: "user", | ||
| }, | ||
| mapStructTypes: ["Preference"], | ||
| }, | ||
| }; | ||
|
|
||
| function renderEventCategoryMethods(eventCategories) { | ||
| return Object.entries(eventCategories) | ||
| .map( | ||
| ([typeName, eventCategory]) => `func (*${typeName}) EventCategory() string { | ||
| \treturn ${JSON.stringify(eventCategory)} | ||
| }`, | ||
| ) | ||
| .join("\n\n"); | ||
| } | ||
|
|
||
| function renderMapStructMethods(typeName) { | ||
| return `func (value *${typeName}) Scan(src interface{}) error { | ||
| \tif src == nil { | ||
| \t\t*value = ${typeName}{} | ||
| \t\treturn nil | ||
| \t} | ||
|
|
||
| \tmapVal := core.Map{} | ||
| \tif err := mapVal.Scan(src); err != nil { | ||
| \t\treturn err | ||
| \t} | ||
|
|
||
| \treturn core.MapToStruct(mapVal, value) | ||
| } | ||
|
|
||
| func (value ${typeName}) Value() (driver.Value, error) { | ||
| \tmapVal, err := core.StructToMap(value) | ||
| \tif err != nil { | ||
| \t\treturn nil, err | ||
| \t} | ||
|
|
||
| \treturn core.Map(mapVal).Value() | ||
| }`; | ||
| } | ||
|
|
||
| function renderJsonMethods(typeName) { | ||
| return `func (value *${typeName}) Scan(src interface{}) error { | ||
| \tif src == nil { | ||
| \t\t*value = ${typeName}{} | ||
| \t\treturn nil | ||
| \t} | ||
|
|
||
| \tdata, err := utils.Cast[[]byte](src) | ||
| \tif err != nil { | ||
| \t\treturn err | ||
| \t} | ||
|
|
||
| \tif err := json.Unmarshal(data, value); err != nil { | ||
| \t\treturn utils.ErrUnmarshal(err) | ||
| \t} | ||
|
|
||
| \treturn nil | ||
| } | ||
|
|
||
| func (value ${typeName}) Value() (driver.Value, error) { | ||
| \tmarshaledValue, err := json.Marshal(value) | ||
| \tif err != nil { | ||
| \t\treturn nil, utils.ErrMarshal(err) | ||
| \t} | ||
|
|
||
| \treturn marshaledValue, nil | ||
| }`; | ||
| } | ||
|
|
||
| function renderGeneratedHelperFile(pkg, spec) { | ||
| const imports = new Map(); | ||
| const sections = []; | ||
|
|
||
| if (spec.mapStructTypes?.length) { | ||
| imports.set("database/sql/driver", null); | ||
| imports.set("github.com/meshery/schemas/models/core", "core"); | ||
| sections.push(spec.mapStructTypes.map(renderMapStructMethods).join("\n\n")); | ||
| } | ||
|
|
||
| if (spec.jsonTypes?.length) { | ||
| imports.set("database/sql/driver", null); | ||
| imports.set("encoding/json", null); | ||
| imports.set("github.com/meshery/meshkit/utils", null); | ||
| sections.push(spec.jsonTypes.map(renderJsonMethods).join("\n\n")); | ||
| } | ||
|
|
||
| if (spec.eventCategories && Object.keys(spec.eventCategories).length > 0) { | ||
| sections.push(renderEventCategoryMethods(spec.eventCategories)); | ||
| } | ||
|
|
||
| if (sections.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const importBlock = imports.size | ||
| ? `import (\n${[...imports.entries()] | ||
| .sort(([left], [right]) => left.localeCompare(right)) | ||
| .map(([importPath, alias]) => | ||
| alias ? `\t${alias} ${JSON.stringify(importPath)}` : `\t${JSON.stringify(importPath)}`, | ||
| ) | ||
| .join("\n")}\n)\n\n` | ||
| : ""; | ||
|
|
||
| return `// Code generated by build/generate-golang.js; DO NOT EDIT.\npackage ${pkg.name}\n\n${importBlock}${sections.join("\n\n")}\n`; | ||
| } | ||
|
|
||
| function writeGeneratedHelperFile(pkg, outputDir) { | ||
| const spec = GENERATED_HELPER_SPECS[`${pkg.version}/${pkg.dirName}`]; | ||
| if (!spec) { | ||
| return null; | ||
| } | ||
|
|
||
| const filePath = path.join(outputDir, "zz_generated.helpers.go"); | ||
| const content = renderGeneratedHelperFile(pkg, spec); | ||
| if (!content) { | ||
| return null; | ||
| } | ||
|
|
||
| fs.writeFileSync(filePath, content, "utf-8"); | ||
| return filePath; | ||
| } | ||
|
|
||
| module.exports = { | ||
| writeGeneratedHelperFile, | ||
| }; | ||
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.