|
| 1 | +# omniparser |
| 2 | + [](https://codecov.io/gh/jf-tech/omniparser) [](https://goreportcard.com/report/github.com/jf-tech/omniparser) |
| 3 | + |
| 4 | +A parser in naive Golang that ingests and transforms input data of various formats (CSV, txt, XML, EDI, JSON) |
| 5 | +into desired JSON output based on a schema spec written in JSON. |
| 6 | + |
| 7 | +Golang Version: 1.14.2 |
| 8 | + |
| 9 | +## Demo in Playground |
| 10 | + |
| 11 | +Use https://omniparser.herokuapp.com/ (might need to wait for a few seconds for heroku instance to wake up) |
| 12 | +for trying out schemas and inputs, yours and from sample library, to see how transform works. |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +Take a detailed look at samples here: |
| 17 | +- [json examples](./samples/omniv2/json) |
| 18 | +- [xml examples](./samples/omniv2/xml). |
| 19 | + |
| 20 | +## Simple Example (JSON -> JSON Transform) |
| 21 | +- Input: |
| 22 | + ``` |
| 23 | + { |
| 24 | + "order_id": "1234567", |
| 25 | + "tracking_number": "1z9999999999999999", |
| 26 | + "items": [ |
| 27 | + { |
| 28 | + "item_sku": "ab123", |
| 29 | + "item_price": 12.34, |
| 30 | + "number_purchased": 5 |
| 31 | + }, |
| 32 | + { |
| 33 | + "item_sku": "ck763-23", |
| 34 | + "item_price": 3.12, |
| 35 | + "number_purchased": 2 |
| 36 | + } |
| 37 | + ] |
| 38 | + } |
| 39 | + ``` |
| 40 | +- Schema: |
| 41 | + ``` |
| 42 | + { |
| 43 | + "parser_settings": { |
| 44 | + "version": "omni.2.0", |
| 45 | + "file_format_type": "json" |
| 46 | + }, |
| 47 | + "transform_declarations": { |
| 48 | + "FINAL_OUTPUT": { "xpath": ".", "object": { |
| 49 | + "order_id": { "xpath": "order_id" }, |
| 50 | + "tracking_number": { "custom_func": { |
| 51 | + "name": "upper", |
| 52 | + "args": [ { "xpath": "tracking_number" } ] |
| 53 | + }}, |
| 54 | + "items": { "array": [{ "xpath": "items/*", "object": { |
| 55 | + "sku": { "custom_func": { |
| 56 | + "name": "substring", |
| 57 | + "args": [ |
| 58 | + { "custom_func": { "name": "upper", "args": [ { "xpath": "item_sku" }]}}, |
| 59 | + { "const": "0", "_comment": "start index" }, |
| 60 | + { "const": "5", "_comment": "sub length" } |
| 61 | + ] |
| 62 | + }}, |
| 63 | + "total_price": { "custom_func": { |
| 64 | + "name": "javascript", |
| 65 | + "args": [ |
| 66 | + { "const": "num * price" }, |
| 67 | + { "const": "num:int" }, { "xpath": "number_purchased" }, |
| 68 | + { "const": "price:float" }, { "xpath": "item_price" } |
| 69 | + ] |
| 70 | + }} |
| 71 | + }}]} |
| 72 | + }} |
| 73 | + } |
| 74 | + } |
| 75 | + ``` |
| 76 | +- Code: |
| 77 | + ``` |
| 78 | + schema, err := omniparser.NewSchema("schema-name", strings.NewReader("...")) |
| 79 | + if err != nil { ... } |
| 80 | + transform, err := parser.NewTransform("input-name", strings.NewReader("..."), &transformctx.Ctx{}) |
| 81 | + if err != nil { ... } |
| 82 | + if !transform.Next() { ... } |
| 83 | + b, err := transform.Read() |
| 84 | + if err != nil { ... } |
| 85 | + fmt.Println(string(b)) |
| 86 | + ``` |
| 87 | +- Output: |
| 88 | + ``` |
| 89 | + { |
| 90 | + "order_id": "1234567", |
| 91 | + "tracking_number": "1Z9999999999999999", |
| 92 | + "items": [ |
| 93 | + { |
| 94 | + "sku": "AB123", |
| 95 | + "total_price": "61.7" |
| 96 | + }, |
| 97 | + { |
| 98 | + "sku": "CK763", |
| 99 | + "total_price": "6.24" |
| 100 | + } |
| 101 | + ] |
| 102 | + } |
| 103 | + ``` |
| 104 | +
|
| 105 | +## Why |
| 106 | +- No good ETL transform/parser library exists in Golang. |
| 107 | +- Even looking into Java and other languages, choices aren't many and all have limitations: |
| 108 | + - [Smooks](https://www.smooks.org/) is dead, plus its EDI parsing/transform is too heavyweight, needing code-gen. |
| 109 | + - [BeanIO](http://beanio.org/) can't deal with EDI input. |
| 110 | + - [Jolt](https://github.com/bazaarvoice/jolt) can't deal with anything other than JSON input. |
| 111 | + - [JSONata](https://jsonata.org/) still only JSON -> JSON transform. |
| 112 | +- Many of the parsers/transforms don't support streaming read, loading entire input into memory - not acceptable in some situations. |
| 113 | +
|
| 114 | +## Requirements |
| 115 | +- Golang 1.14 |
| 116 | +
|
| 117 | + This is only needed for `javascript` engine integration. Please raise an issue if you think 1.14 is too high, and |
| 118 | + you don't need `javascript` custom_func. Then we may consider moving `javascript` custom_func into a separate |
| 119 | + extension repo/package; the rest of the library is just golang 1.12. |
| 120 | +
|
| 121 | +## Recent Feature Additions |
| 122 | +- added trie based high performance `times.SmartParse`. |
| 123 | +- command line interface (one-off `transform` cmd or long-running http `server` mode). |
| 124 | +- javascript engine integration as a custom_func. |
| 125 | +- JSON stream parser. |
| 126 | +- Extensibility: |
| 127 | + - Ability to provide custom functions. |
| 128 | + - Ability to provide custom schema handler. |
| 129 | + - Ability to customize the built-in omniv2 schema handler's parsing code. |
| 130 | + - Ability to provide a new file format support to built-in omniv2 schema handler. |
0 commit comments