Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add UUID, date, and datetime helpers for terraform usage #96

Merged
merged 2 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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