Skip to content

Commit

Permalink
new x/jsonx season type
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Sep 19, 2024
1 parent 3137d0d commit 8737f6b
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 10 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 2 additions & 7 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion x/jsonx/jsonx.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
123 changes: 123 additions & 0 deletions x/jsonx/season.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 8737f6b

Please sign in to comment.