Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 00fe76c

Browse files
authored
Finish implementation of Parser and move package nodes to under omniparser (#18)
Finish implementation of Parser and move package `nodes` to under `omniparser`
1 parent 848319f commit 00fe76c

File tree

13 files changed

+137
-35
lines changed

13 files changed

+137
-35
lines changed

‎go.mod‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/antchfx/xpath v1.1.10
88
github.com/bradleyjkemp/cupaloy v2.3.0+incompatible
99
github.com/hashicorp/golang-lru v0.5.4
10+
github.com/jf-tech/iohelper v1.0.3
1011
github.com/stretchr/testify v1.6.1
1112
golang.org/x/text v0.3.0
1213
)

‎go.sum‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18h
1010
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
1111
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
1212
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
13+
github.com/jf-tech/iohelper v1.0.3 h1:304dQL6ZKJEmDkbIivqCGrhmJTi7k7+1e2CC+WiERS4=
14+
github.com/jf-tech/iohelper v1.0.3/go.mod h1:X28R+KF0lnKEhZ8Q0iBzLI9FKHJy/jXZ+axaM7HMOXA=
1315
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1416
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1517
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
18+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
1619
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
1720
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1821
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -27,5 +30,6 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
2730
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
2831
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2932
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
33+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
3034
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
3135
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎omniparser/parser.go‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"io"
77
"io/ioutil"
88

9+
"github.com/jf-tech/iohelper"
10+
911
"github.com/jf-tech/omniparser/omniparser/customfuncs"
1012
"github.com/jf-tech/omniparser/omniparser/errs"
1113
"github.com/jf-tech/omniparser/omniparser/schemaplugin"
@@ -31,10 +33,13 @@ type Parser interface {
3133

3234
// Extension allows client of omniparser to supply its own custom funcs and/or schema plugin.
3335
type Extension struct {
36+
// CustomFuncs contains a collection of custom funcs provided by this extension. Optional.
3437
CustomFuncs customfuncs.CustomFuncs
38+
// ParseSchema is a constructor function that matches and creates a schema plugin. Optional.
3539
ParseSchema schemaplugin.SchemaParserFunc
3640
}
3741

42+
// BuiltinExtensions contains all the built-in extensions (custom funcs, and schema plugins)
3843
var BuiltinExtensions = []Extension{
3944
{
4045
CustomFuncs: customfuncs.BuiltinCustomFuncs,
@@ -91,7 +96,24 @@ func NewParser(schemaName string, schemaReader io.Reader, exts ...Extension) (Pa
9196

9297
// GetTransformOp creates and returns an instance of TransformOp for a given input.
9398
func (p *parser) GetTransformOp(name string, input io.Reader, ctx *transformctx.Ctx) (TransformOp, error) {
94-
panic("TBD")
99+
br, err := iohelper.StripBOM(p.schemaHeader.ParserSettings.WrapEncoding(input))
100+
if err != nil {
101+
return nil, err
102+
}
103+
inputProcessor, err := p.schemaPlugin.GetInputProcessor(ctx, br)
104+
if err != nil {
105+
return nil, err
106+
}
107+
if ctx.InputName != name {
108+
ctx.InputName = name
109+
}
110+
// If caller already specified a way to do context aware error formatting, use it;
111+
// otherwise (vast majority cases), use the InputProcessor (which implements CtxAwareErr
112+
// interface) created by the schema plugin.
113+
if ctx.CtxAwareErr == nil {
114+
ctx.CtxAwareErr = inputProcessor
115+
}
116+
return &transformOp{inputProcessor: inputProcessor}, nil
95117
}
96118

97119
// SchemaHeader returns the associated schema plugin's schema header.

‎omniparser/parser_test.go‎

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/jf-tech/omniparser/omniparser/errs"
1212
"github.com/jf-tech/omniparser/omniparser/schemaplugin"
13+
"github.com/jf-tech/omniparser/omniparser/transformctx"
1314
"github.com/jf-tech/omniparser/testlib"
1415
)
1516

@@ -91,17 +92,58 @@ func TestNewParser(t *testing.T) {
9192
}
9293
}
9394

94-
func TestParser(t *testing.T) {
95+
func TestParser_GetTransformOp_StripBOMFailure(t *testing.T) {
96+
p := &parser{
97+
schemaHeader: schemaplugin.Header{
98+
ParserSettings: schemaplugin.ParserSettings{Version: "999", FileFormatType: "exe"},
99+
},
100+
}
101+
op, err := p.GetTransformOp("test input", testlib.NewMockReadCloser("bom read failure", nil), nil)
102+
assert.Error(t, err)
103+
assert.Equal(t, "bom read failure", err.Error())
104+
assert.Nil(t, op)
105+
}
106+
107+
type testSchemaPlugin struct {
108+
getInputProcessorErr error
109+
}
110+
111+
func (t testSchemaPlugin) GetInputProcessor(_ *transformctx.Ctx, _ io.Reader) (schemaplugin.InputProcessor, error) {
112+
if t.getInputProcessorErr != nil {
113+
return nil, t.getInputProcessorErr
114+
}
115+
return &testInputProcessor{}, nil
116+
}
117+
118+
func TestParser_GetTransformOp_GetInputProcessorFailure(t *testing.T) {
119+
p := &parser{
120+
schemaHeader: schemaplugin.Header{
121+
ParserSettings: schemaplugin.ParserSettings{Version: "999", FileFormatType: "exe"},
122+
},
123+
schemaPlugin: testSchemaPlugin{getInputProcessorErr: errors.New("test failure")},
124+
}
125+
op, err := p.GetTransformOp("test input", strings.NewReader("something"), nil)
126+
assert.Error(t, err)
127+
assert.Equal(t, "test failure", err.Error())
128+
assert.Nil(t, op)
129+
}
130+
131+
func TestParser_GetTransformOp_NameAndCtxAwareErrOverwrite(t *testing.T) {
95132
header := schemaplugin.Header{
96133
ParserSettings: schemaplugin.ParserSettings{Version: "999", FileFormatType: "exe"},
97134
}
98135
p := &parser{
99136
schemaHeader: header,
100137
schemaContent: []byte("test schema content"),
138+
schemaPlugin: testSchemaPlugin{},
101139
}
102-
assert.Panics(t, func() {
103-
_, _ = p.GetTransformOp("name", nil, nil)
104-
})
140+
ctx := &transformctx.Ctx{}
141+
op, err := p.GetTransformOp("test input", strings.NewReader("something"), ctx)
142+
assert.NoError(t, err)
143+
assert.NotNil(t, op)
144+
assert.Equal(t, "test input", ctx.InputName)
145+
assert.NotNil(t, ctx.CtxAwareErr)
146+
105147
assert.Equal(t, header, p.SchemaHeader())
106-
assert.Equal(t, []byte("test schema content"), p.SchemaContent())
148+
assert.Equal(t, "test schema content", string(p.SchemaContent()))
107149
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /