Skip to content

Commit

Permalink
cli-fix: fitconv csv-to-fit on parsing fields (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
muktihari authored Sep 19, 2024
1 parent 720cc58 commit 7eb269b
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions cmd/fitconv/fitcsv/csv_to_fit.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ type dynamicFieldRef struct {
value string
}

var placeholderField = proto.Field{FieldBase: &proto.FieldBase{Num: 255, Name: "fitconv_field_placeholder"}}

func (c *CSVToFITConv) createMesg(num typedef.MesgNum, record []string) (proto.Message, error) {
c.col = 0

Expand Down Expand Up @@ -214,7 +216,11 @@ func (c *CSVToFITConv) createMesg(num typedef.MesgNum, record []string) (proto.M
}

var recoverableUnknownField bool
fieldNum, ok := fieldNumLookup[num][fieldName]
var fieldNum byte
var ok bool
if int(num) < len(fieldNumLookup) {
fieldNum, ok = fieldNumLookup[num][fieldName]
}
if !ok {
if strings.HasPrefix(fieldName, factory.NameUnknown) {
digits := strings.Map(func(r rune) rune {
Expand Down Expand Up @@ -262,6 +268,7 @@ func (c *CSVToFITConv) createMesg(num typedef.MesgNum, record []string) (proto.M
name: fieldName,
value: strValue,
})
mesg.Fields = append(mesg.Fields, placeholderField)
}

for _, ref := range dynamicFieldRefs {
Expand Down Expand Up @@ -409,13 +416,26 @@ func (c *CSVToFITConv) revertSubFieldSubtitution(mesgRef *proto.Message, ref dyn
return err
}

mesgRef.Fields = append(mesgRef.Fields[:ref.index+1], mesgRef.Fields[ref.index:]...) // make space for fieldRef
mesgRef.Fields[ref.index] = fieldRef // put field at original index
mesgRef.Fields[ref.index] = fieldRef // replace field placeholder
return nil
}
}
}
}

// 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 7eb269b

Please sign in to comment.