Replies: 4 comments
-
|
Hi @GgFred, thanks for reaching out. I believe formatting string the way you wanted is presentation logic, different programs have different intents for how the data should be presented. For example, when printing scaled value, different program might expect to print 4 decimal points instead of 2, some programs might expect to print with "g" flag instead of "f", and other expect raw value, it really depends on the intention of the users.
The fitconv and all programs under cmd folder are standalone programs and are not part of this library to be used for import. They are more like proof-of-concept or example of programs build using this library, they are useful on their own way. And they are not guaranteed to be stable or even stay in this repository as stated here: Lines 5 to 8 in 4f0dd1f
String() method printing only the Value might be ambiguous IMO, users might expect to print all the struct's fields, no? While I open for a feature request, I want to keep this library simple for maintenance as well. My suggestion will be to copy the unexported code as you see fit into your code base and maintain it separately. Let me know if you have more questions. Thanks! |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the detailed response! I understand the reasoning about presentation logic. However, the part I'd find most valuable to have exported is specifically the scale/offset handling. Having it built into the library would make it automatic and prevent users from accidentally forgetting to apply it — which is an easy mistake to make when working with FIT fields. That said, I'll follow your suggestion and maintain my own copy for now. Thanks again! |
Beta Was this translation helpful? Give feedback.
-
|
I get that can be tricky, after spending quite a while writing programs using this library and making my share of mistakes, I’ve come to the conclusion that when working directly with FIT fields, I’d expect users to have at least a basic understanding of the protocol and knowing that FIT fields produced by the decoder will always hold raw/unscaled value. That being said, I’ve created a few helpers to make things a bit easier. My another suggestion is to try using this building block (?) import (
"fmt"
"strconv"
"github.com/muktihari/fit/kit/datetime"
"github.com/muktihari/fit/kit/scaleoffset"
)
func formatValue(field proto.Field) string {
val := scaleoffset.ApplyAny(field.Value, field.Scale, field.Offset)
switch x := val.(type) {
case float64:
return strconv.FormatFloat(x, 'f', 2, 64) // 0.12
case []float64:
var buf = []byte{'['}
for i, v := range x {
buf = strconv.AppendFloat(buf, v, 'f', 2, 64)
if i < len(x)-1 {
buf = append(buf, ' ')
}
}
buf = append(buf, ']')
return string(buf) // [0.12 0.34]
case uint32:
if field.Type == profile.DateTime || field.Type == profile.LocalDateTime {
return datetime.ToTime(x).Format(time.RFC3339)
}
return fmt.Sprintf("%v", x)
default:
return fmt.Sprintf("%v", x)
}
}I’ve considered having |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the detailed explanation, this clarifies the design choices quite well. The helper approach makes sense, and the formatValue example is a good starting point. One possible extension could be to make it slightly more flexible, e.g.: That would allow callers to control formatting concerns (precision, time layout) without re-implementing the logic on their side. Regarding the expectation around protocol knowledge, I agree in principle. That said, in practice, some users may interact with FIT data in a more opportunistic way (e.g. integrating into an existing pipeline or addressing a time-constrained requirement) without having a full understanding of the protocol semantics upfront. In those cases, having slightly higher-level helpers can help bridge the gap. Appreciate the insight—very helpful overall. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I'm working on a project that reads FIT files and I need to display
proto.Fieldvalues as formatted strings (applying scale/offset, handling arrays, etc.).For now, I ended up writing my own version:
But this is essentially duplicating logic that already exists in the codebase. I'd love to avoid maintaining a separate copy.
Maybe the format() and concat() functions in cmd/fitconv/fitcsv/formatter.go are better, but they are unexported.
Would you consider either:
format()andconcat()(or a subset) so they can be reused by library consumers?String()method onproto.Field(or a helper in a public package) that handles scale, offset, and type-aware formatting?But this is essentially duplicating logic that already exists in the codebase. I'd love to avoid maintaining a separate copy.
What do you think? Happy to contribute a PR if you're open to it.
Thanks for the great library!
Beta Was this translation helpful? Give feedback.
All reactions