This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from invopop/replicate
Adding support for new Replicate command & GOBL 0.75
- Loading branch information
Showing
10 changed files
with
518 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/invopop/gobl.cli/internal" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type replicateOpts struct { | ||
*rootOpts | ||
} | ||
|
||
func replicate(root *rootOpts) *replicateOpts { | ||
return &replicateOpts{ | ||
rootOpts: root, | ||
} | ||
} | ||
|
||
func (o *replicateOpts) cmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Args: cobra.MaximumNArgs(2), | ||
RunE: o.runE, | ||
Use: "replicate [infile] [outfile]", | ||
Short: "Replicate a document from the provided input", | ||
} | ||
return cmd | ||
} | ||
|
||
func (o *replicateOpts) runE(cmd *cobra.Command, args []string) error { | ||
ctx := commandContext(cmd) | ||
|
||
input, err := openInput(cmd, args) | ||
if err != nil { | ||
return err | ||
} | ||
defer input.Close() // nolint:errcheck | ||
|
||
out, err := o.openOutput(cmd, args) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() // nolint:errcheck | ||
|
||
rOpts := &internal.ReplicateOptions{ | ||
ParseOptions: &internal.ParseOptions{ | ||
Input: input, | ||
}, | ||
} | ||
|
||
obj, err := internal.Replicate(ctx, rOpts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
enc := json.NewEncoder(out) | ||
if o.indent { | ||
enc.SetIndent("", "\t") | ||
} | ||
|
||
return enc.Encode(obj) | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/invopop/gobl" | ||
"github.com/invopop/gobl/schema" | ||
) | ||
|
||
// ReplicateOptions define all the basic options required to build a replicated | ||
// document from the input. | ||
type ReplicateOptions struct { | ||
*ParseOptions | ||
} | ||
|
||
// Replicate takes a base document as input and builds a replicated document | ||
// for the output. | ||
func Replicate(ctx context.Context, opts *ReplicateOptions) (interface{}, error) { | ||
res, err := replicate(ctx, opts) | ||
if err != nil { | ||
return nil, wrapError(http.StatusUnprocessableEntity, err) | ||
} | ||
return res, nil | ||
} | ||
|
||
func replicate(ctx context.Context, opts *ReplicateOptions) (interface{}, error) { | ||
obj, err := parseGOBLData(ctx, opts.ParseOptions) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if env, ok := obj.(*gobl.Envelope); ok { | ||
e2, err := env.Replicate() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err = e2.Validate(); err != nil { | ||
return nil, err | ||
} | ||
return e2, nil | ||
} | ||
|
||
if doc, ok := obj.(*schema.Object); ok { | ||
// Documents are updated in place | ||
if err := doc.Replicate(); err != nil { | ||
return nil, err | ||
} | ||
if err = doc.Validate(); err != nil { | ||
return nil, err | ||
} | ||
return doc, nil | ||
} | ||
|
||
panic("input must be either an envelope or a document") | ||
} |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"gitlab.com/flimzy/testy" | ||
) | ||
|
||
// These tests depend on the build_test.go for some of the basics. | ||
|
||
func TestReplicate(t *testing.T) { | ||
type tt struct { | ||
opts *ReplicateOptions | ||
err string | ||
} | ||
|
||
tests := testy.NewTable() | ||
|
||
tests.Add("success", func(t *testing.T) interface{} { | ||
return tt{ | ||
opts: &ReplicateOptions{ | ||
ParseOptions: &ParseOptions{ | ||
Input: testFileReader(t, "testdata/success.json"), | ||
}, | ||
}, | ||
} | ||
}) | ||
|
||
tests.Add("success just invoice", func(t *testing.T) interface{} { | ||
return tt{ | ||
opts: &ReplicateOptions{ | ||
ParseOptions: &ParseOptions{ | ||
Input: testFileReader(t, "testdata/invoice.json"), | ||
}, | ||
}, | ||
} | ||
}) | ||
|
||
tests.Run(t, func(t *testing.T, tt tt) { | ||
t.Parallel() | ||
opts := tt.opts | ||
got, err := Replicate(context.Background(), opts) | ||
if tt.err == "" { | ||
assert.Nil(t, err) | ||
} else { | ||
if assert.Error(t, err) { | ||
assert.Contains(t, err.Error(), tt.err) | ||
} | ||
} | ||
if err != nil { | ||
return | ||
} | ||
replacements := []testy.Replacement{ | ||
{ | ||
Regexp: regexp.MustCompile(`(?s)"sigs": \[.*\]`), | ||
Replacement: `"sigs": ["signature data"]`, | ||
}, | ||
{ | ||
Regexp: regexp.MustCompile(`"uuid":.?"[^\"]+"`), | ||
Replacement: `"uuid":"00000000-0000-0000-0000-000000000000"`, | ||
}, | ||
{ | ||
Regexp: regexp.MustCompile(`"val":.?"[\w\d]{64}"`), | ||
Replacement: `"val":"74ffc799663823235951b43a1324c70555c0ba7e3b545c1f50af34bbcc57033b"`, | ||
}, | ||
} | ||
if d := testy.DiffAsJSON(testy.Snapshot(t), got, replacements...); d != nil { | ||
t.Error(d) | ||
} | ||
}) | ||
} |
Oops, something went wrong.