Use dedicated type for each order field

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-03-19 00:20:05 +01:00
parent c73e417e5c
commit 5a1f873d58
32 changed files with 518 additions and 119 deletions

View File

@@ -55,8 +55,13 @@ type (
}
)
func (c Control) CursorKey(orderBy page.OrderField) page.CursorKey {
return page.NewCursorKey(c.ID, c.CreatedAt)
func (c Control) CursorKey(orderBy ControlOrderField) page.CursorKey {
switch orderBy {
case ControlOrderFieldCreatedAt:
return page.NewCursorKey(c.ID, c.CreatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (c *Control) LoadByID(
@@ -170,7 +175,7 @@ func (c *Controls) LoadByFrameworkID(
conn pg.Conn,
scope Scoper,
frameworkID gid.GID,
cursor *page.Cursor,
cursor *page.Cursor[ControlOrderField],
) error {
q := `
SELECT

View File

@@ -0,0 +1,40 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
type (
ControlOrderField string
)
const (
ControlOrderFieldCreatedAt ControlOrderField = "CREATED_AT"
)
func (p ControlOrderField) Column() string {
return string(p)
}
func (p ControlOrderField) String() string {
return string(p)
}
func (p ControlOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *ControlOrderField) UnmarshalText(text []byte) error {
*p = ControlOrderField(text)
return nil
}

View File

@@ -42,8 +42,13 @@ type (
Evidences []*Evidence
)
func (e Evidence) CursorKey(orderBy page.OrderField) page.CursorKey {
return page.NewCursorKey(e.ID, e.CreatedAt)
func (e Evidence) CursorKey(orderBy EvidenceOrderField) page.CursorKey {
switch orderBy {
case EvidenceOrderFieldCreatedAt:
return page.NewCursorKey(e.ID, e.CreatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (e Evidence) Insert(
@@ -145,7 +150,7 @@ func (e *Evidences) LoadByTaskID(
conn pg.Conn,
scope Scoper,
taskID gid.GID,
cursor *page.Cursor,
cursor *page.Cursor[EvidenceOrderField],
) error {
q := `
SELECT

View File

@@ -0,0 +1,40 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
type (
EvidenceOrderField string
)
const (
EvidenceOrderFieldCreatedAt EvidenceOrderField = "CREATED_AT"
)
func (p EvidenceOrderField) Column() string {
return string(p)
}
func (p EvidenceOrderField) String() string {
return string(p)
}
func (p EvidenceOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *EvidenceOrderField) UnmarshalText(text []byte) error {
*p = EvidenceOrderField(text)
return nil
}

View File

@@ -47,8 +47,13 @@ type (
}
)
func (f Framework) CursorKey(orderBy page.OrderField) page.CursorKey {
return page.NewCursorKey(f.ID, f.CreatedAt)
func (f Framework) CursorKey(orderBy FrameworkOrderField) page.CursorKey {
switch orderBy {
case FrameworkOrderFieldCreatedAt:
return page.NewCursorKey(f.ID, f.CreatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (f *Frameworks) LoadByOrganizationID(
@@ -56,7 +61,7 @@ func (f *Frameworks) LoadByOrganizationID(
conn pg.Conn,
scope Scoper,
organizationID gid.GID,
cursor *page.Cursor,
cursor *page.Cursor[FrameworkOrderField],
) error {
q := `
SELECT

View File

@@ -0,0 +1,40 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
type (
FrameworkOrderField string
)
const (
FrameworkOrderFieldCreatedAt FrameworkOrderField = "CREATED_AT"
)
func (p FrameworkOrderField) Column() string {
return string(p)
}
func (p FrameworkOrderField) String() string {
return string(p)
}
func (p FrameworkOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *FrameworkOrderField) UnmarshalText(text []byte) error {
*p = FrameworkOrderField(text)
return nil
}

View File

@@ -39,8 +39,13 @@ type (
Organizations []*Organization
)
func (o Organization) CursorKey(orderBy page.OrderField) page.CursorKey {
return page.NewCursorKey(o.ID, o.CreatedAt)
func (o Organization) CursorKey(orderBy OrganizationOrderField) page.CursorKey {
switch orderBy {
case OrganizationOrderFieldCreatedAt:
return page.NewCursorKey(o.ID, o.CreatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (o *Organization) LoadByID(

View File

@@ -0,0 +1,40 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
type (
OrganizationOrderField string
)
const (
OrganizationOrderFieldCreatedAt OrganizationOrderField = "CREATED_AT"
)
func (p OrganizationOrderField) Column() string {
return string(p)
}
func (p OrganizationOrderField) String() string {
return string(p)
}
func (p OrganizationOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *OrganizationOrderField) UnmarshalText(text []byte) error {
*p = OrganizationOrderField(text)
return nil
}

View File

@@ -50,8 +50,15 @@ type (
}
)
func (p People) CursorKey(orderBy page.OrderField) page.CursorKey {
return page.NewCursorKey(p.ID, p.CreatedAt)
func (p People) CursorKey(orderBy PeopleOrderField) page.CursorKey {
switch orderBy {
case PeopleOrderFieldCreatedAt:
return page.NewCursorKey(p.ID, p.CreatedAt)
case PeopleOrderFieldFullName:
return page.NewCursorKey(p.ID, p.FullName)
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (p *People) LoadByID(
@@ -171,8 +178,9 @@ func (p *Peoples) LoadByOrganizationID(
conn pg.Conn,
scope Scoper,
organizationID gid.GID,
cursor *page.Cursor,
cursor *page.Cursor[PeopleOrderField],
) error {
// Base query
q := `
SELECT
id,

View File

@@ -0,0 +1,41 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
type (
PeopleOrderField string
)
const (
PeopleOrderFieldCreatedAt PeopleOrderField = "CREATED_AT"
PeopleOrderFieldFullName PeopleOrderField = "FULL_NAME"
)
func (p PeopleOrderField) Column() string {
return string(p)
}
func (p PeopleOrderField) String() string {
return string(p)
}
func (p PeopleOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *PeopleOrderField) UnmarshalText(text []byte) error {
*p = PeopleOrderField(text)
return nil
}

View File

@@ -38,8 +38,15 @@ type (
}
)
func (p Policy) CursorKey(orderBy page.OrderField) page.CursorKey {
return page.NewCursorKey(p.ID, p.CreatedAt)
func (p Policy) CursorKey(orderBy PolicyOrderField) page.CursorKey {
switch orderBy {
case PolicyOrderFieldCreatedAt:
return page.NewCursorKey(p.ID, p.CreatedAt)
case PolicyOrderFieldName:
return page.NewCursorKey(p.ID, p.Name)
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (p *Policy) LoadByID(
@@ -93,7 +100,7 @@ func (p *Policies) LoadByOrganizationID(
conn pg.Conn,
scope Scoper,
organizationID gid.GID,
cursor *page.Cursor,
cursor *page.Cursor[PolicyOrderField],
) error {
q := `
SELECT

View File

@@ -0,0 +1,41 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
type (
PolicyOrderField string
)
const (
PolicyOrderFieldCreatedAt PolicyOrderField = "CREATED_AT"
PolicyOrderFieldName PolicyOrderField = "NAME"
)
func (p PolicyOrderField) Column() string {
return string(p)
}
func (p PolicyOrderField) String() string {
return string(p)
}
func (p PolicyOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *PolicyOrderField) UnmarshalText(text []byte) error {
*p = PolicyOrderField(text)
return nil
}

View File

@@ -38,8 +38,13 @@ type (
SessionData struct{}
)
func (s Session) CursorKey(orderBy page.OrderField) page.CursorKey {
return page.NewCursorKey(s.ID, s.CreatedAt)
func (s Session) CursorKey(orderBy SessionOrderField) page.CursorKey {
switch orderBy {
case SessionOrderFieldCreatedAt:
return page.NewCursorKey(s.ID, s.CreatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (s *Session) LoadByID(

View File

@@ -0,0 +1,40 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
type (
SessionOrderField string
)
const (
SessionOrderFieldCreatedAt SessionOrderField = "CREATED_AT"
)
func (p SessionOrderField) Column() string {
return string(p)
}
func (p SessionOrderField) String() string {
return string(p)
}
func (p SessionOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *SessionOrderField) UnmarshalText(text []byte) error {
*p = SessionOrderField(text)
return nil
}

View File

@@ -53,8 +53,13 @@ type (
}
)
func (t Task) CursorKey(orderBy page.OrderField) page.CursorKey {
return page.NewCursorKey(t.ID, t.CreatedAt)
func (t Task) CursorKey(orderBy TaskOrderField) page.CursorKey {
switch orderBy {
case TaskOrderFieldCreatedAt:
return page.NewCursorKey(t.ID, t.CreatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (t *Task) LoadByID(
@@ -163,7 +168,7 @@ func (t *Tasks) LoadByControlID(
conn pg.Conn,
scope Scoper,
controlID gid.GID,
cursor *page.Cursor,
cursor *page.Cursor[TaskOrderField],
) error {
q := `
SELECT

View File

@@ -0,0 +1,40 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
type (
TaskOrderField string
)
const (
TaskOrderFieldCreatedAt TaskOrderField = "CREATED_AT"
)
func (p TaskOrderField) Column() string {
return string(p)
}
func (p TaskOrderField) String() string {
return string(p)
}
func (p TaskOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *TaskOrderField) UnmarshalText(text []byte) error {
*p = TaskOrderField(text)
return nil
}

View File

@@ -59,15 +59,20 @@ func (e ErrUserAlreadyExists) Error() string {
return e.message
}
func (u User) CursorKey(orderBy page.OrderField) page.CursorKey {
return page.NewCursorKey(u.ID, u.CreatedAt)
func (u User) CursorKey(orderBy UserOrderField) page.CursorKey {
switch orderBy {
case UserOrderFieldCreatedAt:
return page.NewCursorKey(u.ID, u.CreatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (u *Users) LoadByOrganizationID(
ctx context.Context,
conn pg.Conn,
organizationID gid.GID,
cursor *page.Cursor,
cursor *page.Cursor[UserOrderField],
) error {
q := `
SELECT

View File

@@ -0,0 +1,40 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
type (
UserOrderField string
)
const (
UserOrderFieldCreatedAt UserOrderField = "CREATED_AT"
)
func (p UserOrderField) Column() string {
return string(p)
}
func (p UserOrderField) String() string {
return string(p)
}
func (p UserOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *UserOrderField) UnmarshalText(text []byte) error {
*p = UserOrderField(text)
return nil
}

View File

@@ -63,17 +63,15 @@ type (
}
)
func (v Vendor) CursorKey(orderBy page.OrderField) page.CursorKey {
func (v Vendor) CursorKey(orderBy VendorOrderField) page.CursorKey {
switch orderBy {
case page.OrderFieldCreatedAt:
case VendorOrderFieldCreatedAt:
return page.NewCursorKey(v.ID, v.CreatedAt)
case page.OrderFieldUpdatedAt:
return page.NewCursorKey(v.ID, v.UpdatedAt)
case page.OrderFieldName:
case VendorOrderFieldName:
return page.NewCursorKey(v.ID, v.Name)
}
panic(fmt.Sprintf("unknown order by: %s", orderBy))
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (v *Vendor) LoadByID(
@@ -213,7 +211,7 @@ func (v *Vendors) LoadByOrganizationID(
conn pg.Conn,
scope Scoper,
organizationID gid.GID,
cursor *page.Cursor,
cursor *page.Cursor[VendorOrderField],
) error {
q := `
SELECT

View File

@@ -0,0 +1,41 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
type (
VendorOrderField string
)
const (
VendorOrderFieldCreatedAt VendorOrderField = "CREATED_AT"
VendorOrderFieldName VendorOrderField = "NAME"
)
func (p VendorOrderField) Column() string {
return string(p)
}
func (p VendorOrderField) String() string {
return string(p)
}
func (p VendorOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *VendorOrderField) UnmarshalText(text []byte) error {
*p = VendorOrderField(text)
return nil
}

View File

@@ -19,17 +19,17 @@ import (
)
type (
Cursor struct {
Cursor[T OrderField] struct {
Size int
Position Position
Key *CursorKey
OrderBy OrderBy
OrderBy OrderBy[T]
}
Position string
OrderBy struct {
Field OrderField
OrderBy[T OrderField] struct {
Field T
Direction OrderDirection
}
)
@@ -41,27 +41,20 @@ const (
Head Position = "HEAD"
)
func NewCursor(size int, from *CursorKey, pos Position, orderBy *OrderBy) *Cursor {
func NewCursor[T OrderField](size int, from *CursorKey, pos Position, orderBy OrderBy[T]) *Cursor[T] {
if size == 0 {
size = DefaultCursorSize
}
if orderBy == nil {
orderBy = &OrderBy{
Field: OrderFieldCreatedAt,
Direction: OrderDirectionDesc,
}
}
return &Cursor{
return &Cursor[T]{
Size: size,
Key: from,
Position: pos,
OrderBy: *orderBy,
OrderBy: orderBy,
}
}
func (c *Cursor) SQLFragment() string {
func (c *Cursor[T]) SQLFragment() string {
fieldName := c.OrderBy.Field.Column()
var orderDirection string
@@ -88,7 +81,7 @@ func (c *Cursor) SQLFragment() string {
return whereClause + " ORDER BY " + orderByClause + " LIMIT @cursor_limit"
}
func (c *Cursor) SQLArguments() pgx.NamedArgs {
func (c *Cursor[T]) SQLArguments() pgx.NamedArgs {
var size = c.Size
if c.Key == nil {
size += 1

View File

@@ -21,51 +21,6 @@ import (
type (
OrderField interface {
Column() string
fmt.Stringer
}
GenericOrderField string
)
const (
OrderFieldCreatedAt GenericOrderField = "CREATED_AT"
OrderFieldUpdatedAt GenericOrderField = "UPDATED_AT"
OrderFieldName GenericOrderField = "NAME"
)
func (of GenericOrderField) String() string {
return string(of)
}
func (of GenericOrderField) Column() string {
switch of {
case OrderFieldCreatedAt:
return "created_at"
case OrderFieldUpdatedAt:
return "updated_at"
case OrderFieldName:
return "name"
default:
return ""
}
}
func (of GenericOrderField) MarshalText() ([]byte, error) {
return []byte(of.String()), nil
}
func (of *GenericOrderField) UnmarshalText(data []byte) error {
val := string(data)
switch val {
case OrderFieldCreatedAt.String():
*of = OrderFieldCreatedAt
case OrderFieldUpdatedAt.String():
*of = OrderFieldUpdatedAt
case OrderFieldName.String():
*of = OrderFieldName
default:
return fmt.Errorf("invalid GenericOrderField value: %q", val)
}
return nil
}

View File

@@ -15,8 +15,8 @@
package page
type (
Paginable interface {
CursorKey(orderBy OrderField) CursorKey
Paginable[T OrderField] interface {
CursorKey(orderBy T) CursorKey
}
PageInfo struct {
@@ -24,14 +24,14 @@ type (
HasPrev bool
}
Page[T Paginable] struct {
Page[T Paginable[U], U OrderField] struct {
Info *PageInfo
Cursor *Cursor
Cursor *Cursor[U]
Data []T
}
)
func (p *Page[T]) First() T {
func (p *Page[T, U]) First() T {
if len(p.Data) == 0 {
var zero T
return zero
@@ -40,7 +40,7 @@ func (p *Page[T]) First() T {
return p.Data[0]
}
func (p *Page[T]) Last() T {
func (p *Page[T, U]) Last() T {
if len(p.Data) == 0 {
var zero T
return zero
@@ -49,11 +49,11 @@ func (p *Page[T]) Last() T {
return p.Data[len(p.Data)-1]
}
func NewPage[T Paginable](data []T, c *Cursor) *Page[T] {
func NewPage[T Paginable[U], U OrderField](data []T, c *Cursor[U]) *Page[T, U] {
pi := &PageInfo{}
if len(data) == 0 {
return &Page[T]{
return &Page[T, U]{
Info: pi,
Data: data,
}
@@ -109,7 +109,7 @@ func NewPage[T Paginable](data []T, c *Cursor) *Page[T] {
}
}
return &Page[T]{
return &Page[T, U]{
Info: pi,
Cursor: c,
Data: edges,

View File

@@ -100,8 +100,8 @@ func (s ControlService) Update(
func (s ControlService) ListForFrameworkID(
ctx context.Context,
frameworkID gid.GID,
cursor *page.Cursor,
) (*page.Page[*coredata.Control], error) {
cursor *page.Cursor[coredata.ControlOrderField],
) (*page.Page[*coredata.Control, coredata.ControlOrderField], error) {
var controls coredata.Controls
err := s.svc.pg.WithConn(

View File

@@ -171,8 +171,8 @@ func (s EvidenceService) GenerateFileURL(
func (s EvidenceService) ListForTaskID(
ctx context.Context,
taskID gid.GID,
cursor *page.Cursor,
) (*page.Page[*coredata.Evidence], error) {
cursor *page.Cursor[coredata.EvidenceOrderField],
) (*page.Page[*coredata.Evidence, coredata.EvidenceOrderField], error) {
var evidences coredata.Evidences
err := s.svc.pg.WithConn(

View File

@@ -106,8 +106,8 @@ func (s FrameworkService) Create(
func (s FrameworkService) ListForOrganizationID(
ctx context.Context,
organizationID gid.GID,
cursor *page.Cursor,
) (*page.Page[*coredata.Framework], error) {
cursor *page.Cursor[coredata.FrameworkOrderField],
) (*page.Page[*coredata.Framework, coredata.FrameworkOrderField], error) {
var frameworks coredata.Frameworks
err := s.svc.pg.WithConn(

View File

@@ -71,8 +71,8 @@ func (s PeopleService) Get(
func (s PeopleService) ListForOrganizationID(
ctx context.Context,
organizationID gid.GID,
cursor *page.Cursor,
) (*page.Page[*coredata.People], error) {
cursor *page.Cursor[coredata.PeopleOrderField],
) (*page.Page[*coredata.People, coredata.PeopleOrderField], error) {
var peoples coredata.Peoples
err := s.svc.pg.WithConn(

View File

@@ -145,8 +145,8 @@ func (s *PolicyService) Delete(
func (s *PolicyService) ListByOrganizationID(
ctx context.Context,
organizationID gid.GID,
cursor *page.Cursor,
) (*page.Page[*coredata.Policy], error) {
cursor *page.Cursor[coredata.PolicyOrderField],
) (*page.Page[*coredata.Policy, coredata.PolicyOrderField], error) {
var policies coredata.Policies
err := s.svc.pg.WithConn(

View File

@@ -185,8 +185,8 @@ func (s TaskService) Update(
func (s TaskService) ListForControlID(
ctx context.Context,
controlID gid.GID,
cursor *page.Cursor,
) (*page.Page[*coredata.Task], error) {
cursor *page.Cursor[coredata.TaskOrderField],
) (*page.Page[*coredata.Task, coredata.TaskOrderField], error) {
var tasks coredata.Tasks
err := s.svc.pg.WithConn(

View File

@@ -61,8 +61,8 @@ type (
func (s VendorService) ListForOrganizationID(
ctx context.Context,
organizationID gid.GID,
cursor *page.Cursor,
) (*page.Page[*coredata.Vendor], error) {
cursor *page.Cursor[coredata.VendorOrderField],
) (*page.Page[*coredata.Vendor, coredata.VendorOrderField], error) {
var vendors coredata.Vendors
err := s.svc.pg.WithConn(

View File

@@ -511,8 +511,8 @@ func (s Service) ConfirmEmail(ctx context.Context, tokenString string) error {
func (s Service) ListUsersForTenant(
ctx context.Context,
organizationID gid.GID,
cursor *page.Cursor,
) (*page.Page[*coredata.User], error) {
cursor *page.Cursor[coredata.UserOrderField],
) (*page.Page[*coredata.User, coredata.UserOrderField], error) {
users := coredata.Users{}
err := s.pg.WithConn(