Repository navigation
feat(generator): add map helpers and nested item helpers for array fields - #9
Merged
Merged
Conversation
When an array field contains object items with their own properties,
generate nested helper functions for each property. For example, a
`containers` array of objects with `name` and `image` fields now
produces:
containers: {
withName(name): { containers+: { name: name } },
withImage(image): { containers+: { image: image } },
}
This mirrors the existing OpenAPI path behavior and is now also
available in the JSON Schema generation path. Golden fixtures
updated for golangci and kratos schemas.
Generate two new function types for every array field in the model: mapXyz(f) - transforms each element via std.map(f, super.field) mapXyzByName(n, fn) - transforms only the element where item.name == n The ByName variant is only generated when array items have a "name" property. Both are available in the OpenAPI/CRD and JSON Schema generation paths. Implementation adds ArrayComprehensionType and CallArg builder types to emit the comprehension pattern and positional call arguments. Golden test fixtures updated for all affected schemas.
Document generated mapXyz(f), mapXyzByName(name, transformFunc), and nested helpers for array item properties. Add usage examples. Remove non-existent mapContainers.libsonnet reference from the custom patches tree. Add AGENTS.md with key commands, gotchas, and codegen pipeline docs.
Closed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds two new function types for every array field in generated Jsonnet libraries:
mapXyz(f)— generic element transformTransforms each element in an array using
std.map:mapXyzByName(name, transformFunc)— targeted element transformTransforms only the element where
item.namematches:Only generated when array items have a
"name"property (containers, env vars, volumes, etc).Additional changes
containers: { withName(), withImage() }), matching the existing OpenAPI path behavior and now also in the JSON Schema pathArrayComprehensionTypefor the[if/then/else for]pattern andCallArgfor positional function call argumentssuper['field-name']bracket notation used for fields with special charactersmap-test-schema.jsoncovering both named and unnamed array itemsAGENTS.mdadded with key commands and gotchasGenerated output example
{ mapContainers(f): { containers: std.map(f, super.containers) }, mapContainersByName(name, transformFunc): { containers: [if c.name == name then transformFunc(c) else c for c in super.containers], }, mapTags(f): { tags: std.map(f, super.tags) }, containers: { withName(name): { containers+: { name: name } }, withImage(image): { containers+: { image: image } }, }, }Files changed
pkg/builder/collections.goArrayComprehensionTypepkg/builder/funcs.goCallArgtypepkg/model/modifiers.goItemHasNamefield toModifierpkg/render/modifiers.gomapFnResult,mapByNameFnResult,superRefStringpkg/complier/jsonschemacomplier/jsonschemacomplier.gosuperRef, map functions, nested helperspkg/complier/jsonschemacomplier/jsonschemacomplier_test.gotestdata/*.json+*.golden.libsonnetREADME.mdAGENTS.md