Documentation ¶
Overview ¶
Package narrow provides conversion functionality for narrowing integer types in a safe and generic manner.
These operations will return not only the casted integer, but also a boolean indicating whether the value was safely casted to the receiver type -- e.g. that the sign didn't change through casting, and that the value was not unexpectedly truncated. This can be really useful for cases where data requires explicit integer-widths, and the incoming data may exceed that integer width.
Conversion is done through one of the two following function types:
- `narrow.ToInteger[To](from) (To, bool)` -- converts to the specified int-type
- `narrow.To*(from) (*, bool)` -- convenience functions for converting to explicit integer-width types to avoid needing generics (just calls into the above).
The former's generic-interface allows better composability with other generic functions, whereas the latter allows a more idiomatic and visible call for explicit cases (e.g. `ints.ToInt32(...)`)
Index ¶
- func ToInt[From constraints.Integer](from From) (int, bool)
- func ToInt16[From constraints.Integer](from From) (int16, bool)
- func ToInt32[From constraints.Integer](from From) (int32, bool)
- func ToInt64[From constraints.Integer](from From) (int64, bool)
- func ToInt8[From constraints.Integer](from From) (int8, bool)
- func ToInteger[To constraints.Integer, From constraints.Integer](from From) (To, bool)
- func ToUint[From constraints.Integer](from From) (uint, bool)
- func ToUint16[From constraints.Integer](from From) (uint16, bool)
- func ToUint32[From constraints.Integer](from From) (uint32, bool)
- func ToUint64[From constraints.Integer](from From) (uint64, bool)
- func ToUint8[From constraints.Integer](from From) (uint8, bool)
- func ToUintptr[From constraints.Integer](from From) (uintptr, bool)
Examples ¶
- ToInt (No_narrowing)
- ToInt16 (Narrows)
- ToInt16 (No_narrowing)
- ToInt32 (Narrows)
- ToInt32 (No_narrowing)
- ToInt64 (No_narrowing)
- ToInt8 (Narrows)
- ToInt8 (No_narrowing)
- ToInteger (Narrows)
- ToInteger (No_narrowing)
- ToUint (Narrows)
- ToUint (No_narrowing)
- ToUint16 (Narrows)
- ToUint16 (No_narrowing)
- ToUint32 (Narrows)
- ToUint32 (No_narrowing)
- ToUint64 (Narrows)
- ToUint64 (No_narrowing)
- ToUint8 (Narrows)
- ToUint8 (No_narrowing)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ToInt ¶
func ToInt[From constraints.Integer](from From) (int, bool)
ToInt converts an integer type into an `int`, additionally returning whether the value was converted successfully without any truncation and loss-of-data occurring.
Example (No_narrowing) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := 42 val, ok := narrow.ToInt32(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Converted 42 to int32!
func ToInt16 ¶
func ToInt16[From constraints.Integer](from From) (int16, bool)
ToInt16 converts an integer type into an `int16`, additionally returning whether the value was converted successfully without any truncation and loss-of-data occurring.
Example (Narrows) ¶
package main import ( "fmt" "math" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := math.MaxInt32 val, ok := narrow.ToInt16(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Unable to convert to int16!
Example (No_narrowing) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := 42 val, ok := narrow.ToInt16(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Converted 42 to int16!
func ToInt32 ¶
func ToInt32[From constraints.Integer](from From) (int32, bool)
ToInt32 converts an integer type into an `int32`, additionally returning whether the value was converted successfully without any truncation and loss-of-data occurring.
Example (Narrows) ¶
package main import ( "fmt" "math" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := math.MaxInt64 val, ok := narrow.ToInt32(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Unable to convert to int32!
Example (No_narrowing) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := 42 val, ok := narrow.ToInt32(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Converted 42 to int32!
func ToInt64 ¶
func ToInt64[From constraints.Integer](from From) (int64, bool)
ToInt64 converts an integer type into an `int64`, additionally returning whether the value was converted successfully without any truncation and loss-of-data occurring.
Example (No_narrowing) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := 42 val, ok := narrow.ToInt64(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Converted 42 to int64!
func ToInt8 ¶
func ToInt8[From constraints.Integer](from From) (int8, bool)
ToInt8 converts an integer type into an `int8`, additionally returning whether the value was converted successfully without any truncation and loss-of-data occurring.
Example (Narrows) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := 10_000 val, ok := narrow.ToInt8(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Unable to convert to int8!
Example (No_narrowing) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := 42 val, ok := narrow.ToInt8(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Converted 42 to int8!
func ToInteger ¶
func ToInteger[To constraints.Integer, From constraints.Integer](from From) (To, bool)
ToInteger converts `From` to a `To` type, additionally returning whether the value was converted successfully without any truncation and loss-of-data occurring.
Example (Narrows) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := -1 val, ok := narrow.ToInteger[uint8](from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Unable to convert to uint8!
Example (No_narrowing) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := uint32(42) val, ok := narrow.ToInteger[uint8](from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Converted 42 to uint8!
func ToUint ¶
func ToUint[From constraints.Integer](from From) (uint, bool)
ToUint converts an integer type into an `uint`, additionally returning whether the value was converted successfully without any truncation and loss-of-data occurring.
Example (Narrows) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := -1 val, ok := narrow.ToUint(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Unable to convert to uint!
Example (No_narrowing) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := 42 val, ok := narrow.ToUint(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Converted 42 to uint!
func ToUint16 ¶
func ToUint16[From constraints.Integer](from From) (uint16, bool)
ToUint16 converts an integer type into an `uint16`, additionally returning whether the value was converted successfully without any truncation and loss-of-data occurring.
Example (Narrows) ¶
package main import ( "fmt" "math" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := math.MaxInt32 val, ok := narrow.ToUint16(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Unable to convert to uint16!
Example (No_narrowing) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := 42 val, ok := narrow.ToUint16(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Converted 42 to uint16!
func ToUint32 ¶
func ToUint32[From constraints.Integer](from From) (uint32, bool)
ToUint32 converts an integer type into an `uint32`, additionally returning whether the value was converted successfully without any truncation and loss-of-data occurring.
Example (Narrows) ¶
package main import ( "fmt" "math" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := math.MaxInt64 val, ok := narrow.ToUint32(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Unable to convert to uint32!
Example (No_narrowing) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := 42 val, ok := narrow.ToUint32(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Converted 42 to uint32!
func ToUint64 ¶
func ToUint64[From constraints.Integer](from From) (uint64, bool)
ToUint64 converts an integer type into an `uint64`, additionally returning whether the value was converted successfully without any truncation and loss-of-data occurring.
Example (Narrows) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := -1 val, ok := narrow.ToUint64(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Unable to convert to uint64!
Example (No_narrowing) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := 42 val, ok := narrow.ToUint64(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Converted 42 to uint64!
func ToUint8 ¶
func ToUint8[From constraints.Integer](from From) (uint8, bool)
ToUint8 converts an integer type into an `uint8`, additionally returning whether the value was converted successfully without any truncation and loss-of-data occurring.
Example (Narrows) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := 10_000 val, ok := narrow.ToUint8(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Unable to convert to uint8!
Example (No_narrowing) ¶
package main import ( "fmt" "github.com/verily-src/fhirpath-go/internal/narrow" ) func main() { from := 42 val, ok := narrow.ToUint8(from) if !ok { fmt.Printf("Unable to convert to %T!", val) } else { fmt.Printf("Converted %v to %T!", from, val) } }
Output: Converted 42 to uint8!
Types ¶
This section is empty.