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

commonids: add composite resource id #208

Merged
merged 4 commits into from
Mar 20, 2024
Merged

commonids: add composite resource id #208

merged 4 commits into from
Mar 20, 2024

Conversation

catriona-m
Copy link
Member

No description provided.

Copy link
Contributor

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @catriona-m

Thanks for this PR - apologies for the delayed review here.

Taking a look through on the whole this is looking pretty good, if we can fix up the comments then this should otherwise be good to go 👍

Thanks!

Comment on lines 49 to 64
return nil, fmt.Errorf("parsing first id of CompositeResourceID: %v", err)
}
err = output.First.FromParseResult(*firstParseResult)
if err != nil {
return nil, fmt.Errorf("populating first id of CompositeResourceID: %v", err)
}

// Parse the second of the two Resource IDs from the components
secondParser := resourceids.NewParserFromResourceIdType(output.Second)
secondParseResult, err := secondParser.Parse(components[1], insensitively)
if err != nil {
return nil, fmt.Errorf("parsing second id of CompositeResourceID: %v", err)
}
err = output.Second.FromParseResult(*secondParseResult)
if err != nil {
return nil, fmt.Errorf("populating second id of CompositeResourceID: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we output the parsed id part as a part of the error to make this clearer?

Suggested change
return nil, fmt.Errorf("parsing first id of CompositeResourceID: %v", err)
}
err = output.First.FromParseResult(*firstParseResult)
if err != nil {
return nil, fmt.Errorf("populating first id of CompositeResourceID: %v", err)
}
// Parse the second of the two Resource IDs from the components
secondParser := resourceids.NewParserFromResourceIdType(output.Second)
secondParseResult, err := secondParser.Parse(components[1], insensitively)
if err != nil {
return nil, fmt.Errorf("parsing second id of CompositeResourceID: %v", err)
}
err = output.Second.FromParseResult(*secondParseResult)
if err != nil {
return nil, fmt.Errorf("populating second id of CompositeResourceID: %v", err)
return nil, fmt.Errorf("parsing first id part %q of CompositeResourceID: %v", components[0], err)
}
err = output.First.FromParseResult(*firstParseResult)
if err != nil {
return nil, fmt.Errorf("populating first id part %q of CompositeResourceID: %v", components[0], err)
}
// Parse the second of the two Resource IDs from the components
secondParser := resourceids.NewParserFromResourceIdType(output.Second)
secondParseResult, err := secondParser.Parse(components[1], insensitively)
if err != nil {
return nil, fmt.Errorf("parsing second id part %q of CompositeResourceID: %v", components[1], err)
}
err = output.Second.FromParseResult(*secondParseResult)
if err != nil {
return nil, fmt.Errorf("populating second id part %q of CompositeResourceID: %v", components[1], err)

return Parse(input, first, second, true)
}

func Parse[T1 resourceids.ResourceId, T2 resourceids.ResourceId](input string, first T1, second T2, insensitively bool) (*CompositeResourceID[T1, T2], error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given we're providing wrapper functions above, does this method need to be public?

Comment on lines 10 to 29
type CompositeResourceID[T1 resourceids.ResourceId, T2 resourceids.ResourceId] struct {
First T1
Second T2
}

func (id CompositeResourceID[T1, T2]) ID() string {
fmtString := "%s|%s"
return fmt.Sprintf(fmtString, id.First.ID(), id.Second.ID())
}

func (id CompositeResourceID[T1, T2]) String() string {
fmtString := "%s\n%s"
return fmt.Sprintf(fmtString, id.First.String(), id.Second.String())
}

func ParseCompositeResourceID[T1 resourceids.ResourceId, T2 resourceids.ResourceId](input string, first T1, second T2) (*CompositeResourceID[T1, T2], error) {
return Parse(input, first, second, false)
}

func ParseCompositeResourceIDInsensitively[T1 resourceids.ResourceId, T2 resourceids.ResourceId](input string, first T1, second T2) (*CompositeResourceID[T1, T2], error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we document this struct/the functions to explain how this is intended to be used?

Copy link
Contributor

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few minor comments inline but otherwise 👍

Comment on lines 22 to 26
// String returns a human-readable description of this Composite Resource Id
func (id CompositeResourceID[T1, T2]) String() string {
fmtString := "%s\n%s"
return fmt.Sprintf(fmtString, id.First.String(), id.Second.String())
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call out that this is a Composite Resource ID:

Suggested change
// String returns a human-readable description of this Composite Resource Id
func (id CompositeResourceID[T1, T2]) String() string {
fmtString := "%s\n%s"
return fmt.Sprintf(fmtString, id.First.String(), id.Second.String())
}
// String returns a human-readable description of this Composite Resource Id
func (id CompositeResourceID[T1, T2]) String() string {
fmtString := "Composite Resource ID (%s | %s)"
return fmt.Sprintf(fmtString, id.First.String(), id.Second.String())
}

Comment on lines 39 to 48
return parse(input, first, second, false)
}

// ParseCompositeResourceIDInsensitively parses 'input' and two ResourceIds (first,second) case-insensitively into a CompositeResourceID
// note: this method should only be used for API response data and not user input
func ParseCompositeResourceIDInsensitively[T1 resourceids.ResourceId, T2 resourceids.ResourceId](input string, first T1, second T2) (*CompositeResourceID[T1, T2], error) {
return parse(input, first, second, true)
}

func parse[T1 resourceids.ResourceId, T2 resourceids.ResourceId](input string, first T1, second T2, insensitively bool) (*CompositeResourceID[T1, T2], error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor but could we update parse to be parseCompositeResourceID so that this isn't used unintentionally?

Suggested change
return parse(input, first, second, false)
}
// ParseCompositeResourceIDInsensitively parses 'input' and two ResourceIds (first,second) case-insensitively into a CompositeResourceID
// note: this method should only be used for API response data and not user input
func ParseCompositeResourceIDInsensitively[T1 resourceids.ResourceId, T2 resourceids.ResourceId](input string, first T1, second T2) (*CompositeResourceID[T1, T2], error) {
return parse(input, first, second, true)
}
func parse[T1 resourceids.ResourceId, T2 resourceids.ResourceId](input string, first T1, second T2, insensitively bool) (*CompositeResourceID[T1, T2], error) {
return parseCompositeResourceID(input, first, second, false)
}
// ParseCompositeResourceIDInsensitively parses 'input' and two ResourceIds (first,second) case-insensitively into a CompositeResourceID
// note: this method should only be used for API response data and not user input
func ParseCompositeResourceIDInsensitively[T1 resourceids.ResourceId, T2 resourceids.ResourceId](input string, first T1, second T2) (*CompositeResourceID[T1, T2], error) {
return parseCompositeResourceID(input, first, second, true)
}
func parseCompositeResourceID[T1 resourceids.ResourceId, T2 resourceids.ResourceId](input string, first T1, second T2, insensitively bool) (*CompositeResourceID[T1, T2], error) {

Comment on lines 12 to 13
First T1
Second T2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor but could we document these fields so that it's clearer what these are?

Suggested change
First T1
Second T2
// First specifies the first component of this Resource ID.
// This is in the format `{first}|{second}`.
First T1
// Second specifies the second component of this Resource ID
// This is in the format `{first}|{second}`.
Second T2

Copy link
Contributor

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One really minor nit for readability purposes but this otherwise LGTM 👍

Co-authored-by: Tom Harvey <tombuildsstuff@users.noreply.github.com>
@catriona-m catriona-m merged commit 547050f into main Mar 20, 2024
2 checks passed
@wuxu92
Copy link
Contributor

wuxu92 commented Mar 21, 2024

Is there a plan to release this PR and update it in the AzureRM dependency?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants