Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ if result.Valid {
```go
// Extract GTS ID from JSON content
content := map[string]any{
"gtsId": "gts.vendor.pkg.ns.type.v1.0",
"id": "gts.vendor.pkg.ns.type.v1.0",
"name": "My Entity",
}

Expand Down Expand Up @@ -131,7 +131,7 @@ store := gts.NewGtsStore(nil)

// Register an entity
entity := gts.NewJsonEntity(map[string]any{
"gtsId": "gts.vendor.pkg.ns.type.v1.0",
"id": "gts.vendor.pkg.ns.type.v1.0",
"name": "My Entity",
}, gts.DefaultGtsConfig())

Expand Down
4 changes: 2 additions & 2 deletions cmd/gts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The commands are:

validate-id validate a GTS ID format
parse-id parse a GTS ID into its components
match-id match a GTS ID against a pattern
match-id-pattern match a GTS ID against a pattern
uuid generate UUID from a GTS ID
validate validate an instance against its schema
relationships resolve relationships for an entity
Expand Down Expand Up @@ -80,7 +80,7 @@ func (c *Command) Runnable() bool {
var commands = []*Command{
cmdValidateID,
cmdParseID,
cmdMatchID,
cmdMatchIDPattern,
cmdUUID,
cmdValidate,
cmdRelationships,
Expand Down
16 changes: 8 additions & 8 deletions cmd/gts/match_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import (
"github.com/GlobalTypeSystem/gts-go/gts"
)

var cmdMatchID = &Command{
UsageLine: "match-id -pattern <pattern> -candidate <gts-id>",
var cmdMatchIDPattern = &Command{
UsageLine: "match-id-pattern -pattern <pattern> -candidate <gts-id>",
Short: "match a GTS ID against a pattern",
Long: `
Match-id checks whether a GTS identifier matches a pattern.
Match-id-pattern checks whether a GTS identifier matches a pattern.

The -pattern flag specifies the pattern (may contain wildcards).
The -candidate flag specifies the GTS ID to match.

Example:

gts match-id -pattern "gts.vendor.pkg.*" -candidate gts.vendor.pkg.ns.type.v1.0
gts match-id-pattern -pattern "gts.vendor.pkg.*" -candidate gts.vendor.pkg.ns.type.v1.0
`,
}

Expand All @@ -30,12 +30,12 @@ var (
)

func init() {
cmdMatchID.Run = runMatchID
cmdMatchID.Flag.StringVar(&matchPattern, "pattern", "", "pattern to match against")
cmdMatchID.Flag.StringVar(&matchCandidate, "candidate", "", "candidate GTS ID")
cmdMatchIDPattern.Run = runMatchIDPattern
cmdMatchIDPattern.Flag.StringVar(&matchPattern, "pattern", "", "pattern to match against")
cmdMatchIDPattern.Flag.StringVar(&matchCandidate, "candidate", "", "candidate GTS ID")
}

func runMatchID(cmd *Command, args []string) {
func runMatchIDPattern(cmd *Command, args []string) {
if matchPattern == "" || matchCandidate == "" {
cmd.Usage()
}
Expand Down
44 changes: 23 additions & 21 deletions gts/cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestCast_MinorVersionUpcast(t *testing.T) {

// Register base event schema
baseSchema := map[string]any{
"$id": "gts.x.core.events.type.v1~",
"$id": "gts://gts.x.core.events.type.v1~",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": []any{"id", "type", "tenantId", "occurredAt"},
Expand Down Expand Up @@ -110,6 +110,7 @@ func TestCast_MinorVersionUpcast(t *testing.T) {

// Register v1.0 instance
v10Instance := map[string]any{
"gtsId": "gts.x.core.events.type.v1~x.commerce.orders.order_placed.v1.0~x.vendor._.inst.v1.0",
"type": "gts.x.core.events.type.v1~x.commerce.orders.order_placed.v1.0~",
"id": "af0e3c1b-8f1e-4a27-9a9b-b7b9b70c1f01",
"tenantId": "11111111-2222-3333-4444-555555555555",
Expand All @@ -135,7 +136,7 @@ func TestCast_MinorVersionUpcast(t *testing.T) {

// Cast from v1.0 to v1.1
result, err := store.Cast(
"gts.x.core.events.type.v1~x.commerce.orders.order_placed.v1.0~",
"gts.x.core.events.type.v1~x.commerce.orders.order_placed.v1.0~x.vendor._.inst.v1.0",
"gts.x.core.events.type.v1~x.commerce.orders.order_placed.v1.1~",
)

Expand Down Expand Up @@ -265,6 +266,7 @@ func TestCast_MinorVersionDowncast(t *testing.T) {

// Register v1.1 instance
v11Instance := map[string]any{
"gtsId": "gts.x.core.events.type.v1~x.test9.cast.event.v1.1~x.vendor._.inst.v1.0",
"type": "gts.x.core.events.type.v1~x.test9.cast.event.v1.1~",
"id": "8b2e3f45-6789-50bc-0123-bcdef234567",
"tenantId": "22222222-3333-4444-5555-666666666666",
Expand All @@ -281,7 +283,7 @@ func TestCast_MinorVersionDowncast(t *testing.T) {

// Cast from v1.1 to v1.0 (downcast)
result, err := store.Cast(
"gts.x.core.events.type.v1~x.test9.cast.event.v1.1~",
"gts.x.core.events.type.v1~x.test9.cast.event.v1.1~x.vendor._.inst.v1.0",
"gts.x.core.events.type.v1~x.test9.cast.event.v1.0~",
)

Expand Down Expand Up @@ -373,9 +375,8 @@ func TestCast_NestedObjects(t *testing.T) {

// Register v1.0 instance
v10Instance := map[string]any{
"gtsId": "gts.x.core.nested.type.v1.0",
"$schema": "gts.x.core.nested.type.v1.0~",
"id": "test-123",
"gtsId": "gts.x.core.nested.type.v1.0~a.b.c.d.v1",
"id": "test-123",
"details": map[string]any{
"name": "John",
},
Expand All @@ -386,7 +387,7 @@ func TestCast_NestedObjects(t *testing.T) {
}

// Cast from v1.0 to v1.1
result, err := store.Cast("gts.x.core.nested.type.v1.0", "gts.x.core.nested.type.v1.1~")
result, err := store.Cast("gts.x.core.nested.type.v1.0~a.b.c.d.v1", "gts.x.core.nested.type.v1.1~")

if err != nil {
t.Fatalf("Cast failed: %v", err)
Expand Down Expand Up @@ -478,8 +479,8 @@ func TestCast_ArrayOfObjects(t *testing.T) {

// Register v1.0 instance with array
v10Instance := map[string]any{
"gtsId": "gts.x.core.array.type.v1.0",
"$schema": "gts.x.core.array.type.v1.0~",
"gtsId": "gts.x.core.array.type.v1.0~a.b.c.d.v1.0",
"type": "gts.x.core.array.type.v1.0~",
"items": []any{
map[string]any{"id": "item1"},
map[string]any{"id": "item2"},
Expand All @@ -491,7 +492,7 @@ func TestCast_ArrayOfObjects(t *testing.T) {
}

// Cast from v1.0 to v1.1
result, err := store.Cast("gts.x.core.array.type.v1.0", "gts.x.core.array.type.v1.1~")
result, err := store.Cast("gts.x.core.array.type.v1.0~a.b.c.d.v1.0", "gts.x.core.array.type.v1.1~")

if err != nil {
t.Fatalf("Cast failed: %v", err)
Expand Down Expand Up @@ -524,7 +525,7 @@ func TestCast_ArrayOfObjects(t *testing.T) {
func TestCast_InstanceNotFound(t *testing.T) {
store := NewGtsStore(nil)

_, err := store.Cast("gts.x.nonexistent.instance.v1.0", "gts.x.nonexistent.schema.v1.1~")
_, err := store.Cast("gts.x.nonexistent.type.v1~a.b.c.d.v1.0", "gts.x.nonexistent.schema.v1.1~")

if err == nil {
t.Error("Expected error for non-existent instance")
Expand All @@ -536,7 +537,7 @@ func TestCast_SchemaNotFound(t *testing.T) {

// Register schema first
schema := map[string]any{
"$id": "gts.x.core.test.type.v1.0~",
"$id": "gts://gts.x.core.test.type.v1.0~",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": []any{"id"},
Expand All @@ -551,15 +552,16 @@ func TestCast_SchemaNotFound(t *testing.T) {

// Register instance with schema
instance := map[string]any{
"$schema": "gts.x.core.test.type.v1.0~",
"id": "test-123",
"gtsId": "gts.x.core.test.type.v1.0~a.b.c.d.v1.0",
"type": "gts.x.core.test.type.v1.0~",
"id": "test-123",
}
instanceEntity := NewJsonEntity(instance, DefaultGtsConfig())
if err := store.Register(instanceEntity); err != nil {
t.Fatalf("Failed to register instance: %v", err)
}

_, err := store.Cast("gts.x.core.test.type.v1.0~", "gts.x.nonexistent.schema.v1.1~")
_, err := store.Cast("gts.x.core.test.type.v1.0~a.b.c.d.v1.0", "gts.x.nonexistent.schema.v1.1~")

if err == nil {
t.Error("Expected error for non-existent target schema")
Expand All @@ -571,7 +573,7 @@ func TestCast_MissingRequiredFieldNoDefault(t *testing.T) {

// Register v1.0 schema
v10Schema := map[string]any{
"$id": "gts.x.core.required.type.v1.0~",
"$id": "gts://gts.x.core.required.type.v1.0~",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": []any{"id"},
Expand All @@ -586,7 +588,7 @@ func TestCast_MissingRequiredFieldNoDefault(t *testing.T) {

// Register v1.1 schema with new required field WITHOUT default
v11Schema := map[string]any{
"$id": "gts.x.core.required.type.v1.1~",
"$id": "gts://gts.x.core.required.type.v1.1~",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": []any{"id", "newRequired"},
Expand All @@ -602,17 +604,17 @@ func TestCast_MissingRequiredFieldNoDefault(t *testing.T) {

// Register v1.0 instance
v10Instance := map[string]any{
"gtsId": "gts.x.core.required.type.v1.0",
"$schema": "gts.x.core.required.type.v1.0~",
"id": "test-123",
"gtsId": "gts.x.core.required.type.v1.0~a.b.c.d.v1.0",
"type": "gts.x.core.required.type.v1.0~",
"id": "test-123",
}
v10InstanceEntity := NewJsonEntity(v10Instance, DefaultGtsConfig())
if err := store.Register(v10InstanceEntity); err != nil {
t.Fatalf("Failed to register v1.0 instance: %v", err)
}

// Cast from v1.0 to v1.1 should fail
result, err := store.Cast("gts.x.core.required.type.v1.0", "gts.x.core.required.type.v1.1~")
result, err := store.Cast("gts.x.core.required.type.v1.0~a.b.c.d.v1.0", "gts.x.core.required.type.v1.1~")

if err != nil {
t.Fatalf("Cast should not error at top level: %v", err)
Expand Down
5 changes: 2 additions & 3 deletions gts/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func DefaultGtsConfig() *GtsConfig {
return &GtsConfig{
EntityIDFields: []string{
"$id",
"$$id",
"gtsId",
"gtsIid",
"gtsOid",
Expand All @@ -27,12 +26,12 @@ func DefaultGtsConfig() *GtsConfig {
"id",
},
SchemaIDFields: []string{
"$schema",
"$$schema",
"gtsTid",
"gtsType",
"gtsT",
"gts_t",
"gts_tid",
"gts_type",
"type",
"schema",
},
Expand Down
Loading
Loading