Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix unmarshaling from text of enums with bitmask flag (#90) #91

Merged
merged 1 commit into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions cmd/dialects-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package dialects

import (
"testing"
"encoding"
"reflect"

"github.com/stretchr/testify/require"

Expand All @@ -36,6 +38,44 @@ func TestDialects(t *testing.T) {
}()
{{- end }}
}

var casesEnum = []struct {
name string
dec encoding.TextMarshaler
enc string
}{
{
"bitmask",
common.POSITION_TARGET_TYPEMASK_VX_IGNORE | common.POSITION_TARGET_TYPEMASK_VY_IGNORE,
"POSITION_TARGET_TYPEMASK_VX_IGNORE | POSITION_TARGET_TYPEMASK_VY_IGNORE",
},
{
"value",
common.GPS_FIX_TYPE_NO_FIX,
"GPS_FIX_TYPE_NO_FIX",
},
}

func TestEnumUnmarshalText(t *testing.T) {
for _, ca := range casesEnum {
t.Run(ca.name, func(t *testing.T) {
dec := reflect.New(reflect.TypeOf(ca.dec)).Interface().(encoding.TextUnmarshaler)
err := dec.UnmarshalText([]byte(ca.enc))
require.NoError(t, err)
require.Equal(t, ca.dec, reflect.ValueOf(dec).Elem().Interface())
})
}
}

func TestEnumMarshalText(t *testing.T) {
for _, ca := range casesEnum {
t.Run(ca.name, func(t *testing.T) {
byts, err := ca.dec.MarshalText()
require.NoError(t, err)
require.Equal(t, ca.enc, string(byts))
})
}
}
`))

func writeTemplate(fpath string, tpl *template.Template, args map[string]interface{}) error {
Expand Down
1 change: 1 addition & 0 deletions pkg/conversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (e *{{ .Enum.Name }}) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
{{- else }}
if value, ok := values_{{ .Enum.Name }}[string(text)]; ok {
*e = value
Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/ardupilotmega/enum_ekf_status_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (e *EKF_STATUS_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/ardupilotmega/enum_gopro_heartbeat_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (e *GOPRO_HEARTBEAT_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (e *GOPRO_VIDEO_SETTINGS_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/ardupilotmega/enum_limit_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (e *LIMIT_MODULE) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/ardupilotmega/enum_rally_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (e *RALLY_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_adsb_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (e *ADSB_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_ais_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func (e *AIS_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_attitude_target_typemask.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (e *ATTITUDE_TARGET_TYPEMASK) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_autotune_axis.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (e *AUTOTUNE_AXIS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_camera_cap_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (e *CAMERA_CAP_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_camera_tracking_target_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (e *CAMERA_TRACKING_TARGET_DATA) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_esc_failure_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (e *ESC_FAILURE_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_estimator_status_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (e *ESTIMATOR_STATUS_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_gimbal_device_cap_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (e *GIMBAL_DEVICE_CAP_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_gimbal_device_error_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (e *GIMBAL_DEVICE_ERROR_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_gimbal_device_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (e *GIMBAL_DEVICE_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_gimbal_manager_cap_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (e *GIMBAL_MANAGER_CAP_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_gimbal_manager_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (e *GIMBAL_MANAGER_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_gps_input_ignore_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (e *GPS_INPUT_IGNORE_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_highres_imu_updated_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (e *HIGHRES_IMU_UPDATED_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_hil_sensor_updated_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (e *HIL_SENSOR_UPDATED_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_hl_failure_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (e *HL_FAILURE_FLAG) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_mav_battery_fault.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (e *MAV_BATTERY_FAULT) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_mav_do_reposition_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (e *MAV_DO_REPOSITION_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_mav_generator_status_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (e *MAV_GENERATOR_STATUS_FLAG) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_mav_power_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (e *MAV_POWER_STATUS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_mav_protocol_capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func (e *MAV_PROTOCOL_CAPABILITY) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_mav_sys_status_sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func (e *MAV_SYS_STATUS_SENSOR) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_mav_sys_status_sensor_extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (e *MAV_SYS_STATUS_SENSOR_EXTENDED) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_mav_winch_status_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (e *MAV_WINCH_STATUS_FLAG) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_position_target_typemask.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (e *POSITION_TARGET_TYPEMASK) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_serial_control_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (e *SERIAL_CONTROL_FLAG) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_utm_data_avail_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (e *UTM_DATA_AVAIL_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/common/enum_video_stream_status_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (e *VIDEO_STREAM_STATUS_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/development/enum_airspeed_sensor_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (e *AIRSPEED_SENSOR_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/development/enum_mav_battery_status_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func (e *MAV_BATTERY_STATUS_FLAGS) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/development/enum_mav_mode_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (e *MAV_MODE_PROPERTY) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (e *TARGET_ABSOLUTE_SENSOR_CAPABILITY_FLAGS) UnmarshalText(text []byte) err
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/minimal/enum_mav_mode_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (e *MAV_MODE_FLAG) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/dialects/minimal/enum_mav_mode_flag_decode_position.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (e *MAV_MODE_FLAG_DECODE_POSITION) UnmarshalText(text []byte) error {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
return nil
}

Expand Down
Loading
Loading