Skip to content

feat(generator): add map helpers and nested item helpers for array fields - #9

Merged
thisisibrahimd merged 3 commits into
mainfrom
add-support-for-mapping-arrays
Aug 31, 2026
Merged

thisisibrahimd merged 3 commits into
mainfrom
add-support-for-mapping-arrays

Conversation

@thisisibrahimd

Copy link
Copy Markdown
Owner

Summary

Adds two new function types for every array field in generated Jsonnet libraries:

mapXyz(f) — generic element transform

Transforms each element in an array using std.map:

deployment + mapContainers(function(c)
  c + { image: c.image + '-patched' }
)

mapXyzByName(name, transformFunc) — targeted element transform

Transforms only the element where item.name matches:

deployment + mapContainersByName('app', function(c)
  c + { image: 'myapp:v2' }
)

Only generated when array items have a "name" property (containers, env vars, volumes, etc).

Additional changes

  • Nested helpers for array items — arrays of objects now produce per-property helpers (e.g. containers: { withName(), withImage() }), matching the existing OpenAPI path behavior and now also in the JSON Schema path
  • New builder types — ArrayComprehensionType for the [if/then/else for] pattern and CallArg for positional function call arguments
  • Hyphenated field support — super['field-name'] bracket notation used for fields with special characters
  • Test schema — map-test-schema.json covering both named and unnamed array items
  • Documentation — README updated with array support examples, AGENTS.md added with key commands and gotchas

Generated 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

File Change
pkg/builder/collections.go Add ArrayComprehensionType
pkg/builder/funcs.go Add CallArg type
pkg/model/modifiers.go Add ItemHasName field to Modifier
pkg/render/modifiers.go Add mapFnResult, mapByNameFnResult, superRefString
pkg/complier/jsonschemacomplier/jsonschemacomplier.go Add superRef, map functions, nested helpers
pkg/complier/jsonschemacomplier/jsonschemacomplier_test.go Add map-test-schema test case
testdata/*.json + *.golden.libsonnet New schema + updated golden fixtures
README.md Document array map functions with examples
AGENTS.md New file with key commands and gotchas

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.
@thisisibrahimd
thisisibrahimd merged commit 9b44746 into main Aug 31, 2026
1 check passed
@thisisibrahimd thisisibrahimd linked an issue Sep 1, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add support for map lists

1 participant