-
Notifications
You must be signed in to change notification settings - Fork 233
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
Update diag convenience methods #449
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package diag | ||
|
||
import "fmt" | ||
|
||
// FromErr will convert an error into a Diagnostics. This returns Diagnostics | ||
// as the most common use case in Go will be handling a single error | ||
// returned from a function. | ||
// | ||
// if err != nil { | ||
// return diag.FromErr(err) | ||
// } | ||
func FromErr(err error) Diagnostics { | ||
return Diagnostics{ | ||
Diagnostic{ | ||
Severity: Error, | ||
Summary: err.Error(), | ||
}, | ||
} | ||
} | ||
|
||
// Errorf creates a Diagnostics with a single Error level Diagnostic entry. | ||
// The summary is populated by performing a fmt.Sprintf with the supplied | ||
// values. This returns a single error in a Diagnostics as errors typically | ||
// do not occur in multiples as warnings may. | ||
// | ||
// if unexpectedCondition { | ||
// return diag.Errorf("unexpected: %s", someValue) | ||
// } | ||
func Errorf(format string, a ...interface{}) Diagnostics { | ||
return Diagnostics{ | ||
Diagnostic{ | ||
Severity: Error, | ||
Summary: fmt.Sprintf(format, a...), | ||
}, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -243,12 +243,9 @@ func (p *Provider) ValidateResource( | |
// | ||
// This won't be called at all if no provider configuration is given. | ||
func (p *Provider) Configure(ctx context.Context, c *terraform.ResourceConfig) diag.Diagnostics { | ||
|
||
var diags diag.Diagnostics | ||
|
||
// No configuration | ||
if p.ConfigureFunc == nil && p.ConfigureContextFunc == nil { | ||
return diags | ||
return nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I swapped some of these back, I think returning |
||
} | ||
|
||
sm := schemaMap(p.Schema) | ||
|
@@ -257,31 +254,30 @@ func (p *Provider) Configure(ctx context.Context, c *terraform.ResourceConfig) d | |
// generate an intermediary "diff" although that is never exposed. | ||
diff, err := sm.Diff(ctx, nil, c, nil, p.meta, true) | ||
if err != nil { | ||
return append(diags, diag.FromErr(err)) | ||
return diag.FromErr(err) | ||
} | ||
|
||
data, err := sm.Data(nil, diff) | ||
if err != nil { | ||
return append(diags, diag.FromErr(err)) | ||
return diag.FromErr(err) | ||
} | ||
|
||
if p.ConfigureFunc != nil { | ||
meta, err := p.ConfigureFunc(data) | ||
if err != nil { | ||
return append(diags, diag.FromErr(err)) | ||
return diag.FromErr(err) | ||
} | ||
p.meta = meta | ||
} | ||
if p.ConfigureContextFunc != nil { | ||
meta, ds := p.ConfigureContextFunc(ctx, data) | ||
diags = append(diags, ds...) | ||
meta, diags := p.ConfigureContextFunc(ctx, data) | ||
if diags.HasError() { | ||
return diags | ||
} | ||
p.meta = meta | ||
} | ||
|
||
return diags | ||
return nil | ||
} | ||
|
||
// Resources returns all the available resource types that this provider | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall I do like this pattern, however I would still like a convenience to create a singular
Diagnostic
from an error. Would it be crazy to keep the helpers for creating on a single diag, but then have a helper to quickly convert to the collection?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's valuable in scenarios where we have gathered warnings, and encounter a function that just returns a go error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the thing is, that you are burdening the 80% case for the 20% case, and you could easily rewrite that second one as:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish the package was called
diags
with this model, but that would collide with calling your slicediags
.