Skip to content

Commit

Permalink
refactor!: change primitive bool to typedef.Bool (#425)
Browse files Browse the repository at this point in the history
* add new type typedef.Bool

* change primitive bool to typedef.Bool

* implement typedef.Bool in mesgdefs

* fitgen: generate mesgdef

* update fitconv regarding typedef.Bool implementation
  • Loading branch information
muktihari authored Sep 13, 2024
1 parent 50d2a35 commit ef61bdb
Show file tree
Hide file tree
Showing 91 changed files with 663 additions and 509 deletions.
6 changes: 3 additions & 3 deletions cmd/fitconv/fitcsv/csv_to_fit.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,11 @@ func parseValue(strValue string, baseType basetype.BaseType, profileType profile
}

if profileType == profile.Bool {
v, err := strconv.ParseBool(strValue)
v, err := strconv.ParseUint(strValue, 0, 8)
if err != nil {
return value, err
}
return proto.Bool(v), nil
return proto.Bool(typedef.Bool(v)), nil
}

var scaledValue float64
Expand Down Expand Up @@ -545,7 +545,7 @@ func packValues(vals []proto.Value) proto.Value {
}
switch vals[0].Type() {
case proto.TypeBool:
values := make([]bool, len(vals))
values := make([]typedef.Bool, len(vals))
for i := range vals {
values[i] = vals[i].Bool()
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/fitconv/fitcsv/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"

"github.com/muktihari/fit/profile/typedef"
"github.com/muktihari/fit/proto"
)

Expand All @@ -22,7 +23,7 @@ import (
func format(val proto.Value) string {
switch val.Type() { // fast path
case proto.TypeBool:
return strconv.FormatBool(val.Bool())
return strconv.FormatUint(uint64(val.Bool()), 10)
case proto.TypeInt8:
return strconv.FormatInt(int64(val.Int8()), 10)
case proto.TypeUint8:
Expand Down Expand Up @@ -54,8 +55,8 @@ func format(val proto.Value) string {
case proto.TypeString:
return val.String()
case proto.TypeSliceBool:
return concat(val.SliceBool(), func(v bool) string {
return strconv.FormatBool(v)
return concat(val.SliceBool(), func(v typedef.Bool) string {
return strconv.FormatUint(uint64(v), 10)
})
case proto.TypeSliceInt8:
return concat(val.SliceInt8(), func(v int8) string {
Expand Down
33 changes: 15 additions & 18 deletions internal/cmd/fitgen/profile/mesgdef/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ func (b *Builder) Build() ([]generator.Data, error) {
// Special case
if isTypeTime(parserField.Type) {
field.IsValidValue = fmt.Sprintf("!m.%s.Before(datetime.Epoch())", field.Name)
} else if parserField.Type == "bool" {
field.IsValidValue = fmt.Sprintf("%s < 2", field.ComparableValue)
} else {
field.IsValidValue = fmt.Sprintf("%s != %s", field.ComparableValue, field.InvalidValue)
}
Expand Down Expand Up @@ -411,7 +413,7 @@ func (b *Builder) transformType(fieldType, fieldArray string, fixedArraySize byt
}

var typ string
if v := b.lookup.BaseType(fieldType).String(); v == fieldType || fieldType == "bool" {
if v := b.lookup.BaseType(fieldType).String(); v == fieldType {
typ = b.lookup.GoType(fieldType)
} else {
typ = fmt.Sprintf("typedef.%s", strutil.ToTitle(fieldType))
Expand Down Expand Up @@ -473,19 +475,6 @@ func (b *Builder) transformPrimitiveValue(fieldName, fieldType, array string) st
return fmt.Sprintf("datetime.ToUint32(m.%s)", fieldName)
}

if b.lookup.BaseType(fieldType).String() == fieldType {
return fmt.Sprintf("m.%s", fieldName) // only for primitive go types.
}

goType := b.lookup.GoType(fieldType)
if goType == "bool" {
return fmt.Sprintf("m.%s", fieldName)
}

if array == "" {
return fmt.Sprintf("%s(m.%s)", goType, fieldName)
}

return fmt.Sprintf("m.%s", fieldName)

}
Expand Down Expand Up @@ -566,15 +555,16 @@ func (b *Builder) transformComparableValue(fieldType, array, primitiveValue stri

func (b *Builder) invalidValueOf(fieldType, array string, fixedArraySize byte) string {
if fieldType == "bool" {
return "false"
return "typedef.BoolInvalid"
}

baseType := b.lookup.BaseType(fieldType).String()

if array != "" {
if fixedArraySize == 0 { // Slice
return "nil"
}

baseType := b.lookup.BaseType(fieldType).String()
baseTypeTitleCase := strutil.ToTitle(baseType)
typ := baseTypeReplacer.Replace(baseTypeTitleCase)
typ = strings.TrimSuffix(typ, "z")
Expand All @@ -589,9 +579,16 @@ func (b *Builder) invalidValueOf(fieldType, array string, fixedArraySize byte) s
),
)
}
if baseType == fieldType || fieldType == "bool" {
return fmt.Sprintf("basetype.%sInvalid",
strutil.ToTitle(b.lookup.BaseType(fieldType).String()))
}

if fieldType == "fit_base_type" {
return "255"
}

return fmt.Sprintf("basetype.%sInvalid",
strutil.ToTitle(b.lookup.BaseType(fieldType).String()))
return fmt.Sprintf("typedef.%sInvalid", strutil.ToTitle(fieldType))
}

func (b *Builder) invalidArrayValueScaled(fixedArraySize byte) string {
Expand Down
46 changes: 19 additions & 27 deletions internal/cmd/fitgen/profile/mesgdef/mesgdef.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -94,37 +94,29 @@ func (m *{{ .Name }}) ToMesg(options *Options) proto.Message {

arr := pool.Get().(*[poolsize]proto.Field)
fields := arr[:0]

mesg := proto.Message{ Num: typedef.MesgNum{{ .Name }} }

{{ range .Fields -}}
{{ if and (eq .Type "bool") (eq .FixedArraySize 0) -}}
{
field := fac.CreateField(mesg.Num, {{ .Num }})
field.Value = {{ .ProtoValue }}
fields = append(fields, field)
}
if {{ .IsValidValue -}} {
{{- if eq .CanExpand true -}} if expanded := m.IsExpandedField({{ .Num }}); !expanded || (expanded && options.IncludeExpandedFields) { {{ end }}
field := fac.CreateField(mesg.Num, {{ .Num }})
{{ if gt .FixedArraySize 0 -}}
{{- /* DO NOT reslice directly from the struct (e.g. m.Velocity[:]), otherwise, we will keep referencing
the struct since we promote the array from the same memory block as the struct. We must copy the array
to local variable, so it will be promoted instead (we only need the local variable to escape to the heap),
only then the struct can be garbage-collected on next GC's cycle. */ -}}
copied := m.{{ .Name }}
field.Value = {{ stringReplace .ProtoValue (printf "m.%s" .Name) "copied[:]" -1 }}
{{ else -}}
if {{ .IsValidValue -}} {
{{- if eq .CanExpand true -}} if expanded := m.IsExpandedField({{ .Num }}); !expanded || (expanded && options.IncludeExpandedFields) { {{ end }}
field := fac.CreateField(mesg.Num, {{ .Num }})
{{ if gt .FixedArraySize 0 -}}
{{- /* DO NOT reslice directly from the struct (e.g. m.Velocity[:]), otherwise, we will keep referencing
the struct since we promote the array from the same memory block as the struct. We must copy the array
to local variable, so it will be promoted instead (we only need the local variable to escape to the heap),
only then the struct can be garbage-collected on next GC's cycle. */ -}}
copied := m.{{ .Name }}
field.Value = {{ stringReplace .ProtoValue (printf "m.%s" .Name) "copied[:]" -1 }}
{{ else -}}
field.Value = {{ .ProtoValue }}
{{ end -}}
{{ if eq .CanExpand true -}}
field.IsExpandedField = expanded
{{ end -}}
fields = append(fields, field)
{{ if eq .CanExpand true -}} } {{ end -}}
}
field.Value = {{ .ProtoValue }}
{{ end -}}
{{ if eq .CanExpand true -}}
field.IsExpandedField = expanded
{{ end -}}
fields = append(fields, field)
{{ if eq .CanExpand true -}} } {{ end -}}
}
{{ end }}

mesg.Fields = make([]proto.Field, len(fields))
Expand Down
6 changes: 3 additions & 3 deletions profile/mesgdef/activity_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion profile/mesgdef/ant_channel_id_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 27 additions & 27 deletions profile/mesgdef/bike_profile_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ef61bdb

Please sign in to comment.