Skip to content

Commit

Permalink
feat: add UUID, date, and datetime helpers for terraform usage (#96)
Browse files Browse the repository at this point in the history
Co-authored-by: Phil Adams <[email protected]>
  • Loading branch information
rmkeezer and padamstx authored Mar 4, 2021
1 parent f11161b commit e651369
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions v5/core/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,17 @@ func init() {
func NormalizeDateTimeUTC(t time.Time) time.Time {
return t.UTC()
}

// 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)
if err == nil {
fmtDate = strfmt.Date(formattedTime)
}
return
}

// ParseDateTime parses the specified date-time string and returns a strfmt.DateTime instance.
func ParseDateTime(dateString string) (strfmt.DateTime, error) {
return strfmt.ParseDateTime(dateString)
}
21 changes: 21 additions & 0 deletions v5/core/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package core
import (
"encoding/json"
"testing"
"time"

"github.com/go-openapi/strfmt"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -159,5 +160,25 @@ func TestModelsDate(t *testing.T) {
roundTripTestDate(t, `{"ws_victory":"2007-10-29"}`, `{"ws_victory":"2007-10-29"}`)
roundTripTestDate(t, `{"ws_victory":"2013-10-31"}`, `{"ws_victory":"2013-10-31"}`)
roundTripTestDate(t, `{"ws_victory":"2018-10-29"}`, `{"ws_victory":"2018-10-29"}`)
}

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.Equal(t, strfmt.Date{}, 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.Equal(t, strfmt.DateTime{}, fmtDTime)
assert.NotNil(t, err)
}
6 changes: 6 additions & 0 deletions v5/core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
"time"

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

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
3 changes: 3 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

0 comments on commit e651369

Please sign in to comment.