Skip to content

Commit

Permalink
Merge pull request #78 from DIMO-Network/as-3064-update-ruptela-conve…
Browse files Browse the repository at this point in the history
…rsion-to-ignore-max-values

As 3064 update ruptela conversion to ignore max values
  • Loading branch information
KevinJoiner authored Oct 16, 2024
2 parents f0f254b + 07cb2e8 commit d1a26a1
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 32 deletions.
11 changes: 9 additions & 2 deletions pkg/ruptela/codegen/functions.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const bitsInByte = 8
func Convert{{ $oid }}(rawValue string) (float64, error) {
const byteSize = {{ $rupSig.Size }}
const offset = float64({{ $rupSig.Offset }})
const nan = 1 << (byteSize*bitsInByte) - 1
const maxSize = 1 << (byteSize*bitsInByte) - 1
const multiplier = float64({{ $rupSig.Multiplier }})
{{ if eq $rupSig.Type "Signed int." -}}
rawInt, err := strconv.ParseInt(rawValue, 16, 64)
Expand All @@ -23,29 +23,36 @@ func Convert{{ $oid }}(rawValue string) (float64, error) {
if err != nil {
return 0, fmt.Errorf("could not parse uint: %w", err)
}
if rawInt == nan {

// Check if the value is equal to the maximum value for the given size.
if rawInt == maxSize {
return 0, errNotFound
}

{{ end -}}
{{ if $rupSig.ErrorSet -}}
{{ $sign := "uint64" }}
// Check if the value is in the error set.
{{ if eq $rupSig.Type "Signed int." -}} {{ $sign := "int64" }} {{ end -}}
if slices.Contains([]{{ $sign }}{ {{ range $_, $val := $rupSig.ErrorSet }}{{$val}}, {{end}} }, rawInt) {
return 0, errNotFound
}
{{ end -}}
{{ if $rupSig.ErrorRange -}}
// Check if the value is within the error range.
if {{ $rupSig.ErrorRange.Min }} <= rawInt && rawInt <= {{ $rupSig.ErrorRange.Max }} {
return 0, errNotFound
}
{{ end -}}

{{ if $rupSig.MinBig -}}
// Check if the value is less than the minimum value.
if rawInt < {{ bigText $rupSig.MinBig }} {
return 0, errNotFound
}
{{ end -}}
{{ if $rupSig.MaxBig -}}
// Check if the value is greater than the maximum value.
if rawInt > {{ bigText $rupSig.MaxBig }} {
return 0, errNotFound
}
Expand Down
Loading

0 comments on commit d1a26a1

Please sign in to comment.