Skip to content

Commit b113e5f

Browse files
mbarnesMatthew Barnes
andauthored
[azcore] Add text [un]marshalling support to ResourceID (Azure#23381)
Co-authored-by: Matthew Barnes <[email protected]>
1 parent 07989a8 commit b113e5f

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

sdk/azcore/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Features Added
66

77
* Added field `OperationLocationResultPath` to `runtime.NewPollerOptions[T]` for LROs that use the `Operation-Location` pattern.
8+
* Support `encoding.TextMarshaler` and `encoding.TextUnmarshaler` interfaces in `arm.ResourceID`.
89

910
### Breaking Changes
1011

sdk/azcore/arm/internal/resource/resource_identifier.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ func (id *ResourceID) String() string {
110110
return id.stringValue
111111
}
112112

113+
// MarshalText returns a textual representation of the ResourceID
114+
func (id *ResourceID) MarshalText() ([]byte, error) {
115+
return []byte(id.String()), nil
116+
}
117+
118+
// UnmarshalText decodes the textual representation of a ResourceID
119+
func (id *ResourceID) UnmarshalText(text []byte) error {
120+
newId, err := ParseResourceID(string(text))
121+
if err != nil {
122+
return err
123+
}
124+
*id = *newId
125+
return nil
126+
}
127+
113128
func newResourceID(parent *ResourceID, resourceTypeName string, resourceName string) *ResourceID {
114129
id := &ResourceID{}
115130
id.init(parent, chooseResourceType(resourceTypeName, parent), resourceName, true)

sdk/azcore/arm/internal/resource/resource_identifier_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
package resource
88

9-
import "testing"
9+
import (
10+
"encoding"
11+
"testing"
12+
)
1013

1114
func TestParseResourceIdentifier(t *testing.T) {
1215
testData := map[string]*ResourceID{
@@ -220,6 +223,26 @@ func TestParseResourceIdentifier(t *testing.T) {
220223
}
221224
}
222225

226+
// Ensure ResourceID implements these interfaces
227+
var _ encoding.TextMarshaler = (*ResourceID)(nil)
228+
var _ encoding.TextUnmarshaler = (*ResourceID)(nil)
229+
230+
func TestMarshalResourceIdentifier(t *testing.T) {
231+
resourceID := &ResourceID{}
232+
input := []byte("/subscriptions/17fecd63-33d8-4e43-ac6f-0aafa111b38d/resourceGroups/myRg/providers/Microsoft.ApiManagement/service/myServiceName/subscriptions/mySubs")
233+
err := resourceID.UnmarshalText(input)
234+
if err != nil {
235+
t.Fatalf("unexpected error: %+v", err)
236+
}
237+
output, err := resourceID.MarshalText()
238+
if err != nil {
239+
t.Fatalf("unexpected error: %+v", err)
240+
}
241+
if string(output) != string(input) {
242+
t.Fatalf("resource id changed, got %v, expected %v", string(output), string(input))
243+
}
244+
}
245+
223246
func equals(left, right *ResourceID) bool {
224247
if left != nil && right != nil {
225248
if left.String() != right.String() {

0 commit comments

Comments
 (0)