-
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
Changes from 4 commits
2f5defc
ce1c611
bff274a
8070bba
a0293d6
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 | ||||
---|---|---|---|---|---|---|
|
@@ -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) { | ||||||
|
||||||
// } |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,11 @@ func NewTypedMux[T thema.Assignee](sch thema.TypedSchema[T], dec Decoder) TypedM | |
} | ||
|
||
if inst, ierr := isch.Validate(v); ierr == nil { | ||
trinst, lac := inst.Translate(sch.Version()) | ||
trinst, lac, err := inst.Translate(sch.Version()) | ||
if err != 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. Shouldn't we do something here? |
||
} | ||
// TODO perf: introduce a typed translator to avoid wastefully re-binding the go type every time | ||
tinst, err := thema.BindInstanceType(trinst, sch) | ||
if err != nil { | ||
panic(fmt.Errorf("unreachable, instance type should always be bindable: %w", err)) | ||
|
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.