Skip to content

Commit

Permalink
Allow marshaling of invalid enum values (#88)
Browse files Browse the repository at this point in the history
* allow marshaling of invalid enum values

* update dialects with new template
  • Loading branch information
twpayne authored Nov 9, 2023
1 parent 8797b13 commit 34a8c65
Show file tree
Hide file tree
Showing 229 changed files with 2,280 additions and 1,982 deletions.
30 changes: 17 additions & 13 deletions pkg/conversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,9 @@ const (
{{- else }}
import (
"strconv"
{{- if .Enum.Bitmask }}
"strings"
{{- else }}
"strconv"
{{- end }}
"fmt"
)
Expand Down Expand Up @@ -118,6 +117,9 @@ var values_{{ .Enum.Name }} = map[string]{{ .Enum.Name }}{
// MarshalText implements the encoding.TextMarshaler interface.
func (e {{ .Enum.Name }}) MarshalText() ([]byte, error) {
{{- if .Enum.Bitmask }}
if e == 0 {
return []byte("0"), nil
}
var names []string
for i := 0; i < {{ len .Enum.Values }}; i++ {
mask := {{ .Enum.Name }}(1 << i)
Expand All @@ -127,11 +129,10 @@ func (e {{ .Enum.Name }}) MarshalText() ([]byte, error) {
}
return []byte(strings.Join(names, " | ")), nil
{{- else }}
name, ok := labels_{{ .Enum.Name }}[e]
if !ok {
return nil, fmt.Errorf("invalid value %d", e)
if name, ok := labels_{{ .Enum.Name }}[e]; ok {
return []byte(name), nil
}
return []byte(name), nil
return []byte(strconv.Itoa(int(e))), nil
{{- end }}
}
Expand All @@ -144,16 +145,20 @@ func (e *{{ .Enum.Name }}) UnmarshalText(text []byte) error {
for _, label := range labels {
if value, ok := values_{{ .Enum.Name }}[label]; ok {
mask |= value
} else if value, err := strconv.Atoi(label); err == nil {
mask |= {{ .Enum.Name }}(value)
} else {
return fmt.Errorf("invalid label '%s'", label)
}
}
{{- else }}
value, ok := values_{{ .Enum.Name }}[string(text)]
if !ok {
if value, ok := values_{{ .Enum.Name }}[string(text)]; ok {
*e = value
} else if value, err := strconv.Atoi(string(text)); err == nil {
*e = {{ .Enum.Name }}(value)
} else {
return fmt.Errorf("invalid label '%s'", text)
}
*e = value
{{- end }}
return nil
}
Expand All @@ -164,11 +169,10 @@ func (e {{ .Enum.Name }}) String() string {
val, _ := e.MarshalText()
return string(val)
{{- else }}
name, ok := labels_{{ .Enum.Name }}[e]
if !ok {
return strconv.Itoa(int(e))
if name, ok := labels_{{ .Enum.Name }}[e]; ok {
return name
}
return name
return strconv.Itoa(int(e))
{{- end }}
}
{{- end }}
Expand Down
22 changes: 11 additions & 11 deletions pkg/dialects/all/enum_mav_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,28 +934,28 @@ var values_MAV_CMD = map[string]MAV_CMD{

// MarshalText implements the encoding.TextMarshaler interface.
func (e MAV_CMD) MarshalText() ([]byte, error) {
name, ok := labels_MAV_CMD[e]
if !ok {
return nil, fmt.Errorf("invalid value %d", e)
if name, ok := labels_MAV_CMD[e]; ok {
return []byte(name), nil
}
return []byte(name), nil
return []byte(strconv.Itoa(int(e))), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (e *MAV_CMD) UnmarshalText(text []byte) error {
value, ok := values_MAV_CMD[string(text)]
if !ok {
if value, ok := values_MAV_CMD[string(text)]; ok {
*e = value
} else if value, err := strconv.Atoi(string(text)); err == nil {
*e = MAV_CMD(value)
} else {
return fmt.Errorf("invalid label '%s'", text)
}
*e = value
return nil
}

// String implements the fmt.Stringer interface.
func (e MAV_CMD) String() string {
name, ok := labels_MAV_CMD[e]
if !ok {
return strconv.Itoa(int(e))
if name, ok := labels_MAV_CMD[e]; ok {
return name
}
return name
return strconv.Itoa(int(e))
}
22 changes: 11 additions & 11 deletions pkg/dialects/ardupilotmega/enum_accelcal_vehicle_pos.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,28 @@ var values_ACCELCAL_VEHICLE_POS = map[string]ACCELCAL_VEHICLE_POS{

// MarshalText implements the encoding.TextMarshaler interface.
func (e ACCELCAL_VEHICLE_POS) MarshalText() ([]byte, error) {
name, ok := labels_ACCELCAL_VEHICLE_POS[e]
if !ok {
return nil, fmt.Errorf("invalid value %d", e)
if name, ok := labels_ACCELCAL_VEHICLE_POS[e]; ok {
return []byte(name), nil
}
return []byte(name), nil
return []byte(strconv.Itoa(int(e))), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (e *ACCELCAL_VEHICLE_POS) UnmarshalText(text []byte) error {
value, ok := values_ACCELCAL_VEHICLE_POS[string(text)]
if !ok {
if value, ok := values_ACCELCAL_VEHICLE_POS[string(text)]; ok {
*e = value
} else if value, err := strconv.Atoi(string(text)); err == nil {
*e = ACCELCAL_VEHICLE_POS(value)
} else {
return fmt.Errorf("invalid label '%s'", text)
}
*e = value
return nil
}

// String implements the fmt.Stringer interface.
func (e ACCELCAL_VEHICLE_POS) String() string {
name, ok := labels_ACCELCAL_VEHICLE_POS[e]
if !ok {
return strconv.Itoa(int(e))
if name, ok := labels_ACCELCAL_VEHICLE_POS[e]; ok {
return name
}
return name
return strconv.Itoa(int(e))
}
22 changes: 11 additions & 11 deletions pkg/dialects/ardupilotmega/enum_camera_feedback_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,28 @@ var values_CAMERA_FEEDBACK_FLAGS = map[string]CAMERA_FEEDBACK_FLAGS{

// MarshalText implements the encoding.TextMarshaler interface.
func (e CAMERA_FEEDBACK_FLAGS) MarshalText() ([]byte, error) {
name, ok := labels_CAMERA_FEEDBACK_FLAGS[e]
if !ok {
return nil, fmt.Errorf("invalid value %d", e)
if name, ok := labels_CAMERA_FEEDBACK_FLAGS[e]; ok {
return []byte(name), nil
}
return []byte(name), nil
return []byte(strconv.Itoa(int(e))), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (e *CAMERA_FEEDBACK_FLAGS) UnmarshalText(text []byte) error {
value, ok := values_CAMERA_FEEDBACK_FLAGS[string(text)]
if !ok {
if value, ok := values_CAMERA_FEEDBACK_FLAGS[string(text)]; ok {
*e = value
} else if value, err := strconv.Atoi(string(text)); err == nil {
*e = CAMERA_FEEDBACK_FLAGS(value)
} else {
return fmt.Errorf("invalid label '%s'", text)
}
*e = value
return nil
}

// String implements the fmt.Stringer interface.
func (e CAMERA_FEEDBACK_FLAGS) String() string {
name, ok := labels_CAMERA_FEEDBACK_FLAGS[e]
if !ok {
return strconv.Itoa(int(e))
if name, ok := labels_CAMERA_FEEDBACK_FLAGS[e]; ok {
return name
}
return name
return strconv.Itoa(int(e))
}
22 changes: 11 additions & 11 deletions pkg/dialects/ardupilotmega/enum_camera_status_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,28 @@ var values_CAMERA_STATUS_TYPES = map[string]CAMERA_STATUS_TYPES{

// MarshalText implements the encoding.TextMarshaler interface.
func (e CAMERA_STATUS_TYPES) MarshalText() ([]byte, error) {
name, ok := labels_CAMERA_STATUS_TYPES[e]
if !ok {
return nil, fmt.Errorf("invalid value %d", e)
if name, ok := labels_CAMERA_STATUS_TYPES[e]; ok {
return []byte(name), nil
}
return []byte(name), nil
return []byte(strconv.Itoa(int(e))), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (e *CAMERA_STATUS_TYPES) UnmarshalText(text []byte) error {
value, ok := values_CAMERA_STATUS_TYPES[string(text)]
if !ok {
if value, ok := values_CAMERA_STATUS_TYPES[string(text)]; ok {
*e = value
} else if value, err := strconv.Atoi(string(text)); err == nil {
*e = CAMERA_STATUS_TYPES(value)
} else {
return fmt.Errorf("invalid label '%s'", text)
}
*e = value
return nil
}

// String implements the fmt.Stringer interface.
func (e CAMERA_STATUS_TYPES) String() string {
name, ok := labels_CAMERA_STATUS_TYPES[e]
if !ok {
return strconv.Itoa(int(e))
if name, ok := labels_CAMERA_STATUS_TYPES[e]; ok {
return name
}
return name
return strconv.Itoa(int(e))
}
22 changes: 11 additions & 11 deletions pkg/dialects/ardupilotmega/enum_copter_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,28 @@ var values_COPTER_MODE = map[string]COPTER_MODE{

// MarshalText implements the encoding.TextMarshaler interface.
func (e COPTER_MODE) MarshalText() ([]byte, error) {
name, ok := labels_COPTER_MODE[e]
if !ok {
return nil, fmt.Errorf("invalid value %d", e)
if name, ok := labels_COPTER_MODE[e]; ok {
return []byte(name), nil
}
return []byte(name), nil
return []byte(strconv.Itoa(int(e))), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (e *COPTER_MODE) UnmarshalText(text []byte) error {
value, ok := values_COPTER_MODE[string(text)]
if !ok {
if value, ok := values_COPTER_MODE[string(text)]; ok {
*e = value
} else if value, err := strconv.Atoi(string(text)); err == nil {
*e = COPTER_MODE(value)
} else {
return fmt.Errorf("invalid label '%s'", text)
}
*e = value
return nil
}

// String implements the fmt.Stringer interface.
func (e COPTER_MODE) String() string {
name, ok := labels_COPTER_MODE[e]
if !ok {
return strconv.Itoa(int(e))
if name, ok := labels_COPTER_MODE[e]; ok {
return name
}
return name
return strconv.Itoa(int(e))
}
22 changes: 11 additions & 11 deletions pkg/dialects/ardupilotmega/enum_deepstall_stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,28 @@ var values_DEEPSTALL_STAGE = map[string]DEEPSTALL_STAGE{

// MarshalText implements the encoding.TextMarshaler interface.
func (e DEEPSTALL_STAGE) MarshalText() ([]byte, error) {
name, ok := labels_DEEPSTALL_STAGE[e]
if !ok {
return nil, fmt.Errorf("invalid value %d", e)
if name, ok := labels_DEEPSTALL_STAGE[e]; ok {
return []byte(name), nil
}
return []byte(name), nil
return []byte(strconv.Itoa(int(e))), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (e *DEEPSTALL_STAGE) UnmarshalText(text []byte) error {
value, ok := values_DEEPSTALL_STAGE[string(text)]
if !ok {
if value, ok := values_DEEPSTALL_STAGE[string(text)]; ok {
*e = value
} else if value, err := strconv.Atoi(string(text)); err == nil {
*e = DEEPSTALL_STAGE(value)
} else {
return fmt.Errorf("invalid label '%s'", text)
}
*e = value
return nil
}

// String implements the fmt.Stringer interface.
func (e DEEPSTALL_STAGE) String() string {
name, ok := labels_DEEPSTALL_STAGE[e]
if !ok {
return strconv.Itoa(int(e))
if name, ok := labels_DEEPSTALL_STAGE[e]; ok {
return name
}
return name
return strconv.Itoa(int(e))
}
22 changes: 11 additions & 11 deletions pkg/dialects/ardupilotmega/enum_device_op_bustype.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,28 @@ var values_DEVICE_OP_BUSTYPE = map[string]DEVICE_OP_BUSTYPE{

// MarshalText implements the encoding.TextMarshaler interface.
func (e DEVICE_OP_BUSTYPE) MarshalText() ([]byte, error) {
name, ok := labels_DEVICE_OP_BUSTYPE[e]
if !ok {
return nil, fmt.Errorf("invalid value %d", e)
if name, ok := labels_DEVICE_OP_BUSTYPE[e]; ok {
return []byte(name), nil
}
return []byte(name), nil
return []byte(strconv.Itoa(int(e))), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (e *DEVICE_OP_BUSTYPE) UnmarshalText(text []byte) error {
value, ok := values_DEVICE_OP_BUSTYPE[string(text)]
if !ok {
if value, ok := values_DEVICE_OP_BUSTYPE[string(text)]; ok {
*e = value
} else if value, err := strconv.Atoi(string(text)); err == nil {
*e = DEVICE_OP_BUSTYPE(value)
} else {
return fmt.Errorf("invalid label '%s'", text)
}
*e = value
return nil
}

// String implements the fmt.Stringer interface.
func (e DEVICE_OP_BUSTYPE) String() string {
name, ok := labels_DEVICE_OP_BUSTYPE[e]
if !ok {
return strconv.Itoa(int(e))
if name, ok := labels_DEVICE_OP_BUSTYPE[e]; ok {
return name
}
return name
return strconv.Itoa(int(e))
}
6 changes: 6 additions & 0 deletions pkg/dialects/ardupilotmega/enum_ekf_status_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package ardupilotmega

import (
"fmt"
"strconv"
"strings"
)

Expand Down Expand Up @@ -65,6 +66,9 @@ var values_EKF_STATUS_FLAGS = map[string]EKF_STATUS_FLAGS{

// MarshalText implements the encoding.TextMarshaler interface.
func (e EKF_STATUS_FLAGS) MarshalText() ([]byte, error) {
if e == 0 {
return []byte("0"), nil
}
var names []string
for i := 0; i < 11; i++ {
mask := EKF_STATUS_FLAGS(1 << i)
Expand All @@ -82,6 +86,8 @@ func (e *EKF_STATUS_FLAGS) UnmarshalText(text []byte) error {
for _, label := range labels {
if value, ok := values_EKF_STATUS_FLAGS[label]; ok {
mask |= value
} else if value, err := strconv.Atoi(label); err == nil {
mask |= EKF_STATUS_FLAGS(value)
} else {
return fmt.Errorf("invalid label '%s'", label)
}
Expand Down
Loading

0 comments on commit 34a8c65

Please sign in to comment.