From 8da7ae56bed87e6e0d90f1d3198c973fb8c9d771 Mon Sep 17 00:00:00 2001 From: aereal Date: Tue, 22 Mar 2022 14:01:00 +0900 Subject: [PATCH 1/2] feat: publish Input --- diff.go | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/diff.go b/diff.go index 612ed7b..6ce1ba2 100644 --- a/diff.go +++ b/diff.go @@ -46,11 +46,11 @@ func Only(query *gojq.Query) Option { // DiffFromFiles calculates differences with flies' contents. func DiffFromFiles(from, to fs.File, opts ...Option) (string, error) { - l, err := newInputFromFile(from) + l, err := NewInputFromFile(from) if err != nil { return "", fmt.Errorf("left: %w", err) } - r, err := newInputFromFile(to) + r, err := NewInputFromFile(to) if err != nil { return "", fmt.Errorf("right: %w", err) } @@ -59,7 +59,7 @@ func DiffFromFiles(from, to fs.File, opts ...Option) (string, error) { // DiffFromObjects calculates differences with from and to. func DiffFromObjects(from, to interface{}, opts ...Option) (string, error) { - return takeDiff(&input{x: from, name: "from"}, &input{x: to, name: "to"}, opts...) + return takeDiff(&Input{X: from, Name: "from"}, &Input{X: to, Name: "to"}, opts...) } // Diff calculates differences with from and to. @@ -69,25 +69,32 @@ func Diff(from, to interface{}, opts ...Option) (string, error) { return DiffFromObjects(from, to, opts...) } -func newInputFromFile(f fs.File) (*input, error) { - var i input +// NewInputFromFile returns a new Input from file's name and contents. +func NewInputFromFile(f fs.File) (*Input, error) { + var i Input st, err := f.Stat() if err != nil { return nil, err } - i.name = st.Name() - if err := json.NewDecoder(f).Decode(&i.x); err != nil { + i.Name = st.Name() + if err := json.NewDecoder(f).Decode(&i.X); err != nil { return nil, err } return &i, nil } -type input struct { - name string - x interface{} +// Input represents a pair of the object that decoded from JSON and its name. +type Input struct { + // Name is Input's name. + // + // It'll be used as patch's file name. + Name string + + // X is an object decoded from JSON. + X interface{} } -func takeDiff(from, to *input, opts ...Option) (string, error) { +func takeDiff(from, to *Input, opts ...Option) (string, error) { o := &opt{} for _, f := range opts { f(o) @@ -96,8 +103,8 @@ func takeDiff(from, to *input, opts ...Option) (string, error) { return "", err } var ( - fromObj = from.x - toObj = to.x + fromObj = from.X + toObj = to.X ) switch { case o.ignore != nil: @@ -131,7 +138,7 @@ func takeDiff(from, to *input, opts ...Option) (string, error) { return "", fmt.Errorf("toJSON(rhs): %v", err) } edits := myers.ComputeEdits(span.URIFromPath(""), l, r) - d := gotextdiff.ToUnified(from.name, to.name, l, edits) + d := gotextdiff.ToUnified(from.Name, to.Name, l, edits) return fmt.Sprint(d), nil } From 1696a260bda06b200ed4c1a243a2aee68354e60f Mon Sep 17 00:00:00 2001 From: aereal Date: Tue, 22 Mar 2022 14:02:42 +0900 Subject: [PATCH 2/2] feat!: change Diff()'s signature --- diff.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/diff.go b/diff.go index 6ce1ba2..327d9c5 100644 --- a/diff.go +++ b/diff.go @@ -54,19 +54,12 @@ func DiffFromFiles(from, to fs.File, opts ...Option) (string, error) { if err != nil { return "", fmt.Errorf("right: %w", err) } - return takeDiff(l, r, opts...) + return Diff(l, r, opts...) } // DiffFromObjects calculates differences with from and to. func DiffFromObjects(from, to interface{}, opts ...Option) (string, error) { - return takeDiff(&Input{X: from, Name: "from"}, &Input{X: to, Name: "to"}, opts...) -} - -// Diff calculates differences with from and to. -// -// Deprecated: use DiffObjects() -func Diff(from, to interface{}, opts ...Option) (string, error) { - return DiffFromObjects(from, to, opts...) + return Diff(&Input{X: from, Name: "from"}, &Input{X: to, Name: "to"}, opts...) } // NewInputFromFile returns a new Input from file's name and contents. @@ -94,7 +87,8 @@ type Input struct { X interface{} } -func takeDiff(from, to *Input, opts ...Option) (string, error) { +// Diff calculates differences with inputs. +func Diff(from, to *Input, opts ...Option) (string, error) { o := &opt{} for _, f := range opts { f(o)