Skip to content

Commit

Permalink
feat: add UUID, date, and datetime helpers for terraform usage
Browse files Browse the repository at this point in the history
  • Loading branch information
rmkeezer committed Mar 3, 2021
1 parent f11161b commit 271a2ea
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions v5/core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

validator "gopkg.in/go-playground/validator.v9"
"github.com/go-openapi/strfmt"
)

// Validate is a shared validator instance used to perform validation of structs.
Expand Down Expand Up @@ -109,6 +110,11 @@ func Float64Ptr(literal float64) *float64 {
return &literal
}

// UUIDPtr returns a pointer to strfmt.UUID literal.
func UUIDPtr(literal strfmt.UUID) *strfmt.UUID {
return &literal
}

// IsJSONMimeType Returns true iff the specified mimeType value represents a
// "JSON" mimetype.
func IsJSONMimeType(mimeType string) bool {
Expand Down Expand Up @@ -182,6 +188,27 @@ func GetCurrentTime() int64 {
return time.Now().Unix()
}

// ParseDate parses the specified RFC3339 full-date string (YYYY-MM-DD) and returns a strfmt.Date instance.
func ParseDate(dateString string) (fmtDate strfmt.Date, err error) {
formattedTime, err := time.Parse(strfmt.RFC3339FullDate, dateString)
var formattedDate strfmt.Date
if err != nil {
return formattedDate, err
}
formattedDate = strfmt.Date(formattedTime)
return formattedDate, nil
}

// ParseDateTime parses the specified date-time string and returns a strfmt.DateTime instance.
func ParseDateTime(dateString string) (fmtDTime strfmt.DateTime, err error) {
var formattedDateTime strfmt.DateTime
formattedDateTime, err = strfmt.ParseDateTime(dateString)
if err != nil {
return formattedDateTime, err
}
return formattedDateTime, nil
}

// ConvertSlice Marshals 'slice' to a json string, performs
// string manipulation on the resulting string, and converts
// the string to a '[]string'. If 'slice' is nil, not a 'slice' type,
Expand Down
24 changes: 24 additions & 0 deletions v5/core/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ func TestPointers(t *testing.T) {

var float64Var = float64(23)
assert.Equal(t, &float64Var, Float64Ptr(float64Var))

var uuidVar = strfmt.UUID("12345678-1234-1234-1234-123456123456")
assert.Equal(t, &uuidVar, UUIDPtr(uuidVar))
}

func TestConvertSliceFloat64(t *testing.T) {
Expand Down Expand Up @@ -318,6 +321,27 @@ func TestConvertSliceByteArray(t *testing.T) {
assert.Empty(t, convertedSlice)
}

func TestDateTimeUtil(t *testing.T) {
dateVar := strfmt.Date(time.Now())
fmtDate, err := ParseDate(dateVar.String())
assert.Nil(t, err)
assert.Equal(t, dateVar.String(), fmtDate.String())

fmtDate, err = ParseDate("not a date")
assert.Nil(t, fmtDate)
assert.NotNil(t, err)

dateTimeVar := strfmt.DateTime(time.Now())
var fmtDTime strfmt.DateTime
fmtDTime, err = ParseDateTime(dateTimeVar.String())
assert.Nil(t, err)
assert.Equal(t, dateTimeVar.String(), fmtDTime.String())

fmtDTime, err = ParseDateTime("not a datetime")
assert.Nil(t, fmtDTime)
assert.NotNil(t, err)
}

func TestConvertSliceDate(t *testing.T) {
date1 := strfmt.Date(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC))
date2 := strfmt.Date(time.Date(2020, time.November, 10, 23, 0, 0, 0, time.UTC))
Expand Down

0 comments on commit 271a2ea

Please sign in to comment.