-
Notifications
You must be signed in to change notification settings - Fork 12
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
feat: Return sane errors from Translate #178
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2f5defc
errors: s/ErrNotAnInstance/ErrInvalidData/g
ce1c611
thema: Split off TranslateErr from Translate
bff274a
thema: Add and use new translation sentinel errors
8070bba
thema: One Translate(), with error return
a0293d6
Add missing error return
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,7 +4,11 @@ import ( | |||||
"fmt" | ||||||
|
||||||
"cuelang.org/go/cue" | ||||||
"cuelang.org/go/cue/errors" | ||||||
cerrors "cuelang.org/go/cue/errors" | ||||||
"cuelang.org/go/pkg/encoding/json" | ||||||
"github.com/cockroachdb/errors" | ||||||
|
||||||
terrors "github.com/grafana/thema/errors" | ||||||
) | ||||||
|
||||||
// BindInstanceType produces a TypedInstance, given an Instance and a | ||||||
|
@@ -94,14 +98,14 @@ func (i *Instance) Dehydrate() *Instance { | |||||
|
||||||
// AsSuccessor translates the instance into the form specified by the successor | ||||||
// schema. | ||||||
func (i *Instance) AsSuccessor() (*Instance, TranslationLacunas) { | ||||||
func (i *Instance) AsSuccessor() (*Instance, TranslationLacunas, error) { | ||||||
i.check() | ||||||
return i.Translate(i.sch.Successor().Version()) | ||||||
} | ||||||
|
||||||
// AsPredecessor translates the instance into the form specified by the predecessor | ||||||
// schema. | ||||||
func (i *Instance) AsPredecessor() (*Instance, TranslationLacunas) { | ||||||
func (i *Instance) AsPredecessor() (*Instance, TranslationLacunas, error) { | ||||||
i.check() | ||||||
return i.Translate(i.sch.Predecessor().Version()) | ||||||
} | ||||||
|
@@ -187,7 +191,11 @@ func (inst *TypedInstance[T]) ValueP() T { | |||||
// result in the exact original data. Input state preservation can be fully | ||||||
// achieved in the program depending on Thema, so we avoid introducing | ||||||
// complexity into Thema that is not essential for all use cases. | ||||||
func (i *Instance) Translate(to SyntacticVersion) (*Instance, TranslationLacunas) { | ||||||
// | ||||||
// Errors only occur in cases where lenses were written in an unexpected way - | ||||||
// for example, not all fields were mapped over, and the resulting object is not | ||||||
// concrete. All errors returned from this func will children of [terrors.ErrInvalidLens]. | ||||||
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.
Suggested change
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. ahhhh whoops missed this, sorry |
||||||
func (i *Instance) Translate(to SyntacticVersion) (*Instance, TranslationLacunas, error) { | ||||||
i.check() | ||||||
|
||||||
// TODO define this in terms of AsSuccessor and AsPredecessor, rather than those in terms of this. | ||||||
|
@@ -208,22 +216,28 @@ func (i *Instance) Translate(to SyntacticVersion) (*Instance, TranslationLacunas | |||||
} | ||||||
|
||||||
if out.Err() != nil { | ||||||
panic(errors.Details(out.Err(), nil)) | ||||||
return nil, nil, errors.Mark(out.Err(), terrors.ErrInvalidLens) | ||||||
} | ||||||
|
||||||
lac := make(multiTranslationLacunas, 0) | ||||||
out.LookupPath(cue.MakePath(cue.Str("lacunas"))).Decode(&lac) | ||||||
|
||||||
// Attempt to evaluate #Translate result into a concrete cue.Value, if possible. | ||||||
// Attempt to evaluate #Translate result to remove intermediate structures created by #Translate. | ||||||
// Otherwise, all the #Translate results are non-concrete, which leads to undesired effects. | ||||||
raw, _ := out.LookupPath(cue.MakePath(cue.Str("result"), cue.Str("result"))).Default() | ||||||
|
||||||
return &Instance{ | ||||||
valid: true, | ||||||
raw: raw, | ||||||
name: i.name, | ||||||
sch: newsch, | ||||||
}, lac | ||||||
// Check that the result is concrete by trying to marshal/export it as JSON | ||||||
_, err = json.Marshal(raw) | ||||||
if err != nil { | ||||||
return nil, nil, errors.Mark(fmt.Errorf("lens produced a non-concrete result: %s", cerrors.Details(err, nil)), terrors.ErrLensIncomplete) | ||||||
} | ||||||
|
||||||
// Ensure the result is a valid instance of the target schema | ||||||
inst, err := newsch.Validate(raw) | ||||||
if err != nil { | ||||||
return nil, nil, errors.Mark(err, terrors.ErrLensResultIsInvalidData) | ||||||
} | ||||||
return inst, lac, err | ||||||
} | ||||||
|
||||||
type multiTranslationLacunas []struct { | ||||||
|
@@ -239,7 +253,3 @@ func (lac multiTranslationLacunas) AsList() []Lacuna { | |||||
} | ||||||
return l | ||||||
} | ||||||
|
||||||
// func TranslateComposed(lin ComposedLineage) { | ||||||
|
||||||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
When docs says:
[error] ... is a child of [anotherError]
, what does that actually mean?Does it mean it should be checkable with
errors.Is
? In such case, I cannot see where that "inheritance" (or better said: wrapping) is happening, but I might be missing something here.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.
Yeah, that's the intention. You're right though, "child" doesn't mean a clear thing here, and it should be phrased in a way where that's clear.