Skip to content

Commit

Permalink
Updated config naming
Browse files Browse the repository at this point in the history
  • Loading branch information
nydrani committed Jan 15, 2021
1 parent 0c6353b commit 4d47422
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
12 changes: 6 additions & 6 deletions docs/docs/std/encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ Usage:
|:-|:-|
| `//encoding.xml.decode('<?xml version="1.0"?><root></root>')` | `[(decl: (target: 'xml', text: 'version="1.0"')), (elem: (children: {}, name: 'root'))]` |

## `//encoding.xml.decoder(config <: (stripFormatting <: bool)) <: (\(csv <: string|bytes) <: array)`
## `//encoding.xml.decoder(config <: (:trimSurroundingWhitespace <: bool)) <: ((csv <: string|bytes) <: array)`

`decoder` takes a tuple used to configure decoding and returns the decoding function:
| config | description |
|:-|:-|
| `stripFormatting` | Strips newline strings `'\n'` used only for xml file formatting |
| `trimSurroundingWhitespace` | Strips newline strings `'\n'` used only for xml file formatting |

Usage:

| example | equals |
|:-|:-|
| `//encoding.xml.decoder((stripFormatting: true))('<?xml version="1.0"?>\n')` | `[(decl: (target: 'xml', text: 'version="1.0"'))` |
| `//encoding.xml.decoder((stripFormatting: false))('<?xml version="1.0"?>\n')` | `[(decl: (target: 'xml', text: 'version="1.0"')), (text: '\n')]` |
| `//encoding.xml.decoder((trimSurroundingWhitespace: true))('<?xml version="1.0"?>\n')` | `[(decl: (target: 'xml', text: 'version="1.0"'))` |
| `//encoding.xml.decoder((trimSurroundingWhitespace: false))('<?xml version="1.0"?>\n')` | `[(decl: (target: 'xml', text: 'version="1.0"')), (text: '\n')]` |

## `//encoding.xml.encode(xml <: array) <: bytes`

`encode` takes an array of tuples and converts it into a XML object.

For details of how Arr.ai encodes XML, see [Encoding](#Encoding) below.

For details of the limitations of XML encodeing, see [Limitations](#Limitations) below.
For details of the limitations of XML encoding, see [Limitations](#Limitations) below.

Usage:

Expand All @@ -51,7 +51,7 @@ Usage:
|:-|:-|
| `//encoding.csv.decode('a,b,c\n1,2,3')` | `[['a', 'b', 'c'], ['1', '2', '3']]` |

## `//encoding.csv.decoder(config <: (comma <: int, comment <: int)) <: (\(csv <: string|bytes) <: array)`
## `//encoding.csv.decoder(config <: (comma <: int, comment <: int)) <: ((csv <: string|bytes) <: array)`

`decoder` takes a tuple used to configure decoding and returns the decoding function.
| config | description |
Expand Down
6 changes: 3 additions & 3 deletions syntax/std_encoding_xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ func stdEncodingXML() rel.Attr {
return rel.NewTupleAttr(
"xml",
rel.NewNativeFunctionAttr("decode", func(_ context.Context, v rel.Value) (rel.Value, error) {
return stdXMLDecode(v, translate.XMLDecodeConfig{StripFormatting: false})
return stdXMLDecode(v, translate.XMLDecodeConfig{TrimSurroundingWhitespace: false})
}),
rel.NewNativeFunctionAttr("decoder", func(_ context.Context, config rel.Value) (rel.Value, error) {
xmlConfig := translate.XMLDecodeConfig{StripFormatting: false}
xmlConfig := translate.XMLDecodeConfig{TrimSurroundingWhitespace: false}

configTuple, ok := config.(rel.Tuple)
if !ok {
return nil, errors.Errorf("first arg to xml.decoder must be tuple, not %s", rel.ValueTypeAsString(config))
}

xmlConfig.StripFormatting = getConfigBool(configTuple, "stripFormatting")
xmlConfig.TrimSurroundingWhitespace = getConfigBool(configTuple, "trimSurroundingWhitespace")

return rel.NewNativeFunction("decode", func(_ context.Context, v rel.Value) (rel.Value, error) {
return stdXMLDecode(v, xmlConfig)
Expand Down
6 changes: 3 additions & 3 deletions syntax/std_encoding_xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func TestXMLDecoder_strip(t *testing.T) {
xml := `<<'<catalog>\n\t<book>Harry\nPotter</book>\n</catalog>'>>`
expected := `[(elem: (attrs: {}, children: [(elem: (attrs: {}, children: [(text: 'Harry\nPotter')], name: 'book'))], name: 'catalog'))]`

AssertCodesEvalToSameValue(t, expected, "//encoding.xml.decoder((stripFormatting: true))("+xml+")")
AssertCodesEvalToSameValue(t, expected, "//encoding.xml.decoder((trimSurroundingWhitespace: true))("+xml+")")
}

//nolint:lll
Expand All @@ -211,12 +211,12 @@ func TestXMLDecoder_dontStrip(t *testing.T) {
xml := `<<'<catalog>\n\t<book>Harry\nPotter</book>\n</catalog>'>>`
expected := `[(elem: (attrs: {}, children: [(text: '\n\t'), (elem: (attrs: {}, children: [(text: 'Harry\nPotter')], name: 'book')), (text: '\n')], name: 'catalog'))]`

AssertCodesEvalToSameValue(t, expected, "//encoding.xml.decoder((stripFormatting: false))("+xml+")")
AssertCodesEvalToSameValue(t, expected, "//encoding.xml.decoder((trimSurroundingWhitespace: false))("+xml+")")
}

func TestXMLDecoder_error(t *testing.T) {
t.Parallel()

AssertCodeErrors(t, "", "//encoding.xml.decoder((stripFormatting: false))(`<root>`)")
AssertCodeErrors(t, "", "//encoding.xml.decoder((trimSurroundingWhitespace: false))(`<root>`)")
AssertCodeErrors(t, "", "//encoding.xml.decoder((unknown: false))(`<root>`)")
}
4 changes: 2 additions & 2 deletions translate/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const childrenKey = "children"
// NOTE: A full cycle from XML -> Arr.ai -> XML reproduces semantically similar documents
// with possibly different content.
type XMLDecodeConfig struct {
StripFormatting bool
TrimSurroundingWhitespace bool
}

// BytesXMLToArrai converts a well formatted XML document in byte representation
Expand Down Expand Up @@ -262,7 +262,7 @@ func parseXML(decoder *xml.Decoder, config XMLDecodeConfig) (rel.Value, error) {
tuple = rel.NewTuple(rel.NewStringAttr(directive, []rune(string(t))))
case xml.CharData:
// ignore formatting new lines, tabs and spaces
if config.StripFormatting && strings.TrimSpace(string(t)) == "" {
if config.TrimSurroundingWhitespace && strings.TrimSpace(string(t)) == "" {
continue
}
tuple = rel.NewTuple(rel.NewStringAttr(charData, []rune(string(t))))
Expand Down

0 comments on commit 4d47422

Please sign in to comment.