Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rid constructors #134

Merged
merged 2 commits into from
Dec 19, 2018
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
29 changes: 26 additions & 3 deletions rid/resource_identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,31 @@ type ResourceIdentifier struct {
Locator string
}

const (
RidClass = "ri"
Separator = "."
)

func MustNew(service, instance, resourceType, locator string) ResourceIdentifier {
resourceIdentifier, err := New(service, instance, resourceType, locator)
if err != nil {
panic(err)
}
return resourceIdentifier
}

func New(service, instance, resourceType, locator string) (ResourceIdentifier, error) {
resourceIdentifier := ResourceIdentifier{
Service: service,
Instance: instance,
Type: resourceType,
Locator: locator,
}
return resourceIdentifier, resourceIdentifier.validate()
}

func (rid ResourceIdentifier) String() string {
return "ri." + rid.Service + "." + rid.Instance + "." + rid.Type + "." + rid.Locator
return RidClass + Separator + rid.Service + Separator + rid.Instance + Separator + rid.Type + Separator + rid.Locator
}

// MarshalText implements encoding.TextMarshaler (used by encoding/json and others).
Expand All @@ -56,8 +79,8 @@ func (rid *ResourceIdentifier) UnmarshalText(text []byte) error {

// ParseRID parses a string into a 4-part resource identifier.
func ParseRID(s string) (ResourceIdentifier, error) {
segments := strings.SplitN(s, ".", 5)
if len(segments) != 5 || segments[0] != "ri" {
segments := strings.SplitN(s, Separator, 5)
if len(segments) != 5 || segments[0] != RidClass {
return ResourceIdentifier{}, errors.New("invalid resource identifier")
}
rid := ResourceIdentifier{
Expand Down
60 changes: 60 additions & 0 deletions rid/resource_identifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,66 @@ func TestResourceIdentifier(t *testing.T) {
},
ExpectedErr: `rid first segment (service) does not match ^[a-z][a-z0-9\-]*$ pattern: rid second segment (instance) does not match ^([a-z0-9][a-z0-9\-]*)?$ pattern: rid third segment (type) does not match ^[a-z][a-z0-9\-]*$ pattern`,
},
{
Name: "service can't be empty",
Input: rid.ResourceIdentifier{
Service: "",
Instance: "b",
Type: "c",
Locator: "d",
},
ExpectedErr: `rid first segment (service) does not match ^[a-z][a-z0-9\-]*$ pattern`,
},
{
Name: "service can't contain '.'",
Input: rid.ResourceIdentifier{
Service: "a.a",
Instance: "b",
Type: "c",
Locator: "d",
},
ExpectedErr: `rid first segment (service) does not match ^[a-z][a-z0-9\-]*$ pattern`,
},
{
Name: "instance can't contain '.'",
Input: rid.ResourceIdentifier{
Service: "a",
Instance: "b.b",
Type: "c",
Locator: "d",
},
ExpectedErr: `rid second segment (instance) does not match ^([a-z0-9][a-z0-9\-]*)?$ pattern`,
},
{
Name: "type can't be empty",
Input: rid.ResourceIdentifier{
Service: "a",
Instance: "b",
Type: "",
Locator: "d",
},
ExpectedErr: `rid third segment (type) does not match ^[a-z][a-z0-9\-]*$ pattern`,
},
{
Name: "type can't contain '.'",
Input: rid.ResourceIdentifier{
Service: "a",
Instance: "b",
Type: "c.c",
Locator: "d",
},
ExpectedErr: `rid third segment (type) does not match ^[a-z][a-z0-9\-]*$ pattern`,
},
{
Name: "locator can't be empty",
Input: rid.ResourceIdentifier{
Service: "a",
Instance: "b",
Type: "c",
Locator: "",
},
ExpectedErr: `rid fourth segment (locator) does not match ^[a-zA-Z0-9\-\._]+$ pattern`,
},
} {
t.Run(test.Name, func(t *testing.T) {
type ridContainer struct {
Expand Down