Skip to content

[pkg/ottl] Allow Optional KeyPath in SliceToMap #41390

@XuechunHou

Description

@XuechunHou

Component(s)

pkg/ottl

Is your feature request related to a problem? Please describe.

The current implementation of SliceToMap has a required argument, keyPath, which expects an array of keys from the input slice of objects. These objects need to have a same key. The values corresponding to these keys must be strings.

This restriction limits the use cases of this function. The given slice of objects might not always have an entry with the same key.

Describe the solution you'd like

Mark keyPath Optional. If keyPath isn't specified, use the string representation of the index when looping through objects in the slice.

i.e:

type SliceToMapArguments[K any] struct {
	Target    ottl.Getter[K]
	KeyPath   ottl.Optional[[]string]
	ValuePath ottl.Optional[[]string]
}


m1 := {
"data1": "param1"
"data2": "param2"
}

m2 := {
"data3": "param3"
"data4": "param4"
}

Target := []any {m1, m2}
SliceToMap(Target) -> {
  "0": {
  "data1": "param1"
  "data2": "param2"
  }

  "1": {
  "data3": "param3"
  "data4": "param4"
  }
}

Describe alternatives you've considered

N/A

Additional context

N/A

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions