Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalcek committed Jul 13, 2021
1 parent d581893 commit 81094cd
Show file tree
Hide file tree
Showing 18 changed files with 1,455 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dist
bafi
site
*.exe
*.exe~
*.csv
output.json


6 changes: 6 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"recommendations": [
"golang.go",
"casualjim.gotemplate"
]
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Mario
Copyright (c) 2021 mmalcek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
145 changes: 145 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
[![Go](https://github.com/mmalcek/bafi/actions/workflows/go.yml/badge.svg)](https://github.com/mmalcek/bafi/actions/workflows/go.yml)
[![CodeQL](https://github.com/mmalcek/bafi/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/mmalcek/bafi/actions/workflows/)
# XML (or other formats) to TEXT using GO templates
Transform XML to text file using GO "text/template"

## Key features
- Various input formats **(json, bson, yaml, xml)**
- Flexible output formatting using text templates
- Support for [LUA](https://www.lua.org/pil/contents.html) custom functions which allows very flexible data manipulation
- stdin/stdout support which allows get data from source -> translate -> delivery to destination. This allows easily translate data between different web services like REST to SOAP




## Command line arguments
- "-i input.xml" Input file name. If not defined app tries read stdin
- "-o output.txt" Output file name. If not defined result is send to stdout
- "-t template.tmpl" Template file. Alternatively you can use *inline* template
- inline template must start with **?** e.g. -t **"?{{.someValue}}"**
- "-f json" Alternative input format.
- Supported formats: **json, bson, yaml, xml (default)**
- "-v" - Show current verion
- "-?" - list available command line arguments

#### Examples
- Basic
```sh
bafi.exe -i testdata.xml -o output.csv -t template.tmpl
```
- JSON input
```sh
bafi.exe -i testdata.json -f json -o output.csv -t template.tmpl
```
- STDIN
```sh
curl.exe -s "someURL" | bafi.exe -f json -t myTemplate.tmpl -o out.txt
```
More info about curl [here](https://curl.se/) but you can of course use any tool with stdout

- Inline template
```sh
bafi.exe -i testdata.xml -o output.json -t "?{{toJSON .}}"
```
- Working example with inline template
```
curl.exe -s "https://api.predic8.de/shop/products/" | bafi.exe -f json -t "?{{range .products}}\"{{.name}}\",{{.product_url}}{{print \"\n\"}}{{end}}"
```

## Template formating
- Basic iterate over lines
```
{{range .TOP_LEVEL.DATA_LINE}}{{.val1}}{{end}}
```
- Get XML tag (-VALUE = tag name)
```
{{index .Linked_Text "-VALUE"}}
```
- Use functions (count val1 + val2)
```
{{add .val1 .val2}}
```
- If statement
```
{{if gt (int $val1) (int $val2)}}Value1{{else}}Value2{{end}} is greater
```
Check template.tmpl and testdata.xml for more advanced example

(more detailed info on https://golang.org/pkg/text/template/ )

## Lua custom functions
Aside of builtin functions you can write your own custom lua functions defined in ./lua/functions.lua file

- Input is always passed as json array of strings
- Output must be passed as string
- lua table array starts with 1
- Lua [documentation](http://www.lua.org/manual/5.1/)

Minimal functions.lua example
```lua
json = require './lua/json'

function sum(incomingData)
dataTable = json.decode(incomingData)
return tostring(tonumber(dataTable[1]) + tonumber(dataTable[2]))
end
```

### Call Lua function in template
```
{{lua "sum" .val1 .val2}}
```
- "sum" - Lua function name

### Call built-in function
```
{{add .val1 .val2}}
```

## Built-in functions
- add -> {{add .Value1 .Value2}}
- add1
- sub
- div
- mod
- mul
- randInt
- add1f - "...f" functions parse float but provide **decimal** operation using https://github.com/shopspring/decimal
- addf
- subf
- divf
- mulf
- round
- max
- min
- maxf
- minf
- dateFormat -> {{dateFormat .Value "oldFormat" "newFormat"}}
- now - {{now "02.01.2006"}} - GO format date (see notes below)
- b64enc
- b64dec
- b32enc
- b32dec
- uuid
- regexMatch
- upper
- lower
- trim
- trimAll
- trimSuffix
- trimPrefix
- atoi
- int64
- int
- float64
- toJSON - convert input object to JSON
- toBSON - convert input object to BSON
- toYAML - convert input object to YAML
- toXML - convert input object to XML
- mapJSON - convert stringified JSON to map so it can be used as object or translated to other formats (e.g. "toXML"). Check template.tmpl for example

### dateFormat
dateFormat can parse date and time using [GO time format](https://programming.guide/go/format-parse-string-time-date-example.html)

## Relase builds (download)
- Releases (windows, linux, mac) can be found [here](https://github.com/mmalcek/bafi/releases)
4 changes: 4 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Examples

## Basic usage
- TODO - Add examples
60 changes: 60 additions & 0 deletions docs/img/bafi.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 74 additions & 0 deletions docs/img/bafiIcon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/favicon.ico
Binary file not shown.
Loading

0 comments on commit 81094cd

Please sign in to comment.