0f8e79a890
Marshal and unmarshal both use the base62 KSUID string, replacing the legacy JSON object wrapper and aligning gqlgen scalar behavior. Co-authored-by: Cursor <cursoragent@cursor.com>
31 lines
651 B
Go
31 lines
651 B
Go
package dbx
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"strconv"
|
|
|
|
"github.com/segmentio/ksuid"
|
|
)
|
|
|
|
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))
|
|
}
|
|
|
|
// marshalKSUIDToGQL writes a JSON-encoded string scalar for gqlgen.
|
|
func marshalKSUIDToGQL(w io.Writer, id ksuid.KSUID) {
|
|
quoted := strconv.Quote(id.String())
|
|
if _, err := io.WriteString(w, quoted); err != nil {
|
|
panic(fmt.Errorf("GraphQL failed to write KSUID value: %w", err))
|
|
}
|
|
}
|
|
|
|
const (
|
|
ksuidBinaryLength = 20
|
|
ksuidStringLength = 27
|
|
)
|