-
Notifications
You must be signed in to change notification settings - Fork 34
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 mavlink codegen enum methods #47
Conversation
Hello, you're right about adding support for combined flags, however, Therefore, edit the PR in this way:
func (e enum) MarshalText() ([]byte, error) {
if l, ok := labels_{{ .Enum.Name }}[e]; ok {
return []byte(l), nil
}
return []byte(strconv.FormatUint(uint64(e), 16)), nil
}
func (e enum) UnmarshalText(text []byte) error {
if rl, ok := reverseLabels_{{ .Enum.Name }}[string(text)]; ok {
*e = rl
return nil
}
hexInt, err := strconv.ParseUint(string(text), 16, 32)
if err == nil {
*e = {{ .Enum.Name }}(hexInt)
return nil
}
return errors.New("invalid value")
}
func (e {{ .Enum.Name }}) String() string {
val, _ := e.MarshalText()
return string(val)
} thanks |
If you need names, I can suggest this code:
Tests:
|
It seems that with the update of mavlink, new Enums and Messages have been added. |
a9368f6
to
7c2112e
Compare
Refactored MarshalText, UnmarshalText, and String methods for Enum types. Add MessageTargetAbsolute MessageTargetRelative and TARGET_OBS_FRAME TARGET_ABSOLUTE_SENSOR_CAPABILITY_FLAGS Enums
merged, thanks! |
@@ -57,27 +58,38 @@ var labels_ADSB_EMITTER_TYPE = map[ADSB_EMITTER_TYPE]string{ | |||
|
|||
// MarshalText implements the encoding.TextMarshaler interface. | |||
func (e ADSB_EMITTER_TYPE) MarshalText() ([]byte, error) { | |||
if l, ok := labels_ADSB_EMITTER_TYPE[e]; ok { | |||
return []byte(l), nil | |||
var names []string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this changed? This enum is not a bit mask, so these changes to the MarshalText and UnmashalText methods return all the types whose values match the mask, not the single expected string.
For example, I would expect this test to pass:
assert.Equal(t, "ADSB_EMITTER_TYPE_ROTOCRAFT", common.ADSB_EMITTER_TYPE_ROTOCRAFT.String())
But it fails:
-ADSB_EMITTER_TYPE_ROTOCRAFT
+ADSB_EMITTER_TYPE_ROTOCRAFT | ADSB_EMITTER_TYPE_SMALL | ADSB_EMITTER_TYPE_HIGH_VORTEX_LARGE | ADSB_EMITTER_TYPE_HEAVY | ADSB_EMITTER_TYPE_LARGE | ADSB_EMITTER_TYPE_LIGHT | ADSB_EMITTER_TYPE_HIGHLY_MANUV | ADSB_EMITTER_TYPE_NO_INFO
Bug while calling the
MarshalText
method for enumExample:
(common.MAV_SYS_STATUS_SENSOR_3D_GYRO | common.MAV_SYS_STATUS_SENSOR_3D_ACCEL).MarshalText()
an error occurs because this value is not in labels_{{ENUM}}
I propose to return the hex value of the number.