dmapper

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2026 License: Apache-2.0 Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

dmapper

A Go library for declarative data mapping using CEL (Common Expression Language).

Features

  • Declarative mapping rules
  • CEL expressions for data transformation
  • Required/optional rule handling
  • Nested path support for target properties

Installation

go get github.com/kdex-tech/dmapper

Usage

package main

import (
	"fmt"

	"github.com/kdex-tech/dmapper"
)

func main() {
	// Define mapping rules
	rules := []dmapper.MappingRule{
		{
			SourceExpression: "self.oidc.groups.filter(g, g.startsWith('app_'))",
			TargetPropPath:   "auth.internal_groups",
		},
		{
			SourceExpression: "self.oidc.name",
			TargetPropPath:   "name",
			Required:         true,
		},
	}

	// Create a new mapper
	m, err := dmapper.NewMapper(rules)
	if err != nil {
		fmt.Println("Error creating mapper:", err)
		return
	}

	// Input data
	input := map[string]any{
		"oidc": map[string]any{
			"name":   "John Doe",
			"groups": []string{"app_group1", "app_group2", "other_group"},
		},
	}

	// Execute mapping
	result, err := m.Execute(input)
	if err != nil {
		fmt.Println("Error executing mapping:", err)
		return
	}

	fmt.Println("Result:", result)
}

MappingRule

The MappingRule struct defines a single mapping rule:

type MappingRule struct {
	// Merge controls how the result combines with a value already at
	// TargetPropPath: Accumulate (default) or Replace.
	Merge MergeStrategy `json:"merge,omitempty"`

	// Required indicates that if the rule fails to produce a value the rule
	// will be skipped. Otherwise the execution should fail.
	Required bool `json:"required"`

	// SourceExpression is CEL program to compute a transformation of input into
	// a new form.
	SourceExpression string `json:"sourceExpression"`

	// TargetPropPath is a dot-separated property path for where the result
	// should be attached in the output.
	TargetPropPath string `json:"targetPropPath"`
}
Fields
  • Merge: Accumulate (the default) or Replace. See Merge semantics.
  • Required: If true, the mapper will return an error if this rule fails. If false, the rule will be skipped and execution will continue.
  • SourceExpression: A CEL expression that operates on the input data. The input is available as the self variable.
  • TargetPropPath: A dot-separated path indicating where the result should be placed in the output map.

Merge semantics

Rules apply in order, and each rule sees earlier rules' output in self. A rule's result is then combined with whatever is already at its targetPropPath, according to merge:

  • Accumulate (default): when both the existing value and the result are lists, the result is the existing list followed by the result's items that are not already present. Restating self.<target> is harmless. Any other shape pair replaces.
  • Replace: the result always replaces. Use it for any rule meant to filter, transform, normalise or override a list already at its own target.

With entitlements: [static:a] and extra_grants: [extra:b]:

Rules (all target entitlements) Result
self.extra_grants [static:a extra:b]
self.entitlements + self.extra_grants [static:a extra:b]
self.entitlements, then self.extra_grants [static:a extra:b]

Under the default, values already at the target in the input stay in the output, including claims asserted by an identity provider. So a rule that reads self.<target> and writes <target> back changed must opt out:

  • a filter (groups → only app_* groups) would remove nothing;
  • a transform (roles.map(r, r.lowerAscii()), prefixing) would keep the originals beside the transformed values, and grows on every re-run;
  • an override (self.mapped_roles → roles) would keep the roles it meant to replace.

(self is typed map(string, any), so a top-level value needs dyn() before a comprehension.)

- sourceExpression: dyn(self.roles).filter(r, r != 'guest')
  targetPropPath: roles
  merge: Replace

Before v0.2.0 every rule replaced its target.

CEL Support

This library uses Google's Common Expression Language (CEL) for data transformation. CEL supports:

  • Variables: self (the input data)
  • Functions: filter(), size(), startsWith(), etc.
  • Operators: Standard arithmetic, comparison, and logical operators
  • Type System: Strong typing with automatic type conversion

See the CEL documentation for complete CEL language details.

Error Handling

  • Compilation Errors: Errors during mapper compilation (e.g., invalid CEL expressions) will be returned by NewMapper.
  • Execution Errors: Errors during expression evaluation will be handled based on the Required field:
    • If Required is true and evaluation fails, Execute will return an error.
    • If Required is false and evaluation fails, the rule will be skipped and Execute will continue with the next rule.

Testing

To run the tests:

go test

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mapper

type Mapper struct {
	CompiledMappingRules []compiledMappingRule
}

Mapper is a collection of compiledMappingRules.

func NewMapper

func NewMapper(rules []MappingRule) (*Mapper, error)

func (*Mapper) Execute

func (m *Mapper) Execute(input map[string]any) (map[string]any, error)

type MappingRule

type MappingRule struct {
	// merge controls how the rule's result combines with a value already at
	// targetPropPath. Accumulate (the default when empty): when both the
	// existing value and the result are lists, the result is the existing list
	// followed by the result's items not already present (order-preserving
	// set-union), so restating self.<target> is harmless and idempotent.
	// Values already at that path in the input — including claims asserted by
	// an identity provider — therefore stay in the output. Any other shape
	// pair (scalar, map, list vs non-list) replaces. Replace: the result
	// always replaces. Use it for any rule meant to filter, transform,
	// normalise or override a list already at its own target (e.g. a rule
	// that reads self.groups and writes groups).
	// +kubebuilder:validation:Optional
	// +kubebuilder:validation:Enum=Accumulate;Replace
	Merge MergeStrategy `json:"merge,omitempty"`

	// required indicates that if the rule fails to produce a value the rule
	// will be skipped. Otherwise the execution should fail.
	// +kubebuilder:validation:Optional
	// +kubebuilder:default:=false
	Required bool `json:"required"`

	// sourceExpression is CEL program to compute a transformation of input into
	// a new form.
	// +kubebuilder:validation:Required
	// +kubebuilder:validation:MinLength=2
	// +kubebuilder:example:=`self.oidc.groups.filter(g, g.startsWith('app_')).join(',')`
	SourceExpression string `json:"sourceExpression"`

	// targetPropPath is a dot-separated property path for where the result
	// should be attached in the output.
	// +kubebuilder:validation:Required
	// +kubebuilder:validation:MinLength=2
	// +kubebuilder:example:=`auth.internal_groups`
	TargetPropPath string `json:"targetPropPath"`
}

MappingRule defines a transformation rule for mapping arbitrary data from an input to an output.

type MergeStrategy added in v0.2.0

type MergeStrategy string

MergeStrategy controls how a rule's result combines with a value already present at its targetPropPath.

const (
	// MergeAccumulate (the default when empty): when both the existing value
	// and the result are lists, the result is the existing list followed by
	// the result's items not already present. Any other shape pair replaces.
	MergeAccumulate MergeStrategy = "Accumulate"
	// MergeReplace: the result always replaces the existing value. Use it for
	// a rule that filters, transforms, normalises or overrides a list already
	// at its own target.
	MergeReplace MergeStrategy = "Replace"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL