influxdbhelper icon indicating copy to clipboard operation
influxdbhelper copied to clipboard

Embedded struct error: all fields must have non-empty names

Open bminer opened this issue 7 years ago • 4 comments

If I have a struct that uses "type embedding" to embed another struct, even if the embedded struct has influx tags on its fields, influxdbhelper complains:

Error writing ... data to influx:  all fields must have non-empty names

Ideally, embedded structs should be supported.

bminer avatar Sep 28 '18 13:09 bminer

Do you have any thoughts on naming fields of embedded structs in influx, as influx records are flat?

cbrake avatar Sep 28 '18 13:09 cbrake

I think influxdbhelper should flatten embedded structs. The Go compiler supports this strange flattening property of embedded structs. For example, if you have:

type A struct {
  FieldA string `influx: fieldA,tag`
}
type B struct {
  A
  FieldB string `influx: fieldB,tag`
}

In this case, B.A.FieldA and B.FieldA are synonymous, and it appears that the embedded struct has been "flattened" in the latter case. Disclaimer: I'm unsure how the reflection API in Go works for embedded structs.

bminer avatar Sep 28 '18 13:09 bminer

This might be helpful: https://stackoverflow.com/questions/24333494/golang-reflection-on-embedded-structs

bminer avatar Sep 28 '18 13:09 bminer

agreed, it would be handy to be able to "compose" structs and essentially flatten them for storage in influx.

The workaround for now is to create another struct for writing into influx, and then copy your data to that. Since influx schemas are generally not huge, this has not been a big problem.

cbrake avatar Sep 28 '18 14:09 cbrake