Skip to content

Commit

Permalink
chore: fitgen's mesgdef: simplify builder and only create array as ne…
Browse files Browse the repository at this point in the history
…eded (#40)
  • Loading branch information
muktihari authored Dec 11, 2023
1 parent 07f97d1 commit 0f23bef
Show file tree
Hide file tree
Showing 105 changed files with 2,557 additions and 2,167 deletions.
27 changes: 1 addition & 26 deletions internal/cmd/fitgen/profile/mesgdef/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func (b *mesgdefBuilder) Build() ([]builder.Data, error) {
Name: strutil.ToTitle(mesg.Fields[i].Name),
String: field.Name,
Type: transformType(field.Type, field.Array),
InvalidValue: b.baseTypeInvalid(strutil.ToTitle(b.baseTypeMapByProfileType[field.Type]), field.Array),
AssignedValue: b.transformValue(field.Num, field.Type, field.Array),
Comment: field.Comment,
}
Expand All @@ -105,13 +104,9 @@ func (b *mesgdefBuilder) Build() ([]builder.Data, error) {
}

fields = append(fields, f)
if strings.HasPrefix(f.InvalidValue, "basetype") {
if strings.HasPrefix(f.Type, "basetype") {
imports["github.com/muktihari/fit/profile/basetype"] = struct{}{}
}

if strings.HasPrefix(f.InvalidValue, "math") {
imports["math"] = struct{}{}
}
}

data := Data{
Expand All @@ -133,26 +128,6 @@ func (b *mesgdefBuilder) Build() ([]builder.Data, error) {
return dataBuilders, nil
}

func (b *mesgdefBuilder) baseTypeInvalid(_type, array string) string {
if strings.ToLower(_type) == "bool" {
return "false"
}

if array == "" {
if strings.HasPrefix(_type, "Float32") {
return fmt.Sprintf("math.Float32frombits(basetype.%sInvalid)", _type)
}

if strings.HasPrefix(_type, "Float64") {
return fmt.Sprintf("math.Float64frombits(basetype.%sInvalid)", _type)
}

return fmt.Sprintf("basetype.%sInvalid", _type)
}

return "nil"
}

func transformType(name, array string) string {
isBaseType := make(map[string]string)
for _, bt := range basetype.List() {
Expand Down
23 changes: 14 additions & 9 deletions internal/cmd/fitgen/profile/mesgdef/mesgdef.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ func New{{ .Name }}(mesg proto.Message) *{{ .Name }} {
return nil
}

vals := [256]any{ // Mark all values as invalid, replace only when specified.
vals := [...]any{ // nil value will be converted to its corresponding invalid value by typeconv.
{{ range .Fields -}}
{{ .Num }}: {{ .InvalidValue }}, /* {{ .Name }} */
{{ .Num }}: nil, /* {{ .Name }} */
{{ end -}}
}

for i := range mesg.Fields {
if mesg.Fields[i].Value == nil {
continue // keep the invalid value
field := &mesg.Fields[i]
if field.Num >= byte(len(vals)) {
continue
}
vals[mesg.Fields[i].Num] = mesg.Fields[i].Value
vals[field.Num] = field.Value
}

return &{{ .Name }}{
Expand All @@ -75,18 +76,22 @@ func (m {{ .Name }}) PutMessage(mesg *proto.Message) {
return
}

vals := [256]any{
vals := [...]any{
{{ range .Fields -}}
{{ .Num }}: m.{{ .Name }},
{{ end -}}
}

for i := range mesg.Fields {
mesg.Fields[i].Value = vals[mesg.Fields[i].Num]
field := &mesg.Fields[i]
if field.Num >= byte(len(vals)) {
continue
}
field.Value = vals[field.Num]
}

{{- if and (not (eq .Name "FileId")) (not (eq .Name "DeveloperDataId")) (not (eq .Name "FieldDescription")) }}
{{ if and (not (eq .Name "FileId")) (not (eq .Name "DeveloperDataId")) (not (eq .Name "FieldDescription")) }}
mesg.DeveloperFields = m.DeveloperFields
{{ end }}
{{ end -}}
}
{{ end }}
44 changes: 24 additions & 20 deletions profile/mesgdef/accelerometer_data_gen.go

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

36 changes: 20 additions & 16 deletions profile/mesgdef/activity_gen.go

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

30 changes: 17 additions & 13 deletions profile/mesgdef/ant_channel_id_gen.go

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

32 changes: 18 additions & 14 deletions profile/mesgdef/ant_rx_gen.go

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

Loading

0 comments on commit 0f23bef

Please sign in to comment.