Fix/config/measurements in yaml (#135)

Custom type & marshal implementation for measurements to write base64 instead of single bytes
This commit is contained in:
Fabian Kammel 2022-05-12 10:15:00 +02:00 committed by GitHub
parent 19394e5563
commit 14103e4f89
4 changed files with 177 additions and 16 deletions

View file

@ -0,0 +1,33 @@
package config
import "encoding/base64"
type Measurements map[uint32][]byte
func (m Measurements) MarshalYAML() (interface{}, error) {
base64Map := make(map[uint32]string)
for key, value := range m {
base64Map[key] = base64.StdEncoding.EncodeToString(value[:])
}
return base64Map, nil
}
func (m *Measurements) UnmarshalYAML(unmarshal func(interface{}) error) error {
base64Map := make(map[uint32]string)
err := unmarshal(base64Map)
if err != nil {
return err
}
*m = make(Measurements)
for key, value := range base64Map {
measurement, err := base64.StdEncoding.DecodeString(value)
if err != nil {
return err
}
(*m)[key] = measurement
}
return nil
}