فهرست منبع

model

- add PageInfo
Alexey Kim 9 ماه پیش
والد
کامیت
aa23216e04
6فایلهای تغییر یافته به همراه187 افزوده شده و 1 حذف شده
  1. 6 0
      Makefile
  2. 2 0
      go.mod
  3. 4 0
      go.sum
  4. 1 1
      graph/sports
  5. 45 0
      model/page_info.go
  6. 129 0
      model/page_info_easyjson.go

+ 6 - 0
Makefile

@@ -15,3 +15,9 @@ deps:
 build:
 	-@echo "-> $@"
 	rover supergraph compose --config ./supergraph.yaml --output ./devops/prod-schema.graphql
+
+.PHONY: gen-json
+gen-json:
+	-@echo "-> $@"
+	-@echo "=== Generating easyJson bindings"
+	easyjson model/page_info.go

+ 2 - 0
go.mod

@@ -12,6 +12,7 @@ require (
 	github.com/labstack/echo-jwt/v4 v4.2.0
 	github.com/labstack/echo/v4 v4.11.1
 	github.com/mailru/easyjson v0.7.7
+	github.com/samber/lo v1.38.1
 	github.com/urfave/cli/v2 v2.25.7
 	gopkg.in/yaml.v3 v3.0.1
 )
@@ -44,6 +45,7 @@ require (
 	go.opentelemetry.io/otel v1.16.0 // indirect
 	go.opentelemetry.io/otel/trace v1.16.0 // indirect
 	golang.org/x/crypto v0.12.0 // indirect
+	golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
 	golang.org/x/net v0.14.0 // indirect
 	golang.org/x/sys v0.11.0 // indirect
 	golang.org/x/text v0.12.0 // indirect

+ 4 - 0
go.sum

@@ -77,6 +77,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
 github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
 github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
 github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
+github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
+github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
 github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
 github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
 github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c=
@@ -115,6 +117,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
 golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
 golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
 golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
+golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
+golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
 golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
 golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
 golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=

+ 1 - 1
graph/sports

@@ -1 +1 @@
-Subproject commit d568499b7c23c7d4d027ce68f4714c848a725c67
+Subproject commit 2ffe1ad28332a09a9fdbd8b02fad5196b6a60b6f

+ 45 - 0
model/page_info.go

@@ -0,0 +1,45 @@
+package model
+
+import (
+	"git.beejay.kim/Craft/Api/connection"
+	"github.com/samber/lo"
+)
+
+//easyjson:json
+type PageInfo struct {
+	Total           int     `json:"total"`
+	StartCursor     *string `json:"start_cursor,omitempty"`
+	EndCursor       *string `json:"end_cursor,omitempty"`
+	HasNextPage     bool    `json:"has_next_page"`
+	HasPreviousPage bool    `json:"has_previous_page"`
+}
+
+func (pi *PageInfo) SetTotal(nodes []connection.Cursor) {
+	pi.Total = len(nodes)
+	pi.Invalidate(nodes)
+}
+
+func (pi *PageInfo) Invalidate(nodes []connection.Cursor) {
+	l := len(nodes)
+	if l < 1 {
+		pi.HasPreviousPage = pi.StartCursor != nil
+		pi.HasNextPage = pi.EndCursor != nil
+		pi.StartCursor = nil
+		pi.EndCursor = nil
+	} else {
+		if pi.StartCursor != nil {
+			pi.HasPreviousPage = !(*pi.StartCursor == nodes[0].Cursor())
+		} else {
+			pi.HasPreviousPage = false
+		}
+
+		if pi.EndCursor != nil {
+			pi.HasNextPage = !(*pi.EndCursor == nodes[l-1].Cursor())
+		} else {
+			pi.HasNextPage = false
+		}
+
+		pi.StartCursor = lo.ToPtr(nodes[0].Cursor())
+		pi.EndCursor = lo.ToPtr(nodes[l-1].Cursor())
+	}
+}

+ 129 - 0
model/page_info_easyjson.go

@@ -0,0 +1,129 @@
+// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT.
+
+package model
+
+import (
+	json "encoding/json"
+	easyjson "github.com/mailru/easyjson"
+	jlexer "github.com/mailru/easyjson/jlexer"
+	jwriter "github.com/mailru/easyjson/jwriter"
+)
+
+// suppress unused package warning
+var (
+	_ *json.RawMessage
+	_ *jlexer.Lexer
+	_ *jwriter.Writer
+	_ easyjson.Marshaler
+)
+
+func easyjson142f204aDecodeGitBeejayKimCraftApiModel(in *jlexer.Lexer, out *PageInfo) {
+	isTopLevel := in.IsStart()
+	if in.IsNull() {
+		if isTopLevel {
+			in.Consumed()
+		}
+		in.Skip()
+		return
+	}
+	in.Delim('{')
+	for !in.IsDelim('}') {
+		key := in.UnsafeFieldName(false)
+		in.WantColon()
+		if in.IsNull() {
+			in.Skip()
+			in.WantComma()
+			continue
+		}
+		switch key {
+		case "total":
+			out.Total = int(in.Int())
+		case "start_cursor":
+			if in.IsNull() {
+				in.Skip()
+				out.StartCursor = nil
+			} else {
+				if out.StartCursor == nil {
+					out.StartCursor = new(string)
+				}
+				*out.StartCursor = string(in.String())
+			}
+		case "end_cursor":
+			if in.IsNull() {
+				in.Skip()
+				out.EndCursor = nil
+			} else {
+				if out.EndCursor == nil {
+					out.EndCursor = new(string)
+				}
+				*out.EndCursor = string(in.String())
+			}
+		case "has_next_page":
+			out.HasNextPage = bool(in.Bool())
+		case "has_previous_page":
+			out.HasPreviousPage = bool(in.Bool())
+		default:
+			in.SkipRecursive()
+		}
+		in.WantComma()
+	}
+	in.Delim('}')
+	if isTopLevel {
+		in.Consumed()
+	}
+}
+func easyjson142f204aEncodeGitBeejayKimCraftApiModel(out *jwriter.Writer, in PageInfo) {
+	out.RawByte('{')
+	first := true
+	_ = first
+	{
+		const prefix string = ",\"total\":"
+		out.RawString(prefix[1:])
+		out.Int(int(in.Total))
+	}
+	if in.StartCursor != nil {
+		const prefix string = ",\"start_cursor\":"
+		out.RawString(prefix)
+		out.String(string(*in.StartCursor))
+	}
+	if in.EndCursor != nil {
+		const prefix string = ",\"end_cursor\":"
+		out.RawString(prefix)
+		out.String(string(*in.EndCursor))
+	}
+	{
+		const prefix string = ",\"has_next_page\":"
+		out.RawString(prefix)
+		out.Bool(bool(in.HasNextPage))
+	}
+	{
+		const prefix string = ",\"has_previous_page\":"
+		out.RawString(prefix)
+		out.Bool(bool(in.HasPreviousPage))
+	}
+	out.RawByte('}')
+}
+
+// MarshalJSON supports json.Marshaler interface
+func (v PageInfo) MarshalJSON() ([]byte, error) {
+	w := jwriter.Writer{}
+	easyjson142f204aEncodeGitBeejayKimCraftApiModel(&w, v)
+	return w.Buffer.BuildBytes(), w.Error
+}
+
+// MarshalEasyJSON supports easyjson.Marshaler interface
+func (v PageInfo) MarshalEasyJSON(w *jwriter.Writer) {
+	easyjson142f204aEncodeGitBeejayKimCraftApiModel(w, v)
+}
+
+// UnmarshalJSON supports json.Unmarshaler interface
+func (v *PageInfo) UnmarshalJSON(data []byte) error {
+	r := jlexer.Lexer{Data: data}
+	easyjson142f204aDecodeGitBeejayKimCraftApiModel(&r, v)
+	return r.Error()
+}
+
+// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
+func (v *PageInfo) UnmarshalEasyJSON(l *jlexer.Lexer) {
+	easyjson142f204aDecodeGitBeejayKimCraftApiModel(l, v)
+}