-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: clean up fitgen + fix mesgdef (#211)
* chore: clean up fitgen code * fix: mesgdef on dynamic field accessor * fitgen generate code
- Loading branch information
Showing
132 changed files
with
8,883 additions
and
8,501 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package lookup | ||
|
||
import ( | ||
"github.com/muktihari/fit/internal/cmd/fitgen/parser" | ||
"github.com/muktihari/fit/profile/basetype" | ||
) | ||
|
||
type Lookup struct { | ||
fieldsByMesgName map[string][]parser.Field // fields is small, slice should be sufficient. | ||
profiles map[string]profile | ||
} | ||
|
||
type profile struct { | ||
goType string | ||
baseType basetype.BaseType | ||
valuesByName map[string]string | ||
} | ||
|
||
func NewLookup(types []parser.Type, messages []parser.Message) *Lookup { | ||
l := &Lookup{ | ||
fieldsByMesgName: make(map[string][]parser.Field), | ||
profiles: make(map[string]profile), | ||
} | ||
l.populateLookupData(types, messages) | ||
return l | ||
} | ||
|
||
func (l *Lookup) populateLookupData(types []parser.Type, messages []parser.Message) { | ||
l.populateProfileLookup(types) | ||
l.populateMessageLookup(messages) | ||
} | ||
|
||
func (l *Lookup) populateProfileLookup(types []parser.Type) { | ||
// additional profile type, mark it as enum | ||
l.profiles["bool"] = profile{ | ||
baseType: basetype.Enum, | ||
goType: "bool", | ||
} | ||
|
||
// map fit_base_type to our defined type "basetype.BaseType" | ||
l.profiles["fit_base_type"] = profile{ | ||
baseType: basetype.Uint8, | ||
goType: "basetype.BaseType", | ||
} | ||
|
||
basetypes := make(map[string]basetype.BaseType) | ||
|
||
// map basetype as profile type | ||
for _, bt := range basetype.List() { | ||
l.profiles[bt.String()] = profile{ | ||
goType: bt.GoType(), | ||
baseType: bt, | ||
} | ||
basetypes[bt.String()] = bt | ||
} | ||
|
||
for _, typ := range types { | ||
p := profile{ | ||
goType: basetypes[typ.BaseType].GoType(), | ||
baseType: basetype.FromString(typ.BaseType), | ||
valuesByName: make(map[string]string), | ||
} | ||
|
||
for _, val := range typ.Values { | ||
p.valuesByName[val.Name] = val.Value | ||
} | ||
|
||
l.profiles[typ.Name] = p | ||
} | ||
} | ||
|
||
func (l *Lookup) populateMessageLookup(messages []parser.Message) { | ||
for _, mesg := range messages { | ||
l.fieldsByMesgName[mesg.Name] = mesg.Fields | ||
} | ||
} | ||
|
||
func (l *Lookup) BaseType(profileType string) basetype.BaseType { | ||
return l.profiles[profileType].baseType | ||
} | ||
|
||
func (l *Lookup) GoType(profileType string) string { | ||
return l.profiles[profileType].goType | ||
} | ||
|
||
func (l *Lookup) TypeValue(profileType string, valueName string) string { | ||
return l.profiles[profileType].valuesByName[valueName] | ||
} | ||
|
||
func (l *Lookup) FieldByNum(mesgName string, fieldNum byte) parser.Field { | ||
for _, field := range l.fieldsByMesgName[mesgName] { | ||
if field.Num == fieldNum { | ||
return field | ||
} | ||
} | ||
return parser.Field{Num: 255, Name: "unknown"} | ||
} | ||
|
||
func (l *Lookup) FieldByName(mesgName string, fieldName string) parser.Field { | ||
for _, field := range l.fieldsByMesgName[mesgName] { | ||
if field.Name == fieldName { | ||
return field | ||
} | ||
} | ||
return parser.Field{Num: 255, Name: "unknown"} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.