Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions omniparser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/jf-tech/omniparser/omniparser/customfuncs"
"github.com/jf-tech/omniparser/omniparser/errs"
"github.com/jf-tech/omniparser/omniparser/schemaplugin"
omniv2 "github.com/jf-tech/omniparser/omniparser/schemaplugin/omni/v2"
"github.com/jf-tech/omniparser/omniparser/transformctx"
)

Expand All @@ -36,14 +37,13 @@ type Extension struct {
// CustomFuncs contains a collection of custom funcs provided by this extension. Optional.
CustomFuncs customfuncs.CustomFuncs
// ParseSchema is a constructor function that matches and creates a schema plugin. Optional.
ParseSchema schemaplugin.SchemaParserFunc
ParseSchema schemaplugin.ParseSchemaFunc
}

// BuiltinExtensions contains all the built-in extensions (custom funcs, and schema plugins)
var BuiltinExtensions = []Extension{
{
CustomFuncs: customfuncs.BuiltinCustomFuncs,
},
{CustomFuncs: customfuncs.BuiltinCustomFuncs},
{ParseSchema: omniv2.ParseSchema},
}

type parser struct {
Expand Down Expand Up @@ -75,7 +75,12 @@ func NewParser(schemaName string, schemaReader io.Reader, exts ...Extension) (Pa
if ext.ParseSchema == nil {
continue
}
plugin, err := ext.ParseSchema(schemaName, schemaHeader, schemaContent)
plugin, err := ext.ParseSchema(&schemaplugin.ParseSchemaCtx{
Name: schemaName,
Header: schemaHeader,
Content: schemaContent,
CustomFuncs: collectCustomFuncs(append([]Extension{ext}, BuiltinExtensions...)), // keep builtin exts last.
})
if err == errs.ErrSchemaNotSupported {
continue
}
Expand All @@ -94,6 +99,22 @@ func NewParser(schemaName string, schemaReader io.Reader, exts ...Extension) (Pa
return nil, errs.ErrSchemaNotSupported
}

func collectCustomFuncs(exts []Extension) customfuncs.CustomFuncs {
funcs := make(customfuncs.CustomFuncs)
for _, ext := range exts {
if ext.CustomFuncs == nil {
continue
}
for name, f := range ext.CustomFuncs {
// This does mean any 3rd party extension custom funcs name-collide with
// builtin custom funcs, they will be overwritten by builtin ones (because
// argument exts always put builtin exts at last), which makes sense. :)
funcs[name] = f
}
}
return funcs
}

// GetTransformOp creates and returns an instance of TransformOp for a given input.
func (p *parser) GetTransformOp(name string, input io.Reader, ctx *transformctx.Ctx) (TransformOp, error) {
br, err := iohelper.StripBOM(p.schemaHeader.ParserSettings.WrapEncoding(input))
Expand Down
6 changes: 3 additions & 3 deletions omniparser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestNewParser(t *testing.T) {
exts: []Extension{
{}, // Empty extension to test if we skip empty extension properly or not.
{
ParseSchema: func(string, schemaplugin.Header, []byte) (schemaplugin.Plugin, error) {
ParseSchema: func(_ *schemaplugin.ParseSchemaCtx) (schemaplugin.Plugin, error) {
return nil, errs.ErrSchemaNotSupported
},
},
Expand All @@ -51,7 +51,7 @@ func TestNewParser(t *testing.T) {
schema: `{"parser_settings": {"version": "9999", "file_format_type": "exe" }}`,
exts: []Extension{
{
ParseSchema: func(string, schemaplugin.Header, []byte) (schemaplugin.Plugin, error) {
ParseSchema: func(_ *schemaplugin.ParseSchemaCtx) (schemaplugin.Plugin, error) {
return nil, errors.New("invalid schema")
},
},
Expand All @@ -63,7 +63,7 @@ func TestNewParser(t *testing.T) {
schema: `{"parser_settings": {"version": "9999", "file_format_type": "exe" }}`,
exts: []Extension{
{
ParseSchema: func(string, schemaplugin.Header, []byte) (schemaplugin.Plugin, error) {
ParseSchema: func(_ *schemaplugin.ParseSchemaCtx) (schemaplugin.Plugin, error) {
return nil, nil
},
},
Expand Down
21 changes: 21 additions & 0 deletions omniparser/schemaplugin/omni/v2/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package omniv2

import (
"github.com/jf-tech/omniparser/omniparser/errs"
"github.com/jf-tech/omniparser/omniparser/schemaplugin"
"github.com/jf-tech/omniparser/omniparser/schemaplugin/omni/v2/transform"
)

const (
pluginVersion = "omni.2.0"
fileFormatXML = "xml"
)

func ParseSchema(_ *schemaplugin.ParseSchemaCtx) (schemaplugin.Plugin, error) {
return nil, errs.ErrSchemaNotSupported
}

type omniSchema struct {
schemaplugin.Header
Decls map[string]*transform.Decl `json:"transform_declarations"`
}
15 changes: 15 additions & 0 deletions omniparser/schemaplugin/omni/v2/plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package omniv2

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/jf-tech/omniparser/omniparser/errs"
)

func TestParseSchema(t *testing.T) {
p, err := ParseSchema(nil)
assert.Equal(t, errs.ErrSchemaNotSupported, err)
assert.Nil(t, p)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
[
{
"const": "123",
"result_type": "int",
"fqdn": "(nil)",
"kind": "(nil)",
"parent": "(nil)"
},
{
"external": "abc",
"result_type": "string",
"keep_leading_trailing_space": true,
"keep_empty_or_null": true,
"fqdn": "(nil)",
"kind": "(nil)",
"parent": "(nil)"
},
{
"xpath": "xyz",
"keep_leading_trailing_space": true,
"keep_empty_or_null": true,
"fqdn": "(nil)",
"kind": "(nil)",
"parent": "(nil)"
},
{
"xpath_dynamic": {
"const": "true",
"result_type": "boolean",
"fqdn": "(nil)",
"kind": "(nil)",
"parent": "(nil)"
},
"template": "t",
"fqdn": "(nil)",
"kind": "(nil)",
"parent": "(nil)"
},
{
"xpath": "abc",
"object": {
"field1": {
"external": "123",
"fqdn": "(nil)",
"kind": "(nil)",
"parent": "(nil)"
}
},
"fqdn": "(nil)",
"kind": "(nil)",
"parent": "(nil)"
},
{
"array": [
{
"const": "123",
"result_type": "float",
"fqdn": "(nil)",
"kind": "(nil)",
"parent": "(nil)"
}
],
"fqdn": "(nil)",
"kind": "(nil)",
"parent": "(nil)"
},
{
"custom_func": {
"name": "upper",
"args": [
{
"const": " abc ",
"keep_leading_trailing_space": true,
"fqdn": "(nil)",
"kind": "(nil)",
"parent": "(nil)"
}
]
},
"fqdn": "(nil)",
"kind": "(nil)",
"parent": "(nil)"
}
]

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"xpath": "abc",
"object": {
"field1": {
"external": "123",
"fqdn": "root.field1",
"kind": "external",
"parent": "root"
}
},
"fqdn": "root",
"kind": "object",
"children": [
"root.field1"
],
"parent": "(nil)"
}

Loading