Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2023-03-29 14:00:05 +02:00
parent 411693c769
commit f0cfbbe2cc
41 changed files with 760 additions and 410 deletions

View File

@ -14,31 +14,40 @@ linters-settings:
min-occurrences: 4
linters:
enable-all: true
disable:
- maligned
- lll
- gochecknoinits
- gochecknoglobals
- godox
- gocognit
- whitespace
- wsl
- funlen
- wrapcheck
- testpackage
- nlreturn
- gofumpt
- goerr113
- gci
- gomnd
- godot
- exhaustivestruct
- paralleltest
- varnamelen
- ireturn
- exhaustruct
#- thelper
enable:
- revive
- goimports
- gosec
- unparam
- unconvert
- predeclared
- prealloc
- misspell
# disable:
# - maligned
# - lll
# - gochecknoinits
# - gochecknoglobals
# - godox
# - gocognit
# - whitespace
# - wsl
# - funlen
# - wrapcheck
# - testpackage
# - nlreturn
# - gofumpt
# - goerr113
# - gci
# - gomnd
# - godot
# - exhaustivestruct
# - paralleltest
# - varnamelen
# - ireturn
# - exhaustruct
# #- thelper
issues:
exclude-rules:

View File

@ -57,7 +57,7 @@ func (d *Date) UnmarshalText(text []byte) error {
if len(text) == 0 {
return nil
}
dd, err := time.Parse(RFC3339FullDate, string(text))
dd, err := time.ParseInLocation(RFC3339FullDate, string(text), DefaultTimeLocation)
if err != nil {
return err
}
@ -107,7 +107,7 @@ func (d *Date) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &strdate); err != nil {
return err
}
tt, err := time.Parse(RFC3339FullDate, strdate)
tt, err := time.ParseInLocation(RFC3339FullDate, strdate, DefaultTimeLocation)
if err != nil {
return err
}
@ -126,7 +126,7 @@ func (d *Date) UnmarshalBSON(data []byte) error {
}
if data, ok := m["data"].(string); ok {
rd, err := time.Parse(RFC3339FullDate, data)
rd, err := time.ParseInLocation(RFC3339FullDate, data, DefaultTimeLocation)
if err != nil {
return err
}

View File

@ -109,7 +109,7 @@ func (f *defaultFormats) MapStructureHookFunc() mapstructure.DecodeHookFunc { //
if to == tpe {
switch v.Name {
case "date":
d, err := time.Parse(RFC3339FullDate, data)
d, err := time.ParseInLocation(RFC3339FullDate, data, DefaultTimeLocation)
if err != nil {
return nil, err
}

View File

@ -29,6 +29,12 @@ import (
"go.mongodb.org/mongo-driver/bson/bsontype"
)
var (
// UnixZero sets the zero unix timestamp we want to compare against.
// Unix 0 for an EST timezone is not equivalent to a UTC timezone.
UnixZero = time.Unix(0, 0).UTC()
)
func init() {
dt := DateTime{}
Default.Add("datetime", &dt, IsDateTime)
@ -86,6 +92,9 @@ var (
// NormalizeTimeForMarshal provides a normalization function on time befeore marshalling (e.g. time.UTC).
// By default, the time value is not changed.
NormalizeTimeForMarshal = func(t time.Time) time.Time { return t }
// DefaultTimeLocation provides a location for a time when the time zone is not encoded in the string (ex: ISO8601 Local variants).
DefaultTimeLocation = time.UTC
)
// ParseDateTime parses a string that represents an ISO8601 time or a unix epoch
@ -95,7 +104,7 @@ func ParseDateTime(data string) (DateTime, error) {
}
var lastError error
for _, layout := range DateTimeFormats {
dd, err := time.Parse(layout, data)
dd, err := time.ParseInLocation(layout, data, DefaultTimeLocation)
if err != nil {
lastError = err
continue
@ -123,6 +132,16 @@ func (t DateTime) String() string {
return NormalizeTimeForMarshal(time.Time(t)).Format(MarshalFormat)
}
// IsZero returns whether the date time is a zero value
func (t DateTime) IsZero() bool {
return time.Time(t).IsZero()
}
// IsUnixZerom returns whether the date time is equivalent to time.Unix(0, 0).UTC().
func (t DateTime) IsUnixZero() bool {
return time.Time(t) == UnixZero
}
// MarshalText implements the text marshaller interface
func (t DateTime) MarshalText() ([]byte, error) {
return []byte(t.String()), nil

View File

@ -15,9 +15,12 @@ import (
// ULID represents a ulid string format
// ref:
// https://github.com/ulid/spec
//
// https://github.com/ulid/spec
//
// impl:
// https://github.com/oklog/ulid
//
// https://github.com/oklog/ulid
//
// swagger:strfmt ulid
type ULID struct {
@ -89,7 +92,9 @@ func NewULIDZero() ULID {
}
// NewULID generates new unique ULID value and a error if any
func NewULID() (u ULID, err error) {
func NewULID() (ULID, error) {
var u ULID
obj := ulidEntropyPool.Get()
entropy, ok := obj.(io.Reader)
if !ok {