Skip to content

Commit

Permalink
fix timestamp and duration literal extraction (#218)
Browse files Browse the repository at this point in the history
* fixed literal extraction for timestamp and duration types - was previously returning protobufs instead of native golang types

Signed-off-by: Daniel Rammer <daniel@union.ai>

* fixed lint error with imports being non-alphabetical

Signed-off-by: Daniel Rammer <daniel@union.ai>
  • Loading branch information
hamersaw committed Sep 1, 2021
1 parent 60c419e commit 4106c65
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions flyteidl/clients/go/coreutils/extract_literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func ExtractFromLiteral(literal *core.Literal) (interface{}, error) {
scalarPrimitiveBoolean := scalarPrimitive.Boolean
return scalarPrimitiveBoolean, nil
case *core.Primitive_Datetime:
scalarPrimitiveDateTime := scalarPrimitive.Datetime
scalarPrimitiveDateTime := scalarPrimitive.Datetime.AsTime()
return scalarPrimitiveDateTime, nil
case *core.Primitive_Duration:
scalarPrimitiveDuration := scalarPrimitive.Duration
scalarPrimitiveDuration := scalarPrimitive.Duration.AsDuration()
return scalarPrimitiveDuration, nil
default:
return nil, fmt.Errorf("unsupported literal scalar primitive type %T", scalarValue)
Expand Down
19 changes: 19 additions & 0 deletions flyteidl/clients/go/coreutils/extract_literal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package coreutils

import (
"testing"
"time"

"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"

Expand All @@ -21,6 +22,24 @@ func TestFetchLiteral(t *testing.T) {
assert.Equal(t, "test_string", val)
})

t.Run("Timestamp", func(t *testing.T) {
now := time.Now().UTC()
lit, err := MakeLiteral(now)
assert.NoError(t, err)
val, err := ExtractFromLiteral(lit)
assert.NoError(t, err)
assert.Equal(t, now, val)
})

t.Run("Duration", func(t *testing.T) {
duration := time.Second * 10
lit, err := MakeLiteral(duration)
assert.NoError(t, err)
val, err := ExtractFromLiteral(lit)
assert.NoError(t, err)
assert.Equal(t, duration, val)
})

t.Run("Array", func(t *testing.T) {
lit, err := MakeLiteral([]interface{}{1, 2, 3})
assert.NoError(t, err)
Expand Down

0 comments on commit 4106c65

Please sign in to comment.