elastic / elastic/elastic-agent-libs

mapstr.M.Clone() should clone []interface{} values

Open
#75 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
6
Forks
57
Avg merge
6d 7h
Merged PRs (30d)
3

Description

`Clone()` does a deep clone of inner `map[string]interface{}` and `mapstr.M` values. But if a map is contained in an array then the value is not cloned. For example, if an object like this were `Clone()`ed then map within `array` would not be cloned because `Clone()` does not look inside `[]interface{}`.

```go
mapstr.M{
"array": []interface{}{
map[string]interface{}{
"key": "value",
},
},
}
```

One use case is Filebeat's httpjson input. It uses heavily uses `Clone()` and as a result arrays object's do not behave as expected. It depends on the result of `Clone()` only containing `mapstr.M` (and not `map[string]interface{}`). It needs `mapstr.M`'s implementation of `fmt.Stringer` that returns json.

As a possible workaround, httpjson has been considering its own `deepClone(mapstr.M) mapstr.M` ([workaround.go.txt](https://github.com/elastic/elastic-agent-libs/files/9383968/workaround.go.txt)) implementation. But I think it would be better if `mapstr.M Clone`'s implementation automatically handled arrays.

Relates: https://github.com/elastic/beats/pull/32472

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.