-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package kong | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gofrs/uuid" | ||
) | ||
|
||
// IDFillableEntitiesT is a type constraint for entities that can be filled with an ID. | ||
type IDFillableEntitiesT interface { | ||
Service | Route | ||
} | ||
|
||
// FillEntityID fills the ID of an entity. It is a no-op if the entity already has an ID. | ||
// ID is generated in a deterministic way using UUIDv5. The UUIDv5 namespace is different for each entity type. | ||
// The name used to generate the UUIDv5 is: | ||
// - for Service: Service.Name | ||
// - for Route: Route.Name | ||
func FillEntityID[T IDFillableEntitiesT](entity *T) error { | ||
switch e := any(entity).(type) { | ||
case *Service: | ||
return fillIDForService(e) | ||
case *Route: | ||
return fillIDForRoute(e) | ||
default: | ||
return fmt.Errorf("unsupported entity: '%T'", entity) | ||
} | ||
} | ||
|
||
// The following variables are UUIDv5 namespaces used to generate IDs for entities, one per each entity type. | ||
var ( | ||
serviceIDsNamespace = uuid.Must(uuid.FromString("497d0cb2-5630-40a7-9858-8f210e6295f4")) | ||
routeIDsNamespace = uuid.Must(uuid.FromString("6324c31d-0568-480f-8d8c-a5c1f4c8eb6c")) | ||
) | ||
|
||
func fillIDForService(svc *Service) error { | ||
if svc == nil { | ||
return fmt.Errorf("service is nil") | ||
} | ||
if svc.ID != nil { | ||
// ID already set, do nothing. | ||
return nil | ||
} | ||
if svc.Name == nil || *svc.Name == "" { | ||
return fmt.Errorf("service name is required") | ||
} | ||
|
||
id := uuid.NewV5(serviceIDsNamespace, *svc.Name) | ||
svc.ID = String(id.String()) | ||
return nil | ||
} | ||
|
||
func fillIDForRoute(route *Route) error { | ||
if route == nil { | ||
return fmt.Errorf("route is nil") | ||
} | ||
if route.ID != nil { | ||
// ID already set, do nothing. | ||
return nil | ||
} | ||
if route.Name == nil || *route.Name == "" { | ||
return fmt.Errorf("route name is required") | ||
} | ||
|
||
id := uuid.NewV5(routeIDsNamespace, *route.Name) | ||
route.ID = String(id.String()) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package kong_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/kong/go-kong/kong" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type fillEntityIDTestCase[T kong.IDFillableEntitiesT] struct { | ||
name string | ||
entity *T | ||
|
||
assertEntity func(t *testing.T, entity *T) | ||
expectErr bool | ||
} | ||
|
||
func (tc fillEntityIDTestCase[T]) run(t *testing.T) { | ||
t.Run(tc.name, func(t *testing.T) { | ||
err := kong.FillEntityID(tc.entity) | ||
if tc.expectErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
tc.assertEntity(t, tc.entity) | ||
}) | ||
} | ||
|
||
func TestFillEntityID(t *testing.T) { | ||
t.Run("Service", func(t *testing.T) { | ||
testCases := []fillEntityIDTestCase[kong.Service]{ | ||
{ | ||
name: "service nil pointer", | ||
entity: (*kong.Service)(nil), | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "service with nil name", | ||
entity: &kong.Service{}, | ||
assertEntity: func(t *testing.T, svc *kong.Service) { | ||
require.Nil(t, svc.ID, "ID should not be set when name is nil") | ||
}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "service with empty name", | ||
entity: &kong.Service{Name: kong.String("")}, | ||
assertEntity: func(t *testing.T, svc *kong.Service) { | ||
require.Nil(t, svc.ID, "ID should not be set when name is empty") | ||
}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "service with name", | ||
entity: &kong.Service{ | ||
Name: kong.String("some.service.name"), | ||
}, | ||
assertEntity: func(t *testing.T, svc *kong.Service) { | ||
require.NotNil(t, svc.ID) | ||
const expectedID = "14a84f13-96ef-5628-b300-8bcd5a509f9b" | ||
require.Equal(t, expectedID, *svc.ID, "ID should be deterministic") | ||
}, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
tc.run(t) | ||
} | ||
}) | ||
|
||
t.Run("Route", func(t *testing.T) { | ||
testCases := []fillEntityIDTestCase[kong.Route]{ | ||
{ | ||
name: "route nil pointer", | ||
entity: (*kong.Route)(nil), | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "route with nil name", | ||
entity: &kong.Route{}, | ||
assertEntity: func(t *testing.T, route *kong.Route) { | ||
require.Nil(t, route.ID, "ID should not be set when name is nil") | ||
}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "route with empty name", | ||
entity: &kong.Route{Name: kong.String("")}, | ||
assertEntity: func(t *testing.T, route *kong.Route) { | ||
require.Nil(t, route.ID, "ID should not be set when name is empty") | ||
}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "route with name", | ||
entity: &kong.Route{ | ||
Name: kong.String("some.service.name"), | ||
}, | ||
assertEntity: func(t *testing.T, route *kong.Route) { | ||
require.NotNil(t, route.ID) | ||
|
||
const expectedID = "9279da23-17de-5cca-b1da-a60d7cec6802" | ||
require.Equal(t, expectedID, *route.ID, "ID should be deterministic") | ||
}, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
tc.run(t) | ||
} | ||
}) | ||
} |