Skip to content

Commit

Permalink
cli-fix: fitconv csv-to-fit on parsing fields (#462)
Browse files Browse the repository at this point in the history
* cli-fix: fitconv csv-to-fit on parsing fields

* cli-fix: bug removing field placeholder and handling dev fields
  • Loading branch information
muktihari committed Sep 19, 2024
1 parent 7eb269b commit 5488953
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions cmd/fitconv/fitcsv/csv_to_fit.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ func (c *CSVToFITConv) convert() error {
if err != nil {
return fmt.Errorf("could not create mesg: num: %q (%d): %w", mesgNum, mesgNum, err)
}
if len(mesg.Fields) == 0 && len(mesg.DeveloperFields) == 0 {
continue
}

if mesg.Num == mesgnum.FieldDescription {
c.fieldDescriptions = append(c.fieldDescriptions, mesgdef.NewFieldDescription(&mesg))
Expand Down Expand Up @@ -199,7 +202,7 @@ func (c *CSVToFITConv) createMesg(num typedef.MesgNum, record []string) (proto.M
}

if len(record) < 6 {
return mesg, nil
return proto.Message{}, nil
}

var dynamicFieldRefs []dynamicFieldRef
Expand Down Expand Up @@ -235,7 +238,7 @@ func (c *CSVToFITConv) createMesg(num typedef.MesgNum, record []string) (proto.M
}
v, err := strconv.ParseUint(digits, 10, 8)
if err != nil {
return mesg, fmt.Errorf("could not parse unknown fieldNum %q: %w", fieldName, err)
return proto.Message{}, fmt.Errorf("could not parse unknown fieldNum %q: %w", fieldName, err)
}
fieldNum = byte(v)
recoverableUnknownField = true
Expand All @@ -246,19 +249,19 @@ func (c *CSVToFITConv) createMesg(num typedef.MesgNum, record []string) (proto.M
if ok {
field, err := c.createField(num, fieldNum, strValue, units, recoverableUnknownField)
if err != nil {
return mesg, fmt.Errorf("could not create field: num: %q (%d): %w", fieldName, fieldNum, err)
return proto.Message{}, fmt.Errorf("could not create field: num: %q (%d): %w", fieldName, fieldNum, err)
}
mesg.Fields = append(mesg.Fields, field)
continue
}

devField, err := c.createDeveloperField(fieldName, strValue, units)
if err != nil {
return mesg, fmt.Errorf("could not create developer field: %w", err)
return proto.Message{}, fmt.Errorf("could not create developer field: %w", err)
}
if devField.Value.Type() != proto.TypeInvalid {
mesg.DeveloperFields = append(mesg.DeveloperFields, devField)
return mesg, nil
continue
}

// If the field cannot be found in fieldNumLookup and isn't a valid developer field,
Expand All @@ -273,10 +276,23 @@ func (c *CSVToFITConv) createMesg(num typedef.MesgNum, record []string) (proto.M

for _, ref := range dynamicFieldRefs {
if err := c.revertSubFieldSubtitution(&mesg, ref); err != nil {
return mesg, err
return proto.Message{}, err
}
}

// Remove remaining field placeholders if it can't be subtituted.
var valid int
for i := range mesg.Fields {
if mesg.Fields[i].Name == placeholderField.Name {
continue
}
if i != valid {
mesg.Fields[i], mesg.Fields[valid] = mesg.Fields[valid], mesg.Fields[i]
}
valid++
}
mesg.Fields = mesg.Fields[:valid]

removeExpandedComponents(&mesg)

return mesg, nil
Expand Down Expand Up @@ -423,19 +439,6 @@ func (c *CSVToFITConv) revertSubFieldSubtitution(mesgRef *proto.Message, ref dyn
}
}

// Remove remaining field placeholders if it can't be subtituted.
var valid int
for i := range mesgRef.Fields {
if mesgRef.Fields[i].Name == placeholderField.Name {
continue
}
if i != valid {
mesgRef.Fields[i], mesgRef.Fields[valid] = mesgRef.Fields[valid], mesgRef.Fields[i]
}
valid++
}
mesgRef.Fields = mesgRef.Fields[:valid]

c.unknwonDynamicField++
return nil
}
Expand Down

0 comments on commit 5488953

Please sign in to comment.