Skip to content

Commit

Permalink
thema: One Translate(), with error return
Browse files Browse the repository at this point in the history
  • Loading branch information
sam boyer committed Jul 12, 2023
1 parent bff274a commit 8070bba
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 28 deletions.
10 changes: 7 additions & 3 deletions cmd/thema/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"path/filepath"

"cuelang.org/go/cue"
"github.com/spf13/cobra"

"github.com/grafana/thema"
"github.com/grafana/thema/vmux"
"github.com/spf13/cobra"
)

type dataCommand struct {
Expand Down Expand Up @@ -180,8 +181,11 @@ func (dc *dataCommand) runTranslate(cmd *cobra.Command, args []string) error {
}

// Prior validations checked that the schema version exists in the lineage
tinst, lac := inst.Translate(dc.lla.dl.sch.Version())
if err := dc.validateTranslationResult(tinst, lac); err != nil {
tinst, lac, err := inst.Translate(dc.lla.dl.sch.Version())
if err != nil {
return err
}
if err = dc.validateTranslationResult(tinst, lac); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var (
)

// Translation errors. These all occur as a result of an invalid lens. Currently
// these may be returned from [thema.Instance.TranslateErr]. Eventually, it is
// these may be returned from [thema.Instance.Translate]. Eventually, it is
// hoped that they will be caught statically in [thema.BindLineage] and cannot
// occur at runtime.
var (
Expand Down
24 changes: 5 additions & 19 deletions instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
cerrors "cuelang.org/go/cue/errors"
"cuelang.org/go/pkg/encoding/json"
"github.com/cockroachdb/errors"

terrors "github.com/grafana/thema/errors"
)

Expand Down Expand Up @@ -97,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())
}
Expand Down Expand Up @@ -170,22 +171,7 @@ func (inst *TypedInstance[T]) ValueP() T {
return t
}

// Translate is [Instance.TranslateErr], but panics if an error is encountered.
//
// Errors can only occur in Translate if lenses are not written correctly.
// Eventually, it is hoped that all possible error cases on lenses will be
// determinable statically, and turned into lineage validity errors that are
// caught by [BindLineage]. If that goal is reached, this function will never
// panic, and [Instance.TranslateErr] will be deprecated.
func (i *Instance) Translate(to SyntacticVersion) (*Instance, TranslationLacunas) {
inst, lac, err := i.TranslateErr(to)
if err != nil {
panic(err)
}
return inst, lac
}

// TranslateErr transforms the provided [Instance] to an Instance of a different
// Translate transforms the provided [Instance] to an Instance of a different
// [Schema] from the same [Lineage]. A new *Instance is returned representing the
// transformed value, along with any lacunas accumulated along the way.
//
Expand All @@ -209,7 +195,7 @@ 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].
func (i *Instance) TranslateErr(to SyntacticVersion) (*Instance, TranslationLacunas, error) {
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.
Expand Down
6 changes: 4 additions & 2 deletions instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func TestInstance_Translate(t *testing.T) {
for from := lin.First(); from != nil; from = from.Successor() {
for _, example := range from.Examples() {
for to := lin.First(); to != nil; to = to.Successor() {
tinst, lacunas := example.Translate(to.Version())
tinst, lacunas, err := example.Translate(to.Version())
require.NoError(t, err)
require.NotNil(t, tinst)

result := tinst.Underlying()
Expand Down Expand Up @@ -127,7 +128,8 @@ schemas: [
require.Equal(t, expected, got)

// Translate cue.Value (no lacunas)
tinst, _ := inst.Translate(SV(0, 1))
tinst, _, err := inst.Translate(SV(0, 1))
require.NoError(t, err)
require.Equal(t, SV(0, 0), inst.Schema().Version())

got, err = tinst.Underlying().LookupPath(cue.ParsePath("title")).String()
Expand Down
6 changes: 5 additions & 1 deletion vmux/typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

}
// 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))
Expand Down
3 changes: 1 addition & 2 deletions vmux/untyped.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ func NewUntypedMux(sch thema.Schema, dec Decoder) UntypedMux {
}

if inst, ierr := isch.Validate(v); ierr == nil {
trinst, lac := inst.Translate(sch.Version())
return trinst, lac, nil
return inst.Translate(sch.Version())
}
}

Expand Down

0 comments on commit 8070bba

Please sign in to comment.