Skip to content

Commit

Permalink
Merge pull request #67 from ekristen/feat-props-time
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen authored Jul 9, 2024
2 parents 64c441c + fb9a96a commit 71dd5d0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pkg/types/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"reflect"
"strings"
"time"
)

// Properties is a map of key-value pairs.
Expand Down Expand Up @@ -51,7 +52,7 @@ func (p Properties) Get(key string) string {
}

// Set sets a key-value pair in the Properties map.
func (p Properties) Set(key string, value interface{}) Properties {
func (p Properties) Set(key string, value interface{}) Properties { //nolint:gocyclo
if value == nil {
return p
}
Expand All @@ -62,23 +63,33 @@ func (p Properties) Set(key string, value interface{}) Properties {
return p
}
p[key] = *v
case string:
p[key] = v
case []byte:
p[key] = string(v)
case *bool:
if v == nil {
return p
}
p[key] = fmt.Sprint(*v)
case bool:
p[key] = fmt.Sprint(v)
case *int64:
if v == nil {
return p
}
p[key] = fmt.Sprint(*v)
case int64:
p[key] = fmt.Sprint(v)
case *int:
if v == nil {
return p
}
p[key] = fmt.Sprint(*v)
case int:
p[key] = fmt.Sprint(v)
case time.Time:
p[key] = v.Format(time.RFC3339)
default:
// Fallback to Stringer interface. This produces gibberish on pointers,
// but is the only way to avoid reflection.
Expand Down Expand Up @@ -233,7 +244,9 @@ func (p Properties) SetFromStruct(data interface{}) Properties { //nolint:funlen

switch value.Kind() {
case reflect.Struct:
// do nothing
if value.Type().String() == "time.Time" {
p.SetWithPrefix(prefix, name, value.Interface())
}
case reflect.Map:
for _, key := range value.MapKeys() {
val := value.MapIndex(key)
Expand Down
37 changes: 37 additions & 0 deletions pkg/types/properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types_test
import (
"fmt"
"testing"
"time"

"github.com/gotidy/ptr"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -404,6 +405,18 @@ func TestPropertiesSetFromStruct(t *testing.T) {
Region string
}

type testStruct11 struct {
Name string
CreatedTime time.Time
DeletedTime *time.Time
}

type testStruct12 struct {
Name string
CreatedTime time.Time `property:"prefix=created"`
DeletedTime *time.Time `property:"prefix=deleted"`
}

cases := []struct {
name string
s interface{}
Expand Down Expand Up @@ -525,6 +538,30 @@ func TestPropertiesSetFromStruct(t *testing.T) {
},
want: types.NewProperties().Set("Region", "us-west-2"),
},
{
name: "time",
s: testStruct11{
Name: "Alice",
CreatedTime: time.Date(2021, 1, 5, 10, 12, 56, 3309, time.UTC),
DeletedTime: ptr.Time(time.Date(2023, 7, 15, 5, 32, 12, 4506, time.UTC)),
},
want: types.NewProperties().
Set("Name", "Alice").
Set("CreatedTime", "2021-01-05T10:12:56Z").
Set("DeletedTime", "2023-07-15T05:32:12Z"),
},
{
name: "time-with-prefix",
s: testStruct12{
Name: "Alice",
CreatedTime: time.Date(2021, 1, 5, 10, 12, 56, 3309, time.UTC),
DeletedTime: ptr.Time(time.Date(2023, 7, 15, 5, 32, 12, 4506, time.UTC)),
},
want: types.NewProperties().
Set("Name", "Alice").
Set("created:CreatedTime", "2021-01-05T10:12:56Z").
Set("deleted:DeletedTime", "2023-07-15T05:32:12Z"),
},
}

for _, tc := range cases {
Expand Down

0 comments on commit 71dd5d0

Please sign in to comment.