From 48666a59c6ac52e14ff6fceab7cdff3d7f75ed79 Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 29 Mar 2024 12:21:04 +0100 Subject: [PATCH] chore: generate (un)marshalers --- api/chargemode.go | 11 +++-------- api/feature.go | 10 +--------- api/feature_enumer.go | 14 +++++++++++++- util/templates/paramtype.go | 14 +------------- util/templates/paramtype_enumer.go | 14 +++++++++++++- 5 files changed, 31 insertions(+), 32 deletions(-) diff --git a/api/chargemode.go b/api/chargemode.go index 3cc5ad6c0..076101dab 100644 --- a/api/chargemode.go +++ b/api/chargemode.go @@ -27,12 +27,7 @@ func ChargeModeString(mode string) (ChargeMode, error) { var _ encoding.TextUnmarshaler = (*ChargeMode)(nil) func (c *ChargeMode) UnmarshalText(text []byte) error { - casted, err := ChargeModeString(string(text)) - if err != nil { - return err - } - - *c = casted - - return nil + var err error + *c, err = ChargeModeString(string(text)) + return err } diff --git a/api/feature.go b/api/feature.go index 179ef0b57..15d7e200f 100644 --- a/api/feature.go +++ b/api/feature.go @@ -2,15 +2,7 @@ package api type Feature int -func (f *Feature) UnmarshalText(text []byte) error { - feat, err := FeatureString(string(text)) - if err == nil { - *f = feat - } - return err -} - -//go:generate enumer -type Feature +//go:generate enumer -type Feature -text const ( _ Feature = iota Offline diff --git a/api/feature_enumer.go b/api/feature_enumer.go index 8636adf68..858b77ac9 100644 --- a/api/feature_enumer.go +++ b/api/feature_enumer.go @@ -1,4 +1,4 @@ -// Code generated by "enumer -type Feature"; DO NOT EDIT. +// Code generated by "enumer -type Feature -text"; DO NOT EDIT. package api @@ -89,3 +89,15 @@ func (i Feature) IsAFeature() bool { } return false } + +// MarshalText implements the encoding.TextMarshaler interface for Feature +func (i Feature) MarshalText() ([]byte, error) { + return []byte(i.String()), nil +} + +// UnmarshalText implements the encoding.TextUnmarshaler interface for Feature +func (i *Feature) UnmarshalText(text []byte) error { + var err error + *i, err = FeatureString(string(text)) + return err +} diff --git a/util/templates/paramtype.go b/util/templates/paramtype.go index a024cd404..6883f4439 100644 --- a/util/templates/paramtype.go +++ b/util/templates/paramtype.go @@ -2,19 +2,7 @@ package templates type ParamType int -func (c *ParamType) UnmarshalText(text []byte) error { - typ, err := ParamTypeString(string(text)) - if err == nil { - *c = typ - } - return err -} - -func (c ParamType) MarshalText() ([]byte, error) { - return []byte(c.String()), nil -} - -//go:generate enumer -type ParamType -trimprefix Type +//go:generate enumer -type ParamType -trimprefix Type -text const ( TypeString ParamType = iota // default type string TypeBool diff --git a/util/templates/paramtype_enumer.go b/util/templates/paramtype_enumer.go index 5c91a6ea4..863a57fb1 100644 --- a/util/templates/paramtype_enumer.go +++ b/util/templates/paramtype_enumer.go @@ -1,4 +1,4 @@ -// Code generated by "enumer -type ParamType -trimprefix Type"; DO NOT EDIT. +// Code generated by "enumer -type ParamType -trimprefix Type -text"; DO NOT EDIT. package templates @@ -100,3 +100,15 @@ func (i ParamType) IsAParamType() bool { } return false } + +// MarshalText implements the encoding.TextMarshaler interface for ParamType +func (i ParamType) MarshalText() ([]byte, error) { + return []byte(i.String()), nil +} + +// UnmarshalText implements the encoding.TextUnmarshaler interface for ParamType +func (i *ParamType) UnmarshalText(text []byte) error { + var err error + *i, err = ParamTypeString(string(text)) + return err +}