I have an object that I receive:
{
"operation": "ACC00000001",
"prm": "23597250350000",
"conso_prod": "Conso",
"index_name": "BASE",
"index_value": "123456",
"timestamp": "2019-08-20T22:00:00Z"
}
The object I use is in a common lib, so it is shared by several services:
common.Measure:
type Measure struct {
Timestamp time.Time
Delta float64
Redistributed float64
IsProd bool
IndexValue uint32
IndexName string
Source string
}
and Meter:
type Meter struct {
ID string
Operation string
Unit string
Timestep time.Duration
Measures []Measure
}
But as the labels don't match, I must create another object MeasureFromJSON
which is the created based in the json I receive.
type MeasureFromJSON struct {
Operation string `json:"operation" binding:"required"`
Prm string `json:"prm"`
Conso_prod string `json:"conso_prod"`
Index_name string `json:"index_name"`
Index_value string `json:"index_value"`
Timestamp string `json:"timestamp"`
}
Thing is I don't like to use 2 models for the same entity, just because I have no JSON labels in common object. Is there a way to use the common models (meter.Measure
)?
Thing is I don't like to use 2 models for the same entity, just because I have no JSON labels in common object
First of all, I have a question, how come same entity return two different sets of data that majority of the fields are different?
If those different fields are still from single entity, then you could probably combine the fields and store it into single common.Measure
.
type Measure struct {
Timestamp time.Time `json:"timestamp"`
Delta float64
Redistributed float64
IsProd bool
IndexValue string `json:"index_value"`
IndexName string `json:"index_name"`
Source string
ConsoProd string `json:"conso_prod"`
Prm string `json:"prm"`
Operation string `json:"operation"`
}