Introduce global id

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-01-23 14:06:39 +01:00
parent 4c95240959
commit 0b5ad00488
7 changed files with 134 additions and 10 deletions

View File

@@ -20,15 +20,17 @@ import (
"maps"
"time"
"github.com/getprobo/probo/pkg/probo/coredata/gid"
"github.com/getprobo/probo/pkg/probo/coredata/page"
"github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg"
)
type (
Control struct {
ID uuid.UUID
ID gid.GID
FrameworkID string
Name string
Description string
@@ -41,7 +43,7 @@ type (
)
func (c Control) CursorKey() page.CursorKey {
return page.NewCursorKey(c.ID, c.CreatedAt)
return page.NewCursorKey(uuid.UUID(c.ID), c.CreatedAt)
}
func (c *Control) scan(r pgx.Row) error {

View File

@@ -0,0 +1,23 @@
// 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
const (
OrganizationEntityType uint32 = iota
FrameworkEntityType
ControlEntityType
TaskEntityType
EvidenceEntityType
)

View File

@@ -14,11 +14,15 @@
package coredata
import "time"
import (
"time"
"github.com/getprobo/probo/pkg/probo/coredata/gid"
)
type (
Evidence struct {
ID string
ID gid.GID
TaskID string
ContentID string
CreatedAt time.Time

View File

@@ -20,15 +20,16 @@ import (
"maps"
"time"
"github.com/getprobo/probo/pkg/probo/coredata/gid"
"github.com/getprobo/probo/pkg/probo/coredata/page"
"github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg"
"go.gearno.de/crypto/uuid"
)
type (
Framework struct {
ID uuid.UUID
ID gid.GID
OrganizationID string
Name string
Description string
@@ -41,7 +42,7 @@ type (
)
func (f Framework) CursorKey() page.CursorKey {
return page.NewCursorKey(f.ID, f.CreatedAt)
return page.NewCursorKey(uuid.UUID(f.ID), f.CreatedAt)
}
func (f *Framework) scan(r pgx.Row) error {

View File

@@ -0,0 +1,91 @@
// 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 gid
import (
"database/sql/driver"
"encoding/base64"
"encoding/binary"
"fmt"
"go.gearno.de/crypto/uuid"
)
type (
GID uuid.UUID
)
func NewGID(et uint32) (GID, error) {
id, err := uuid.NewV7()
if err != nil {
return GID(uuid.Nil), err
}
binary.BigEndian.PutUint32(id[10:14], et)
return GID(id), nil
}
func (gid GID) Value() (driver.Value, error) {
return gid[:], nil
}
func (gid GID) EntityType() uint32 {
return binary.BigEndian.Uint32(gid[10:14])
}
func (gid *GID) Scan(value interface{}) error {
switch v := value.(type) {
case string:
id, err := uuid.Parse(v)
if err != nil {
return err
}
*gid = GID(id)
case []byte:
id, err := uuid.ParseBytes(v)
if err != nil {
return err
}
*gid = GID(id)
default:
return fmt.Errorf("invalid type for GID: expected string or []byte, got %T", value)
}
return nil
}
func (gid GID) String() string {
return base64.RawURLEncoding.EncodeToString(gid[:])
}
func (gid GID) MarshalText() ([]byte, error) {
enc := base64.RawURLEncoding
buf := make([]byte, enc.EncodedLen(len(gid)))
enc.Encode(buf, gid[:])
return buf, nil
}
func (gid *GID) UnmarshalText(encoded []byte) error {
enc := base64.RawURLEncoding
_, err := enc.Decode(gid[:], encoded)
if err != nil {
return err
}
return nil
}

View File

@@ -20,13 +20,14 @@ import (
"maps"
"time"
"github.com/getprobo/probo/pkg/probo/coredata/gid"
"github.com/jackc/pgx/v5"
"go.gearno.de/kit/pg"
)
type (
Organization struct {
ID string
ID gid.GID
Name string
CreatedAt time.Time
UpdatedAt time.Time

View File

@@ -20,15 +20,17 @@ import (
"maps"
"time"
"github.com/getprobo/probo/pkg/probo/coredata/gid"
"github.com/getprobo/probo/pkg/probo/coredata/page"
"github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg"
)
type (
Task struct {
ID uuid.UUID
ID gid.GID
ControlID string
ContentRef string
CreatedAt time.Time
@@ -39,7 +41,7 @@ type (
)
func (t Task) CursorKey() page.CursorKey {
return page.NewCursorKey(t.ID, t.CreatedAt)
return page.NewCursorKey(uuid.UUID(t.ID), t.CreatedAt)
}
func (t *Task) scan(r pgx.Row) error {