mirror of
https://github.com/kataras/iris.git
synced 2025-01-22 18:21:03 +01:00
new x/jsonx season type
This commit is contained in:
parent
3137d0d791
commit
8737f6b691
3
go.mod
3
go.mod
|
@ -57,7 +57,7 @@ require (
|
|||
github.com/cespare/xxhash v1.1.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/dgraph-io/ristretto v0.1.1 // indirect
|
||||
github.com/dgraph-io/ristretto v1.0.0 // indirect
|
||||
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
|
@ -67,7 +67,6 @@ require (
|
|||
github.com/gobwas/httphead v0.1.0 // indirect
|
||||
github.com/gobwas/pool v0.2.1 // indirect
|
||||
github.com/gobwas/ws v1.4.0 // indirect
|
||||
github.com/golang/glog v1.2.2 // indirect
|
||||
github.com/golang/protobuf v1.5.4 // indirect
|
||||
github.com/google/go-querystring v1.1.0 // indirect
|
||||
github.com/gorilla/css v1.0.1 // indirect
|
||||
|
|
9
go.sum
generated
9
go.sum
generated
|
@ -28,7 +28,6 @@ github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
|||
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
|
||||
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
|
||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
|
||||
|
@ -42,8 +41,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
|||
github.com/dgraph-io/badger/v2 v2.2007.4 h1:TRWBQg8UrlUhaFdco01nO2uXwzKS7zd+HVdwV/GHc4o=
|
||||
github.com/dgraph-io/badger/v2 v2.2007.4/go.mod h1:vSw/ax2qojzbN6eXHIx6KPKtCSHJN/Uz0X0VPruTIhk=
|
||||
github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
|
||||
github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8=
|
||||
github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA=
|
||||
github.com/dgraph-io/ristretto v1.0.0 h1:SYG07bONKMlFDUYu5pEu3DGAh8c2OFNzKm6G9J4Si84=
|
||||
github.com/dgraph-io/ristretto v1.0.0/go.mod h1:jTi2FiYEhQ1NsMmA7DeBykizjOuY88NhKBkepyu1jPc=
|
||||
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
|
||||
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 h1:fAjc9m62+UWV/WAFKLNi6ZS0675eEUC9y3AlwSbQu1Y=
|
||||
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
|
||||
|
@ -73,9 +72,6 @@ github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
|
|||
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
|
||||
github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs=
|
||||
github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/glog v1.2.2 h1:1+mZ9upx1Dh6FmUTFR1naJ77miKiXgALjWOZ3NVFPmY=
|
||||
github.com/golang/glog v1.2.2/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w=
|
||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||
|
@ -300,7 +296,6 @@ golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBc
|
|||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
||||
|
|
|
@ -1,11 +1,17 @@
|
|||
package jsonx
|
||||
|
||||
import "bytes"
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
quoteLiteral = '"'
|
||||
emptyQuoteBytes = []byte(`""`)
|
||||
nullLiteral = []byte("null")
|
||||
|
||||
// ErrInvalid is returned when the value is invalid.
|
||||
ErrInvalid = errors.New("invalid")
|
||||
)
|
||||
|
||||
func isNull(b []byte) bool {
|
||||
|
|
123
x/jsonx/season.go
Normal file
123
x/jsonx/season.go
Normal file
|
@ -0,0 +1,123 @@
|
|||
package jsonx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GetSeasonByDate returns the season based on the given date.
|
||||
func GetSeasonByDate(date SimpleDate) Season {
|
||||
month := date.ToTime().Month()
|
||||
|
||||
switch month {
|
||||
case time.December, time.January, time.February:
|
||||
return Winter
|
||||
case time.March, time.April, time.May:
|
||||
return Spring
|
||||
case time.June, time.July, time.August:
|
||||
return Summer
|
||||
case time.September, time.October, time.November:
|
||||
return Autumn
|
||||
default:
|
||||
return 0 // Should never happen.
|
||||
}
|
||||
}
|
||||
|
||||
// Season is a bitmask of seasons
|
||||
// Winter, Spring, Summer, Autumn.
|
||||
// It is used to represent the seasons of a year.
|
||||
// It is a bitmask so that multiple seasons can be set at once.
|
||||
// It has methods to check if a season is set, add a season,
|
||||
// remove a season, and get a string representation of the seasons.
|
||||
type Season int
|
||||
|
||||
const (
|
||||
// December, January, February.
|
||||
Winter Season = 1 << iota // 1 << 0 = 1
|
||||
// March, April, May.
|
||||
Spring // 1 << 1 = 2
|
||||
// June, July, August.
|
||||
Summer // 1 << 2 = 4
|
||||
// September, October, November.
|
||||
Autumn // 1 << 3 = 8
|
||||
|
||||
// AllSeasons is a bitmask of all seasons.
|
||||
// It's the number 15 because it's the sum of all seasons.
|
||||
AllSeasons = Winter | Spring | Summer | Autumn // 1 + 2 + 4 + 8 = 15
|
||||
)
|
||||
|
||||
// IsValid checks if the season is valid.
|
||||
func (s Season) IsValid() bool {
|
||||
return s&AllSeasons == s
|
||||
}
|
||||
|
||||
// Is checks if the season is set in the bitmask.
|
||||
func (s Season) Is(season Season) bool {
|
||||
return s&season != 0
|
||||
}
|
||||
|
||||
// Add adds a season to the bitmask.
|
||||
func (s *Season) Add(season Season) {
|
||||
*s |= season
|
||||
}
|
||||
|
||||
// Remove removes a season from the bitmask.
|
||||
func (s *Season) Remove(season Season) {
|
||||
*s &= ^season
|
||||
}
|
||||
|
||||
// String returns the string representation of the season(s).
|
||||
func (s Season) String() string {
|
||||
var seasons []string
|
||||
|
||||
if s.Is(Winter) {
|
||||
seasons = append(seasons, "Winter")
|
||||
}
|
||||
if s.Is(Spring) {
|
||||
seasons = append(seasons, "Spring")
|
||||
}
|
||||
if s.Is(Summer) {
|
||||
seasons = append(seasons, "Summer")
|
||||
}
|
||||
if s.Is(Autumn) {
|
||||
seasons = append(seasons, "Autumn")
|
||||
}
|
||||
|
||||
if len(seasons) == 0 {
|
||||
return "None"
|
||||
}
|
||||
|
||||
return strings.Join(seasons, ", ")
|
||||
}
|
||||
|
||||
// MarshalJSON marshals the season to JSON.
|
||||
// It marshals the season as a string.
|
||||
func (s *Season) UnmarshalJSON(data []byte) error {
|
||||
if isNull(data) {
|
||||
return nil
|
||||
}
|
||||
|
||||
data = trimQuotes(data)
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
str := string(data)
|
||||
constantAsInt, err := strconv.Atoi(str)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if constantAsInt == 0 { // if 0 is passed, it means All seasons.
|
||||
constantAsInt = int(AllSeasons)
|
||||
}
|
||||
|
||||
*s = Season(constantAsInt)
|
||||
if !s.IsValid() {
|
||||
return fmt.Errorf("%w: season: %s", ErrInvalid, str)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user