feat: add StringKSUID and BinaryKSUID types
Replace the UUID wrapper with storage-specific KSUID types that share GraphQL string transit but use distinct SQL Scan/Value encodings. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package dbx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/segmentio/ksuid"
|
||||
)
|
||||
|
||||
type ksuidGQLTransport struct {
|
||||
KSUIDStr string `json:"ksuid_str"`
|
||||
}
|
||||
|
||||
func unmarshalKSUIDFromGQL(value any, unmarshal func([]byte) error) error {
|
||||
str, ok := value.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("GraphQL failed to unmarshal KSUID value: %v", value)
|
||||
}
|
||||
return unmarshal([]byte(str))
|
||||
}
|
||||
|
||||
func marshalKSUIDToGQL(w io.Writer, id ksuid.KSUID) {
|
||||
transport := ksuidGQLTransport{KSUIDStr: id.String()}
|
||||
data, err := json.Marshal(transport)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("GraphQL failed to JSON-marshal KSUID value: %s", err))
|
||||
}
|
||||
if _, err := w.Write(data); err != nil {
|
||||
panic(fmt.Errorf("GraphQL failed to write KSUID value: %s", string(data)))
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
ksuidBinaryLength = 20
|
||||
ksuidStringLength = 27
|
||||
)
|
||||
Reference in New Issue
Block a user