Skip to content

Commit

Permalink
feat: mesgdef now track fields from component expansion (#68)
Browse files Browse the repository at this point in the history
* feat: mesgdef now tracks fields from component expansion

* generate: mesgdef

* chore: clean up code
  • Loading branch information
muktihari authored Dec 26, 2023
1 parent 9f5fdbb commit 88da702
Show file tree
Hide file tree
Showing 17 changed files with 245 additions and 24 deletions.
64 changes: 50 additions & 14 deletions internal/cmd/fitgen/profile/mesgdef/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type mesgdefBuilder struct {
goTypesByBaseTypes map[string]string // (k -> v) enum -> byte
goTypesByProfileTypes map[string]string // (k -> v) typedef.DateTime -> uint32
baseTypeMapByProfileType map[string]string // (k -> v) enum -> enum, typedef.DateTime -> uint32

fieldByMesgNameByFieldName map[string]map[string]parser.Field
}

func NewBuilder(path, version string, message []parser.Message, types []parser.Type) builder.Builder {
Expand All @@ -41,14 +43,15 @@ func NewBuilder(path, version string, message []parser.Message, types []parser.T
return &mesgdefBuilder{
template: template.Must(template.New("main").
ParseFiles(filepath.Join(cd, "mesgdef.tmpl"))),
templateExec: "mesgdef",
path: filepath.Join(path, "profile", "mesgdef"),
sdkVersion: version,
messages: message,
types: types,
goTypesByBaseTypes: make(map[string]string),
goTypesByProfileTypes: make(map[string]string),
baseTypeMapByProfileType: make(map[string]string),
templateExec: "mesgdef",
path: filepath.Join(path, "profile", "mesgdef"),
sdkVersion: version,
messages: message,
types: types,
goTypesByBaseTypes: make(map[string]string),
goTypesByProfileTypes: make(map[string]string),
baseTypeMapByProfileType: make(map[string]string),
fieldByMesgNameByFieldName: make(map[string]map[string]parser.Field),
}
}

Expand All @@ -68,13 +71,41 @@ func (b *mesgdefBuilder) populateLookupData() {
b.goTypesByProfileTypes[_type.Name] = b.goTypesByBaseTypes[_type.BaseType]
b.baseTypeMapByProfileType[_type.Name] = _type.BaseType
}

for _, mesg := range b.messages {
b.fieldByMesgNameByFieldName[mesg.Name] = make(map[string]parser.Field)
for _, field := range mesg.Fields {
b.fieldByMesgNameByFieldName[mesg.Name][field.Name] = field
}
}
}

func (b *mesgdefBuilder) Build() ([]builder.Data, error) {
b.once.Do(func() { b.populateLookupData() })

dataBuilders := make([]builder.Data, 0, len(b.messages))
for _, mesg := range b.messages {
canExpandMap := map[string]byte{}
var maxFieldExpandNum byte
for _, field := range mesg.Fields {
for _, component := range field.Components {
ref := b.fieldByMesgNameByFieldName[mesg.Name][component]
canExpandMap[ref.Name] = ref.Num
if ref.Num > maxFieldExpandNum {
maxFieldExpandNum = ref.Num
}
}
for _, subfield := range field.SubFields {
for _, component := range subfield.Components {
ref := b.fieldByMesgNameByFieldName[mesg.Name][component]
canExpandMap[ref.Name] = ref.Num
if ref.Num > maxFieldExpandNum {
maxFieldExpandNum = ref.Num
}
}
}
}

dataBuilder := builder.Data{
Template: b.template,
TemplateExec: b.templateExec,
Expand Down Expand Up @@ -104,6 +135,10 @@ func (b *mesgdefBuilder) Build() ([]builder.Data, error) {
Comment: field.Comment,
}

if _, ok := canExpandMap[field.Name]; ok {
f.CanExpand = true
}

f.ComparableValue = b.transformComparableValue(field.Type, field.Array, f.PrimitiveValue)

if field.Array == "" && b.baseTypeMapByProfileType[field.Type] == "string" {
Expand Down Expand Up @@ -139,12 +174,13 @@ func (b *mesgdefBuilder) Build() ([]builder.Data, error) {
}

data := Data{
SDKVersion: b.sdkVersion,
Package: "mesgdef",
Imports: []string{},
Name: strutil.ToTitle(mesg.Name),
Fields: fields,
MaxFieldNum: maxFieldNum + 1,
SDKVersion: b.sdkVersion,
Package: "mesgdef",
Imports: []string{},
Name: strutil.ToTitle(mesg.Name),
Fields: fields,
MaxFieldNum: maxFieldNum + 1,
MaxFieldExpandNum: maxFieldExpandNum + 1,
}

for k := range imports {
Expand Down
14 changes: 8 additions & 6 deletions internal/cmd/fitgen/profile/mesgdef/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
package mesgdef

type Data struct {
SDKVersion string
Package string
Imports []string
Name string
Fields []Field
MaxFieldNum byte
SDKVersion string
Package string
Imports []string
Name string
Fields []Field
MaxFieldNum byte
MaxFieldExpandNum byte
}

type Field struct {
Expand All @@ -23,4 +24,5 @@ type Field struct {
ComparableValue string
InvalidValue string
Comment string
CanExpand bool
}
27 changes: 23 additions & 4 deletions internal/cmd/fitgen/profile/mesgdef/mesgdef.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,21 @@ type {{ .Name }} struct {
// Developer Fields are dynamic, can't be mapped as struct's fields.
// [Added since protocol version 2.0]
DeveloperFields []proto.DeveloperField
{{- end -}}
{{ end -}}

{{ if gt .MaxFieldExpandNum 1 }}

IsExpandedFields [{{ .MaxFieldExpandNum }}]bool // Used for tracking expanded fields, field.Num as index.
{{ end -}}
}

// New{{ .Name }} creates new {{ .Name }} struct based on given mesg.
// If mesg is nil, it will return {{ .Name }} with all fields being set to its corresponding invalid value.
func New{{ .Name }}(mesg *proto.Message) *{{ .Name }} {
vals := [{{ .MaxFieldNum }}]any{}
{{ if gt .MaxFieldExpandNum 1 -}}
isExpandedFields := [{{ .MaxFieldExpandNum }}]bool{}
{{ end }}

{{ if and (not (eq .Name "FileId")) (not (eq .Name "DeveloperDataId")) (not (eq .Name "FieldDescription")) -}}
var developerFields []proto.DeveloperField
Expand All @@ -45,6 +53,11 @@ func New{{ .Name }}(mesg *proto.Message) *{{ .Name }} {
if mesg.Fields[i].Num >= byte(len(vals)) {
continue
}
{{ if gt $.MaxFieldExpandNum 1 -}}
if mesg.Fields[i].Num < byte(len(isExpandedFields)) {
isExpandedFields[mesg.Fields[i].Num] = mesg.Fields[i].IsExpandedField
}
{{ end -}}
vals[mesg.Fields[i].Num] = mesg.Fields[i].Value
}
{{- if and (not (eq .Name "FileId")) (not (eq .Name "DeveloperDataId")) (not (eq .Name "FieldDescription")) }}
Expand All @@ -60,6 +73,9 @@ func New{{ .Name }}(mesg *proto.Message) *{{ .Name }} {

{{- if and (not (eq .Name "FileId")) (not (eq .Name "DeveloperDataId")) (not (eq .Name "FieldDescription")) }}
DeveloperFields: developerFields,
{{ end }}
{{ if gt .MaxFieldExpandNum 1 -}}
IsExpandedFields: isExpandedFields,
{{ end -}}
}
}
Expand All @@ -74,9 +90,12 @@ func (m *{{ .Name }}) ToMesg(fac Factory) proto.Message {

{{ range .Fields -}}
if {{ .ComparableValue }} != {{ .InvalidValue }} {
field := fac.CreateField(mesg.Num, {{ .Num }})
field.Value = {{ .PrimitiveValue }}
fields = append(fields, field)
field := fac.CreateField(mesg.Num, {{ .Num }})
field.Value = {{ .PrimitiveValue }}
{{ if eq .CanExpand true -}}
field.IsExpandedField = m.IsExpandedFields[{{ .Num }}]
{{ end -}}
fields = append(fields, field)
}
{{ end }}

Expand Down
10 changes: 10 additions & 0 deletions profile/mesgdef/ant_rx_gen.go

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

10 changes: 10 additions & 0 deletions profile/mesgdef/ant_tx_gen.go

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

19 changes: 19 additions & 0 deletions profile/mesgdef/event_gen.go

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

Loading

0 comments on commit 88da702

Please sign in to comment.