Skip to content

Commit c2d93e2

Browse files
Merge pull request #3407 from hashicorp/fromparse
`tools/generator-go-sdk` - add FromParseResult on id generator
2 parents 0dbebc2 + a110d30 commit c2d93e2

File tree

2 files changed

+78
-57
lines changed

2 files changed

+78
-57
lines changed

tools/generator-go-sdk/generator/templater_id_parser.go

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ func (r resourceIdTemplater) methods() (*string, error) {
115115
methods = append(methods, *functionBody)
116116
}
117117

118+
functionBody, err = r.fromParseResultFunction(r.resource.Segments)
119+
if err != nil {
120+
return nil, fmt.Errorf("generating the fromParseResult function: %+v", err)
121+
}
122+
methods = append(methods, *functionBody)
123+
118124
// Validate function
119125
methods = append(methods, r.validateFunction(nameWithoutSuffix))
120126

@@ -264,9 +270,36 @@ func (r resourceIdTemplater) parseFunction(nameWithoutSuffix string, caseSensiti
264270
functionName += "Insensitively"
265271
}
266272

273+
description := fmt.Sprintf("// %[1]s parses 'input' into a %[2]s", functionName, r.name)
274+
if !caseSensitive {
275+
description = fmt.Sprintf(`
276+
// %[1]s parses 'input' case-insensitively into a %[2]s
277+
// note: this method should only be used for API response data and not user input`, functionName, r.name)
278+
}
279+
280+
out := fmt.Sprintf(`%[4]s
281+
func %[1]s(input string) (*%[2]s, error) {
282+
parser := resourceids.NewParserFromResourceIdType(%[2]s{})
283+
parsed, err := parser.Parse(input, %[3]t)
284+
if err != nil {
285+
return nil, fmt.Errorf("parsing %%q: %%+v", input, err)
286+
}
287+
288+
id := %[2]s{}
289+
if err := id.FromParseResult(*parsed); err != nil {
290+
return nil, err
291+
}
292+
293+
return &id, nil
294+
}`, functionName, r.name, !caseSensitive, description)
295+
return &out, nil
296+
}
297+
298+
func (r resourceIdTemplater) fromParseResultFunction(segments []resourcemanager.ResourceIdSegment) (*string, error) {
299+
267300
lines := make([]string, 0)
268301
varDeclaration := ""
269-
for _, segment := range r.resource.Segments {
302+
for _, segment := range segments {
270303
switch segment.Type {
271304
case resourcemanager.ConstantSegment:
272305
{
@@ -276,14 +309,14 @@ func (r resourceIdTemplater) parseFunction(nameWithoutSuffix string, caseSensiti
276309

277310
lines = append(lines, fmt.Sprintf(`
278311
279-
if v, ok := parsed.Parsed[%[1]q]; true {
312+
if v, ok := input.Parsed[%[1]q]; true {
280313
if !ok {
281-
return nil, resourceids.NewSegmentNotSpecifiedError(id, %[1]q, *parsed)
314+
return resourceids.NewSegmentNotSpecifiedError(id, %[1]q, input)
282315
}
283316
284317
%[1]s, err := parse%[3]s(v)
285318
if err != nil {
286-
return nil, fmt.Errorf("parsing %%q: %%+v", v, err)
319+
return fmt.Errorf("parsing %%q: %%+v", v, err)
287320
}
288321
id.%[2]s = *%[1]s
289322
}
@@ -294,8 +327,8 @@ func (r resourceIdTemplater) parseFunction(nameWithoutSuffix string, caseSensiti
294327
case resourcemanager.ResourceGroupSegment, resourcemanager.ScopeSegment, resourcemanager.SubscriptionIdSegment, resourcemanager.UserSpecifiedSegment:
295328
{
296329
lines = append(lines, fmt.Sprintf(`
297-
if id.%[2]s, ok = parsed.Parsed[%[1]q]; !ok {
298-
return nil, resourceids.NewSegmentNotSpecifiedError(id, %[1]q, *parsed)
330+
if id.%[2]s, ok = input.Parsed[%[1]q]; !ok {
331+
return resourceids.NewSegmentNotSpecifiedError(id, %[1]q, input)
299332
}
300333
`, segment.Name, strings.Title(segment.Name)))
301334

@@ -308,29 +341,13 @@ func (r resourceIdTemplater) parseFunction(nameWithoutSuffix string, caseSensiti
308341
continue
309342
}
310343
}
344+
out := fmt.Sprintf(`func (id *%[1]s) FromParseResult(input resourceids.ParseResult) error {
345+
%[2]s
311346
312-
description := fmt.Sprintf("// %[1]s parses 'input' into a %[2]s", functionName, r.name)
313-
if !caseSensitive {
314-
description = fmt.Sprintf(`
315-
// %[1]s parses 'input' case-insensitively into a %[2]s
316-
// note: this method should only be used for API response data and not user input`, functionName, r.name)
317-
}
318-
319-
out := fmt.Sprintf(`%[5]s
320-
func %[1]s(input string) (*%[2]s, error) {
321-
parser := resourceids.NewParserFromResourceIdType(%[2]s{})
322-
parsed, err := parser.Parse(input, %[3]t)
323-
if err != nil {
324-
return nil, fmt.Errorf("parsing %%q: %%+v", input, err)
325-
}
326-
327-
%[6]s
328-
id := %[2]s{}
347+
%[3]s
348+
return nil
349+
}`, r.name, varDeclaration, strings.Join(lines, "\n"))
329350

330-
%[4]s
331-
332-
return &id, nil
333-
}`, functionName, r.name, !caseSensitive, strings.Join(lines, "\n"), description, varDeclaration)
334351
return &out, nil
335352
}
336353

tools/generator-go-sdk/generator/templater_id_parser_test.go

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,10 @@ var _ resourceids.ResourceId = BasicTestId{}
7171
return nil, fmt.Errorf("parsing %q: %+v", input, err)
7272
}
7373
74-
var ok bool
7574
id := BasicTestId{}
76-
if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok {
77-
return nil, resourceids.NewSegmentNotSpecifiedError(id, "subscriptionId", *parsed)
78-
}
75+
if err := id.FromParseResult(*parsed); err != nil {
76+
return nil, err
77+
}
7978
8079
return &id, nil
8180
}
@@ -89,15 +88,24 @@ var _ resourceids.ResourceId = BasicTestId{}
8988
return nil, fmt.Errorf("parsing %q: %+v", input, err)
9089
}
9190
92-
var ok bool
9391
id := BasicTestId{}
94-
if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok {
95-
return nil, resourceids.NewSegmentNotSpecifiedError(id, "subscriptionId", *parsed)
96-
}
92+
if err := id.FromParseResult(*parsed); err != nil {
93+
return nil, err
94+
}
9795
9896
return &id, nil
9997
}
10098
99+
func (id *BasicTestId) FromParseResult(input resourceids.ParseResult) error {
100+
var ok bool
101+
102+
if id.SubscriptionId, ok = input.Parsed["subscriptionId"]; !ok {
103+
return resourceids.NewSegmentNotSpecifiedError(id, "subscriptionId", input)
104+
}
105+
106+
return nil
107+
}
108+
101109
// ValidateBasicTestID checks that 'input' can be parsed as a Basic Test ID
102110
func ValidateBasicTestID(input interface{}, key string) (warnings []string, errors []error) {
103111
v, ok := input.(string)
@@ -219,17 +227,8 @@ func ParseConstantOnlyID(input string) (*ConstantOnlyId, error) {
219227
}
220228
221229
id := ConstantOnlyId{}
222-
223-
if v, ok := parsed.Parsed["thingId"]; true {
224-
if !ok {
225-
return nil, resourceids.NewSegmentNotSpecifiedError(id, "thingId", *parsed)
226-
}
227-
228-
thingId, err := parseThing(v)
229-
if err != nil {
230-
return nil, fmt.Errorf("parsing %q: %+v", v, err)
231-
}
232-
id.ThingId = *thingId
230+
if err := id.FromParseResult(*parsed); err != nil {
231+
return nil, err
233232
}
234233
235234
return &id, nil
@@ -245,22 +244,27 @@ func ParseConstantOnlyIDInsensitively(input string) (*ConstantOnlyId, error) {
245244
}
246245
247246
id := ConstantOnlyId{}
248-
249-
if v, ok := parsed.Parsed["thingId"]; true {
250-
if !ok {
251-
return nil, resourceids.NewSegmentNotSpecifiedError(id, "thingId", *parsed)
252-
}
253-
254-
thingId, err := parseThing(v)
255-
if err != nil {
256-
return nil, fmt.Errorf("parsing %q: %+v", v, err)
257-
}
258-
id.ThingId = *thingId
247+
if err := id.FromParseResult(*parsed); err != nil {
248+
return nil, err
259249
}
260250
261251
return &id, nil
262252
}
263253
254+
func (id *ConstantOnlyId) FromParseResult(input resourceids.ParseResult) error {
255+
if v, ok := input.Parsed["thingId"]; true {
256+
if !ok {
257+
return resourceids.NewSegmentNotSpecifiedError(id, "thingId", input)
258+
}
259+
thingId, err := parseThing(v)
260+
if err != nil {
261+
return fmt.Errorf("parsing %q: %+v", v, err)
262+
}
263+
id.ThingId = *thingId
264+
}
265+
return nil
266+
}
267+
264268
// ValidateConstantOnlyID checks that 'input' can be parsed as a Constant Only ID
265269
func ValidateConstantOnlyID(input interface{}, key string) (warnings []string, errors []error) {
266270
v, ok := input.(string)

0 commit comments

Comments
 (0)