Skip to content

Commit ddd52b9

Browse files
authored
Deprecate custom_parse in favor of custom_func (#135)
Deprecated but still functional. Simply removed mention of `custom_parse` from doc and marked it `Deprecated` in public APIs.
1 parent 4c56223 commit ddd52b9

19 files changed

+66
-88
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Examples:
3838
- [XML Examples](extensions/omniv21/samples/xml).
3939
- [EDI Examples](extensions/omniv21/samples/edi).
4040
- [Custom File Format](extensions/omniv21/samples/customfileformats/jsonlog)
41-
- [Custom Parse](extensions/omniv21/samples/customparse)
41+
- [Custom Funcs](extensions/omniv21/samples/customfuncs)
4242

4343
In the example folders above you will find pairs of input files and their schema files. Then in the
4444
`.snapshots` sub directory, you'll find their corresponding output files.
@@ -64,6 +64,8 @@ situations.
6464
- Golang 1.14
6565

6666
## Recent Major Feature Additions/Changes
67+
- Deprecated `custom_parse` in favor of `custom_func` (`custom_parse` is still usable for
68+
back-compatibility, it is just removed from all public docs and samples).
6769
- Added `NonValidatingReader` EDI segment reader.
6870
- Added fixed-length file format support in omniv21 handler.
6971
- Added EDI file format support in omniv21 handler.

doc/programmability.md

-26
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
* [Programmability of Omniparser](#programmability-of-omniparser)
22
* [Out\-of\-Box Basic Use Case](#out-of-box-basic-use-case)
33
* [Add A New custom\_func](#add-a-new-custom_func)
4-
* [Add A New custom\_parse](#add-a-new-custom_parse)
54
* [Add A New File Format](#add-a-new-file-format)
65
* [Add A New Schema Handler](#add-a-new-schema-handler)
76
* [Put All Together](#put-all-together)
@@ -77,31 +76,6 @@ Each `custom_func` must be a Golang function with the first param being `*transf
7776
params can be of any type, as long as they will match the types of data that are fed into the function
7877
in `transform_declarations`.
7978

80-
## Add A New `custom_parse`
81-
82-
There are several ways to customize transform logic, one of which is using the all mighty `custom_func`
83-
`javascript` (or its silibing `javascript_with_context`), see details
84-
[here](./use_of_custom_funcs.md#javascript-and-javascript_with_context).
85-
86-
However currently we don't support multi-line javascript (yet), which makes writing complex transform
87-
logic in a single line javascript difficult to read and debug. Also there are situations where schema
88-
writers want the following:
89-
- native Golang code transform logic
90-
- logging/stats
91-
- better/thorough test coverage
92-
- more complexed operations like RPCs calls, encryption, etc, which isn't really suited/possible for
93-
javascript to handle.
94-
95-
`custom_parse` provides an in-code transform plugin mechanism. In addition to a number of built-in
96-
transforms, such as field, `const`, `external`, `object`, `template`, `array`, and `custom_func`,
97-
`custom_parse` allows schema writer to be able to provide a Golang function that takes in the
98-
`*idr.Node` at the current IDR cursor (see more about IDR cursoring
99-
[here](./xpath.md#data-context-and-anchoring)), does whatever processing and transforms as it sees
100-
fit, and returns whatever the desired result to be embedded in place of the `custom_parse`.
101-
102-
[This sample](../extensions/omniv21/samples/customparse/sample_test.go) gives a very detailed demo
103-
of how `custom_parse` works.
104-
10579
## Add A New File Format
10680

10781
While built-in `omni.2.1` schema handler already supports most popular file formats in a typical

doc/transforms.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,13 @@ examples:
134134
- Custom Function Call (**custom_func**): e.g. `{ "custom_func": {...} }`. See more details about
135135
`custom_func` transform directive [here](./use_of_custom_funcs.md).
136136

137-
- Custom Parse (**custom_parse**): e.g. `{ "custom_parse": "<custom parse id>" }`. See more details about
138-
`custom_parse` transform directive [here](./programmability.md).
139-
140137
## Miscellaneous
141138

142139
Several attributes can be specified on some or all transform directives:
143140

144141
1. `xpath` (or `xpath_dynamic`) can be used for data extraction or IDR cursor anchoring with the following
145142
transform types: field (in fact field has nothing else but an `xpath` or `xpath_dynamic`), `object`,
146-
`template`, `custom_func` and `custom_parse`. See more details about use of `xpath` (or `xpath_dynamic`)
143+
`template`, and `custom_func`. See more details about use of `xpath` (or `xpath_dynamic`)
147144
[here](./xpath.md).
148145

149146
2. `type` tells omniparser the result from the transform needs a type cast. Supported type cast types are:

doc/xpath.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ The most common use of `xpath` is for data extraction. Consider again the sample
8181
```
8282
8383
The `xpath` attributes on `"order_id"`, `"customer_id"`, and `"country"` tell omniparser where to get
84-
the field string data from. When `xpath` **not** appearing with `object`, `template`, `custom_func`, or
85-
`custom_parse`, then it is a data extraction directive telling omniparser to extract the text data at the
86-
location specified by the `xpath` query. Note in this situation, omniparser will require the result set of
87-
such `xpath` queries to be of a single node: if such `xpath` query results in more than one node, omniparser
84+
the field string data from. When `xpath` **not** appearing with `object`, `template`, or `custom_func`,
85+
then it is a data extraction directive telling omniparser to extract the text data at the location
86+
specified by the `xpath` query. Note in this situation, omniparser will require the result set of such
87+
`xpath` queries to be of a single node: if such `xpath` query results in more than one node, omniparser
8888
will fail the current record transform (but will continue onto the next one as this isn't considered fatal).
8989
9090
## Data Context and Anchoring
@@ -191,8 +191,7 @@ Now let's go through the schema and input together to see how `xpath` anchoring
191191
for a `custom_func`, then everything inside the `custom_func`, namely those argument transforms, are all
192192
anchored on the cursor position prescribed by the `xpath`.
193193
194-
When `xpath` is used for anchoring and cursoring, it can appear with `object`, `template`, `custom_func`, and
195-
`custom_parse`.
194+
When `xpath` is used for anchoring and cursoring, it can appear with `object`, `template`, and `custom_func`.
196195
197196
## Static and Dynamic XPath Queries
198197
@@ -280,8 +279,8 @@ for `color` field in the output, we need to dynamically construct an XPath query
280279
needed for `size` field data extraction.
281280
282281
`xpath_dynamic` is used in such a situation. It basically says, unlike `xpath` is always a constant and static
283-
string value, `xpath_dynamic` is computed, by either `custom_func`, or `custom_parse`, or `template`, or
284-
`external`, or `const`, or another `xpath` direct data extraction.
282+
string value, `xpath_dynamic` is computed, by either `custom_func`, or `template`, or `external`, or
283+
`const`, or another `xpath` direct data extraction.
285284
286285
`xpath_dynamic` can be used everywhere `xpath` is used, except on `FINAL_OUTPUT`. `FINAL_OUTPUT` can only
287286
use `xpath`.
@@ -304,7 +303,7 @@ Depending on which transform is in play, different outcomes, including error, ca
304303
- Example: `"FINAL_OUTPUT": { "xpath": "/publishers/*", "object": {`
305304
- The result set can be either empty, or of one node, or of multiple nodes.
306305
307-
- `xpath`/`xpath_dynamic` used in `object`, `custom_func`, `custom_parse`, `template` transform
306+
- `xpath`/`xpath_dynamic` used in `object`, `custom_func`, `template` transform
308307
(other than `FINAL_OUTPUT` or directly under an `array` transform):
309308
310309
- Example: `"contact": { "xpath": "PATH/TO/CONTACT", "object": {`

extensions/omniv21/ingester.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
type ingester struct {
1515
finalOutputDecl *transform.Decl
1616
customFuncs customfuncs.CustomFuncs
17-
customParseFuncs transform.CustomParseFuncs
17+
customParseFuncs transform.CustomParseFuncs // Deprecated.
1818
ctx *transformctx.Ctx
1919
reader fileformat.FormatReader
2020
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"parser_settings": {
3+
"version": "omni.2.1",
4+
"file_format_type": "xml"
5+
},
6+
"transform_declarations": {
7+
"FINAL_OUTPUT": { "xpath": "/Employees/Employee", "object": {
8+
"employee_id": { "xpath": "ID" },
9+
"personal_details": { "xpath": "ID", "custom_func": { "name": "employee_personal_details_lookup" }},
10+
"business_details": { "xpath": "ID", "custom_func": { "name": "employee_business_details_lookup" }},
11+
"team": { "xpath": "ID", "custom_func": { "name": "employee_team_lookup" }}
12+
}}
13+
}
14+
}

extensions/omniv21/samples/customparse/sample_test.go renamed to extensions/omniv21/samples/customfuncs/sample_test.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package customparse
1+
package customfuncs
22

33
import (
44
"io"
@@ -13,8 +13,9 @@ import (
1313
"github.com/stretchr/testify/assert"
1414

1515
"github.com/jf-tech/omniparser"
16+
"github.com/jf-tech/omniparser/customfuncs"
1617
"github.com/jf-tech/omniparser/extensions/omniv21"
17-
"github.com/jf-tech/omniparser/extensions/omniv21/transform"
18+
v21 "github.com/jf-tech/omniparser/extensions/omniv21/customfuncs"
1819
"github.com/jf-tech/omniparser/idr"
1920
"github.com/jf-tech/omniparser/transformctx"
2021
)
@@ -37,13 +38,14 @@ func TestSample(t *testing.T) {
3738
schemaFileReader,
3839
omniparser.Extension{
3940
CreateSchemaHandler: omniv21.CreateSchemaHandler,
40-
CreateSchemaHandlerParams: &omniv21.CreateParams{
41-
CustomParseFuncs: transform.CustomParseFuncs{
41+
CustomFuncs: customfuncs.Merge(
42+
customfuncs.CommonCustomFuncs,
43+
v21.OmniV21CustomFuncs,
44+
customfuncs.CustomFuncs{
4245
"employee_personal_details_lookup": employeePersonalDetailsLookup,
4346
"employee_business_details_lookup": employeeBusinessDetailsLookup,
4447
"employee_team_lookup": employeeTempLookup,
45-
},
46-
},
48+
}),
4749
})
4850
assert.NoError(t, err)
4951
transform, err := schema.NewTransform(inputFileBaseName, inputFileReader, &transformctx.Ctx{})
@@ -71,7 +73,7 @@ func TestSample(t *testing.T) {
7173
func employeePersonalDetailsLookup(_ *transformctx.Ctx, node *idr.Node) (interface{}, error) {
7274
id := node.InnerText()
7375
// Pretend some complex logic and/or RPC calls...
74-
// This custom_parse demonstrates how to return a complex object with map[string]interface{}
76+
// This custom_func demonstrates how to return a complex object with map[string]interface{}
7577
idInt, err := strconv.Atoi(id)
7678
if err != nil {
7779
return nil, err
@@ -91,7 +93,7 @@ func employeePersonalDetailsLookup(_ *transformctx.Ctx, node *idr.Node) (interfa
9193
func employeeBusinessDetailsLookup(_ *transformctx.Ctx, node *idr.Node) (interface{}, error) {
9294
id := node.InnerText()
9395
// Pretend some complex logic and/or RPC calls...
94-
// This custom_parse demonstrates how to return a complex object with golang struct
96+
// This custom_func demonstrates how to return a complex object with golang struct
9597
type employeeReview struct {
9698
Year int `json:"year"`
9799
Rating string `json:"rating"`

extensions/omniv21/samples/customparse/sample_schema.json

-14
This file was deleted.

extensions/omniv21/schemahandler.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const (
2525
// CreateParams allows user of this 'omni.2.1' schema handler to provide creation customization.
2626
type CreateParams struct {
2727
CustomFileFormats []fileformat.FileFormat
28-
CustomParseFuncs transform.CustomParseFuncs
28+
// Deprecated.
29+
CustomParseFuncs transform.CustomParseFuncs
2930
}
3031

3132
// CreateSchemaHandler parses, validates and creates an omni-schema based handler.

extensions/omniv21/transform/decl.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const (
1818
kindObject kind = "object"
1919
kindArray kind = "array"
2020
kindCustomFunc kind = "custom_func"
21-
kindCustomParse kind = "custom_parse"
21+
kindCustomParse kind = "custom_parse" // Deprecated
2222
kindTemplate kind = "template"
2323
)
2424

@@ -83,7 +83,7 @@ type Decl struct {
8383
XPathDynamic *Decl `json:"xpath_dynamic,omitempty"`
8484
// CustomFunc specifies the input element is a custom function.
8585
CustomFunc *CustomFuncDecl `json:"custom_func,omitempty"`
86-
// CustomParse specifies the input element is to be custom parsed.
86+
// CustomParse specifies the input element is to be custom parsed. Deprecated.
8787
CustomParse *string `json:"custom_parse,omitempty"`
8888
// Template specifies the input element is a template.
8989
Template *string `json:"template,omitempty"`

extensions/omniv21/transform/invokeCustomParse.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"github.com/jf-tech/omniparser/transformctx"
88
)
99

10-
// CustomParseFuncType represents a custom_parse function type.
10+
// CustomParseFuncType represents a custom_parse function type. Deprecated. Use customfuncs.CustomFuncType.
1111
type CustomParseFuncType func(*transformctx.Ctx, *idr.Node) (interface{}, error)
1212

13-
// CustomParseFuncs is a map from custom_parse names to an actual custom parse functions.
13+
// CustomParseFuncs is a map from custom_parse names to an actual custom parse functions. Deprecated. Use
14+
// customfuncs.CustomFuncs.
1415
type CustomParseFuncs = map[string]CustomParseFuncType
1516

1617
func (p *parseCtx) invokeCustomParse(customParseFn CustomParseFuncType, n *idr.Node) (interface{}, error) {

extensions/omniv21/transform/parse.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
type parseCtx struct {
1616
transformCtx *transformctx.Ctx
1717
customFuncs customfuncs.CustomFuncs
18-
customParseFuncs CustomParseFuncs
19-
disableTransformCache bool // by default we have caching on. only in some tests we turn caching off.
18+
customParseFuncs CustomParseFuncs // Deprecated.
19+
disableTransformCache bool // by default we have caching on. only in some tests we turn caching off.
2020
transformCache map[string]interface{}
2121
}
2222

extensions/omniv21/transform/validate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
type validateCtx struct {
1717
Decls map[string]*Decl `json:"transform_declarations"`
1818
customFuncs customfuncs.CustomFuncs
19-
customParseFuncs CustomParseFuncs
19+
customParseFuncs CustomParseFuncs // Deprecated.
2020
declHashes map[string]string
2121
}
2222

extensions/omniv21/validation/transformDeclarations.go

+9-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)