Skip to content

Commit

Permalink
Merge pull request #76 from strongdm/native-go-datetime-duration-conv…
Browse files Browse the repository at this point in the history
…erters

types: add go native conversion methods to several types
  • Loading branch information
philhassey authored Dec 5, 2024
2 parents 38eae78 + 84ee8af commit 96529dc
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ var ErrDecimal = fmt.Errorf("error parsing decimal value")
var ErrDuration = fmt.Errorf("error parsing duration value")
var ErrIP = fmt.Errorf("error parsing ip value")
var ErrNotComparable = fmt.Errorf("incompatible types in comparison")
var ErrDurationRange = fmt.Errorf("duration out of range")
5 changes: 5 additions & 0 deletions types/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ func (a Datetime) Milliseconds() int64 {
return a.value
}

// Time returns the UTC time.Time representation of a Datetime.
func (a Datetime) Time() time.Time {
return time.UnixMilli(a.value).UTC()
}

func (v Datetime) hash() uint64 {
return uint64(v.value)
}
8 changes: 8 additions & 0 deletions types/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ func TestDatetime(t *testing.T) {
testutil.Equals(t, one.Milliseconds(), two.Milliseconds())
})

t.Run("Time", func(t *testing.T) {
t.Parallel()
in := types.NewDatetime(time.UnixMilli(42))
got := in.Time()
want := time.UnixMilli(42).UTC()
testutil.Equals(t, got, want)
})

t.Run("Equal", func(t *testing.T) {
t.Parallel()
one := types.NewDatetimeFromMillis(1)
Expand Down
6 changes: 6 additions & 0 deletions types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ func (d Decimal) MarshalJSON() ([]byte, error) {
})
}

// Float returns a float64 representation of a Decimal. Warning: some precision
// may be lost during this conversion.
func (d Decimal) Float() float64 {
return float64(d.value) / decimalPrecision
}

func (d Decimal) hash() uint64 {
return uint64(d.value)
}
9 changes: 9 additions & 0 deletions types/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,15 @@ func TestDecimal(t *testing.T) {
}
})

t.Run("Float", func(t *testing.T) {
t.Parallel()
in, err := types.NewDecimalFromFloat(42.42)
testutil.OK(t, err)
got := in.Float()
want := 42.42
testutil.Equals(t, got, want)
})

t.Run("MarshalCedar", func(t *testing.T) {
t.Parallel()
testutil.Equals(
Expand Down
12 changes: 12 additions & 0 deletions types/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,18 @@ func (v Duration) ToMilliseconds() int64 {
return v.value
}

// Duration returns a time.Duration representation of a Duration. An error
// is returned if the duration cannot be converted to a time.Duration.
func (v Duration) Duration() (time.Duration, error) {
if v.value > math.MaxInt64/1000 {
return 0, internal.ErrDurationRange
}
if v.value < math.MinInt64/1000 {
return 0, internal.ErrDurationRange
}
return time.Millisecond * time.Duration(v.value), nil
}

func (v Duration) hash() uint64 {
return uint64(v.value)
}
24 changes: 24 additions & 0 deletions types/duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types_test
import (
"encoding/json"
"fmt"
"math"
"testing"
"time"

Expand Down Expand Up @@ -87,6 +88,29 @@ func TestDuration(t *testing.T) {
testutil.Equals(t, one.ToMilliseconds(), two.ToMilliseconds())
})

t.Run("Duration", func(t *testing.T) {
t.Parallel()
tests := []struct {
name string
in types.Duration
out time.Duration
err func(testutil.TB, error)
}{
{"ok", types.NewDuration(time.Millisecond * 42), time.Millisecond * 42, testutil.OK},
{"maxPlusOne", types.NewDurationFromMillis(math.MaxInt64/1000 + 1), 0, testutil.Error},
{"minMinusOne", types.NewDurationFromMillis(math.MinInt64/1000 - 1), 0, testutil.Error},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
out, err := tt.in.Duration()
testutil.Equals(t, out, tt.out)
tt.err(t, err)
})
}
})

t.Run("Equal", func(t *testing.T) {
t.Parallel()
one := types.NewDurationFromMillis(1)
Expand Down

0 comments on commit 96529dc

Please sign in to comment.