Skip to content

Commit

Permalink
🔇 silent changes: updated query model #9
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Feb 5, 2024
1 parent 31a8514 commit 8f26029
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
99 changes: 99 additions & 0 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"time"

"github.com/sivaosorg/govm/builder"
"github.com/sivaosorg/govm/timex"
"github.com/sivaosorg/govm/utils"
)

Expand Down Expand Up @@ -149,6 +151,90 @@ func (i *IPageQuery) Json() string {
return utils.ToJson(i)
}

func NewDecision() *decision {
i := &decision{}
i.SetOn(time.Now())
return i
}

func WithDecision(value bool) *decision {
i := NewDecision().SetEnabled(value).SetOn(time.Now())
return i
}

func (i *decision) SetEnabled(value bool) *decision {
i.IsEnabled = value
return i
}

func (i *decision) SetValue(value interface{}) *decision {
i.Value = value
return i
}

func (i *decision) SetOn(value time.Time) *decision {
i.on = value
return i
}

func (i *decision) On() time.Time {
return i.on
}

func (i *decision) OnString() string {
return i.on.Format(timex.TimeFormat20060102150405)
}

func (i *decision) Json() string {
return utils.ToJson(i)
}

func NewModify() Modify {
m := make(Modify)
return m
}

func (m Modify) Add(key string, value decision) Modify {
m[key] = value
return m
}

func (m Modify) Addon(key string, value *decision) Modify {
m[key] = *value
return m
}

func (m Modify) Remove(key string) Modify {
delete(m, key)
return m
}

func (m Modify) RemoveAny(keys ...string) Modify {
for _, v := range keys {
delete(m, v)
}
return m
}

func (m Modify) Size() int {
return len(m)
}

func (m Modify) Available() bool {
return m.Size() > 0
}

func (m Modify) Transform() map[string]interface{} {
values := builder.NewMapBuilder()
for k, e := range m {
if !e.IsEnabled {
continue
}
values.Add(k, e.Value)
}
return values.Build()
}

func GetTotalPages(totalCount int, size int) int {
d := float64(totalCount) / float64(size)
return int(math.Ceil(d))
Expand All @@ -157,3 +243,16 @@ func GetTotalPages(totalCount int, size int) int {
func GetHasMore(currentPage, totalCount, size int) bool {
return currentPage < totalCount/size
}

func Transforms(ms ...Modify) map[string]interface{} {
values := builder.NewMapBuilder()
for _, m := range ms {
for k, e := range m {
if !e.IsEnabled {
continue
}
values.Add(k, e.Value)
}
}
return values.Build()
}
8 changes: 8 additions & 0 deletions query/query_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ type IPageQuery struct {
EventAt timestampQuery `json:"event_at,omitempty"`
IndexAt timestampQuery `json:"index_at,omitempty"`
}

type decision struct {
IsEnabled bool `json:"enabled"`
Value interface{} `json:"value,omitempty"`
on time.Time
}

type Modify map[string]decision

0 comments on commit 8f26029

Please sign in to comment.