chore: generate (un)marshalers

This commit is contained in:
andig 2024-03-29 12:21:04 +01:00
parent 6446d920e9
commit 48666a59c6
5 changed files with 31 additions and 32 deletions

View file

@ -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
}

View file

@ -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

View file

@ -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
}

View file

@ -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

View file

@ -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
}