Use generic package maps to clone maps

This commit is contained in:
Holger Obermaier 2023-09-26 11:49:19 +02:00
parent 553fcff468
commit 19ec6d06db

View File

@ -7,6 +7,7 @@ import (
influxdb2 "github.com/influxdata/influxdb-client-go/v2" influxdb2 "github.com/influxdata/influxdb-client-go/v2"
write "github.com/influxdata/influxdb-client-go/v2/api/write" write "github.com/influxdata/influxdb-client-go/v2/api/write"
lp "github.com/influxdata/line-protocol" // MIT license lp "github.com/influxdata/line-protocol" // MIT license
"golang.org/x/exp/maps"
) )
// Most functions are derived from github.com/influxdata/line-protocol/metric.go // Most functions are derived from github.com/influxdata/line-protocol/metric.go
@ -195,19 +196,13 @@ func New(
) (CCMetric, error) { ) (CCMetric, error) {
m := &ccMetric{ m := &ccMetric{
name: name, name: name,
tags: make(map[string]string, len(tags)), tags: maps.Clone(tags),
meta: make(map[string]string, len(meta)), meta: maps.Clone(meta),
fields: make(map[string]interface{}, len(fields)), fields: make(map[string]interface{}, len(fields)),
tm: tm, tm: tm,
} }
// deep copy tags, meta data tags and fields // deep copy fields
for k, v := range tags {
m.tags[k] = v
}
for k, v := range meta {
m.meta[k] = v
}
for k, v := range fields { for k, v := range fields {
v := convertField(v) v := convertField(v)
if v == nil { if v == nil {
@ -221,28 +216,14 @@ func New(
// FromMetric copies the metric <other> // FromMetric copies the metric <other>
func FromMetric(other CCMetric) CCMetric { func FromMetric(other CCMetric) CCMetric {
otags := other.Tags()
ometa := other.Meta() return &ccMetric{
ofields := other.Fields()
m := &ccMetric{
name: other.Name(), name: other.Name(),
tags: make(map[string]string, len(otags)), tags: maps.Clone(other.Tags()),
meta: make(map[string]string, len(ometa)), meta: maps.Clone(other.Meta()),
fields: make(map[string]interface{}, len(ofields)), fields: maps.Clone(other.Fields()),
tm: other.Time(), tm: other.Time(),
} }
// deep copy tags, meta data tags and fields
for key, value := range otags {
m.tags[key] = value
}
for key, value := range ometa {
m.meta[key] = value
}
for key, value := range ofields {
m.fields[key] = value
}
return m
} }
// FromInfluxMetric copies the influxDB line protocol metric <other> // FromInfluxMetric copies the influxDB line protocol metric <other>