Compare commits
90 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 65ca806488 | |||
| 5c94d86029 | |||
| b76eb62117 | |||
| 196ce3a1f6 | |||
| 9f8f82dfe8 | |||
| 661484ae7e | |||
| 34de94e6a8 | |||
| 88263a05cc | |||
| 1dbe20e76c | |||
| 05bf3807d3 | |||
| 06838de5d2 | |||
| db62263e3e | |||
| 2d866e3fae | |||
| 100799f7b7 | |||
| ecd155a62f | |||
| bcacc71a18 | |||
| 16c9a8bdc0 | |||
| f99d6bbca1 | |||
| 8784f9c73a | |||
| a60e466129 | |||
| 44aed552fd | |||
| 1479e10663 | |||
| 9ba7363552 | |||
| 96ff402934 | |||
| 249d0eaf46 | |||
| 19eb8cf036 | |||
| c5fbd3eba6 | |||
| 9ccd9bbc7a | |||
| e7d1a179ae | |||
| 71a8bd4c61 | |||
| 34782191ba | |||
| 7fbde32684 | |||
| 82a6a1977d | |||
| cc3100c329 | |||
| f1ba6388fb | |||
| d05497900e | |||
| e29a498ed5 | |||
| 2b8e33f503 | |||
| d3c92c5999 | |||
| 71c324cf7b | |||
| 4c840f1b8b | |||
| d1e0fc37ce | |||
| 947ab3f90a | |||
| e9e8265313 | |||
| a30fd2239c | |||
| 323fe5d063 | |||
| 24d4446802 | |||
| 5060c72d94 | |||
| 0a459e938d | |||
| e872682c78 | |||
| 145b18309a | |||
| 8e8d2a6aad | |||
| 3f7178ffd6 | |||
| 9fd5922321 | |||
| 610cf85ed6 | |||
| 99f8a2a010 | |||
| 556d384d4c | |||
| eb7280e4a7 | |||
| 7ee1118b4b | |||
| a12e102214 | |||
| ad60b7e437 | |||
| 3503483c73 | |||
| d2d17bccec | |||
| 76a94674c9 | |||
| 80f8b7660b | |||
| 6f6ca41621 | |||
| c4efb7477c | |||
| 903d9455db | |||
| a89a075e1b | |||
| 5e74bb91ea | |||
| 3a4d7af89e | |||
| 8a362ad712 | |||
| 5edf9acd3e | |||
| e95df67ba3 | |||
| bef0f57967 | |||
| e87c92d4f4 | |||
| 8fe62057ea | |||
| 5f42261979 | |||
| 75654e60b8 | |||
| 091e2dc498 | |||
| 095a905e04 | |||
| ec312409d3 | |||
| 26fd12ff54 | |||
| b40204d36a | |||
| 4d5afd743f | |||
| 3ded2e09ee | |||
| 781fbae71e | |||
| 68063a447e | |||
| 84da2c4a25 | |||
| dba45d427f |
@@ -1,170 +0,0 @@
|
|||||||
version: 2.1
|
|
||||||
|
|
||||||
executors:
|
|
||||||
golang:
|
|
||||||
parameters:
|
|
||||||
version:
|
|
||||||
type: string
|
|
||||||
docker:
|
|
||||||
- image: circleci/golang:<< parameters.version >>
|
|
||||||
|
|
||||||
commands:
|
|
||||||
get_deps:
|
|
||||||
description: "Get go dependencies"
|
|
||||||
steps:
|
|
||||||
- run: go get github.com/jstemmer/go-junit-report
|
|
||||||
|
|
||||||
run_test:
|
|
||||||
description: "Run unit tests for a go module"
|
|
||||||
parameters:
|
|
||||||
test_name:
|
|
||||||
type: string
|
|
||||||
module:
|
|
||||||
type: string
|
|
||||||
coverage:
|
|
||||||
default: false
|
|
||||||
type: boolean
|
|
||||||
allow_fail:
|
|
||||||
type: boolean
|
|
||||||
default: false
|
|
||||||
steps:
|
|
||||||
- run:
|
|
||||||
name: "Run tests for <<parameters.test_name>>"
|
|
||||||
command: |
|
|
||||||
TEST_DIR="/tmp/test-results/<<parameters.test_name>>"
|
|
||||||
mkdir -p ${TEST_DIR}
|
|
||||||
trap "go-junit-report </tmp/test-results/go-test.out > ${TEST_DIR}/go-test-report.xml" EXIT
|
|
||||||
go test <<parameters.module>> -race -v \
|
|
||||||
<<# parameters.coverage >>-coverprofile=/tmp/workspace/coverage.txt -covermode=atomic<</ parameters.coverage >> \
|
|
||||||
| tee /tmp/test-results/go-test.out <<# parameters.allow_fail >>|| true<</ parameters.allow_fail >>
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
go:
|
|
||||||
parameters:
|
|
||||||
version:
|
|
||||||
type: string
|
|
||||||
allow_fail:
|
|
||||||
type: boolean
|
|
||||||
default: false
|
|
||||||
executor:
|
|
||||||
name: golang
|
|
||||||
version: "<<parameters.version>>"
|
|
||||||
working_directory: /go/src/github.com/pelletier/go-toml
|
|
||||||
environment:
|
|
||||||
GO111MODULE: "on"
|
|
||||||
steps:
|
|
||||||
- checkout
|
|
||||||
- run: mkdir -p /tmp/workspace
|
|
||||||
- run: go fmt ./... <<# parameters.allow_fail >>|| true<</ parameters.allow_fail >>
|
|
||||||
- get_deps
|
|
||||||
- run_test:
|
|
||||||
test_name: "go-toml"
|
|
||||||
module: "github.com/pelletier/go-toml"
|
|
||||||
coverage: true
|
|
||||||
allow_fail: <<parameters.allow_fail>>
|
|
||||||
- run_test:
|
|
||||||
test_name: "tomljson"
|
|
||||||
module: "github.com/pelletier/go-toml/cmd/tomljson"
|
|
||||||
allow_fail: <<parameters.allow_fail>>
|
|
||||||
- run_test:
|
|
||||||
test_name: "tomll"
|
|
||||||
module: "github.com/pelletier/go-toml/cmd/tomll"
|
|
||||||
allow_fail: <<parameters.allow_fail>>
|
|
||||||
- run_test:
|
|
||||||
test_name: "query"
|
|
||||||
module: "github.com/pelletier/go-toml/query"
|
|
||||||
allow_fail: <<parameters.allow_fail>>
|
|
||||||
- store_test_results:
|
|
||||||
path: /tmp/test-results
|
|
||||||
codecov:
|
|
||||||
docker:
|
|
||||||
- image: "circleci/golang:1.12"
|
|
||||||
steps:
|
|
||||||
- attach_workspace:
|
|
||||||
at: /tmp/workspace
|
|
||||||
- run:
|
|
||||||
name: "upload to codecov"
|
|
||||||
working_directory: /tmp/workspace
|
|
||||||
command: |
|
|
||||||
curl https://codecov.io/bash > codecov.sh
|
|
||||||
bash codecov.sh -v
|
|
||||||
docker:
|
|
||||||
docker:
|
|
||||||
- image: "circleci/golang:1.12"
|
|
||||||
steps:
|
|
||||||
- checkout
|
|
||||||
- setup_remote_docker:
|
|
||||||
docker_layer_caching: true
|
|
||||||
- run: docker build -t pelletier/go-toml:$CIRCLE_SHA1 .
|
|
||||||
- run:
|
|
||||||
name: "Publish docker image"
|
|
||||||
command: |
|
|
||||||
if [ "${CIRCLE_PR_REPONAME}" == "" ]; then
|
|
||||||
IMAGE_NAME="pelletier/go-toml"
|
|
||||||
IMAGE_SHA_TAG="${IMAGE_NAME}:$CIRCLE_SHA1"
|
|
||||||
if [ "${CIRCLE_BRANCH}" = "master" ]; then
|
|
||||||
docker login -u $DOCKER_USER -p $DOCKER_PASS
|
|
||||||
docker tag ${IMAGE_SHA_TAG} ${IMAGE_NAME}:latest
|
|
||||||
docker push ${IMAGE_NAME}:latest
|
|
||||||
fi
|
|
||||||
if [ "${CIRCLE_TAG}" != "" ]; then
|
|
||||||
docker login -u $DOCKER_USER -p $DOCKER_PASS
|
|
||||||
docker tag ${IMAGE_SHA_TAG} ${IMAGE_NAME}:${CIRCLE_TAG}
|
|
||||||
docker push ${IMAGE_NAME}:${CIRCLE_TAG}
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
echo "not pushing docker image for forked repo"
|
|
||||||
fi
|
|
||||||
|
|
||||||
workflows:
|
|
||||||
version: 2.1
|
|
||||||
build:
|
|
||||||
jobs:
|
|
||||||
- go:
|
|
||||||
name: "go1_11"
|
|
||||||
version: "1.11"
|
|
||||||
- go:
|
|
||||||
name: "go1_12"
|
|
||||||
version: "1.12"
|
|
||||||
post-steps:
|
|
||||||
- run: go tool cover -html=/tmp/workspace/coverage.txt -o coverage.html
|
|
||||||
- store_artifacts:
|
|
||||||
path: /tmp/workspace/coverage.txt
|
|
||||||
- store_artifacts:
|
|
||||||
path: coverage.html
|
|
||||||
- persist_to_workspace:
|
|
||||||
root: /tmp/workspace
|
|
||||||
paths:
|
|
||||||
- coverage.txt
|
|
||||||
- go:
|
|
||||||
name: "gotip"
|
|
||||||
version: "1.12" # use as base
|
|
||||||
allow_fail: true
|
|
||||||
pre-steps:
|
|
||||||
- restore_cache:
|
|
||||||
keys:
|
|
||||||
- go-tip-source
|
|
||||||
- run:
|
|
||||||
name: "Compile go tip"
|
|
||||||
command: |
|
|
||||||
if [ ! -d "/tmp/go" ]; then
|
|
||||||
git clone https://go.googlesource.com/go /tmp/go
|
|
||||||
fi
|
|
||||||
cd /tmp/go
|
|
||||||
git checkout master
|
|
||||||
git pull
|
|
||||||
cd src
|
|
||||||
./make.bash
|
|
||||||
echo 'export PATH="/tmp/go/bin:$PATH"' >> $BASH_ENV
|
|
||||||
- run: go version
|
|
||||||
- save_cache:
|
|
||||||
key: go-tip-source
|
|
||||||
paths:
|
|
||||||
- "/tmp/go"
|
|
||||||
- codecov:
|
|
||||||
requires:
|
|
||||||
- go1_11
|
|
||||||
- go1_12
|
|
||||||
- docker:
|
|
||||||
requires:
|
|
||||||
- codecov
|
|
||||||
-22
@@ -1,22 +0,0 @@
|
|||||||
sudo: false
|
|
||||||
language: go
|
|
||||||
go:
|
|
||||||
- 1.11.x
|
|
||||||
- 1.12.x
|
|
||||||
- tip
|
|
||||||
matrix:
|
|
||||||
allow_failures:
|
|
||||||
- go: tip
|
|
||||||
fast_finish: true
|
|
||||||
env:
|
|
||||||
- GO111MODULE=on
|
|
||||||
script:
|
|
||||||
- if [ -n "$(go fmt ./...)" ]; then exit 1; fi
|
|
||||||
- go test github.com/pelletier/go-toml -race -coverprofile=coverage.txt -covermode=atomic
|
|
||||||
- go test github.com/pelletier/go-toml/cmd/tomljson
|
|
||||||
- go test github.com/pelletier/go-toml/cmd/tomll
|
|
||||||
- go test github.com/pelletier/go-toml/query
|
|
||||||
- ./benchmark.sh $TRAVIS_BRANCH https://github.com/$TRAVIS_REPO_SLUG.git
|
|
||||||
|
|
||||||
after_success:
|
|
||||||
- bash <(curl -s https://codecov.io/bash)
|
|
||||||
@@ -8,3 +8,4 @@ RUN go install ./...
|
|||||||
FROM scratch
|
FROM scratch
|
||||||
COPY --from=builder /go/bin/tomll /usr/bin/tomll
|
COPY --from=builder /go/bin/tomll /usr/bin/tomll
|
||||||
COPY --from=builder /go/bin/tomljson /usr/bin/tomljson
|
COPY --from=builder /go/bin/tomljson /usr/bin/tomljson
|
||||||
|
COPY --from=builder /go/bin/jsontoml /usr/bin/jsontoml
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
export CGO_ENABLED=0
|
||||||
|
go := go
|
||||||
|
go.goos ?= $(shell echo `go version`|cut -f4 -d ' '|cut -d '/' -f1)
|
||||||
|
go.goarch ?= $(shell echo `go version`|cut -f4 -d ' '|cut -d '/' -f2)
|
||||||
|
|
||||||
|
out.tools := tomll tomljson jsontoml
|
||||||
|
out.dist := $(out.tools:=_$(go.goos)_$(go.goarch).tar.xz)
|
||||||
|
sources := $(wildcard **/*.go)
|
||||||
|
|
||||||
|
|
||||||
|
.PHONY:
|
||||||
|
tools: $(out.tools)
|
||||||
|
|
||||||
|
$(out.tools): $(sources)
|
||||||
|
GOOS=$(go.goos) GOARCH=$(go.goarch) $(go) build ./cmd/$@
|
||||||
|
|
||||||
|
.PHONY:
|
||||||
|
dist: $(out.dist)
|
||||||
|
|
||||||
|
$(out.dist):%_$(go.goos)_$(go.goarch).tar.xz: %
|
||||||
|
if [ "$(go.goos)" = "windows" ]; then \
|
||||||
|
tar -cJf $@ $^.exe; \
|
||||||
|
else \
|
||||||
|
tar -cJf $@ $^; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
.PHONY:
|
||||||
|
clean:
|
||||||
|
rm -rf $(out.tools) $(out.dist)
|
||||||
@@ -3,12 +3,11 @@
|
|||||||
Go library for the [TOML](https://github.com/mojombo/toml) format.
|
Go library for the [TOML](https://github.com/mojombo/toml) format.
|
||||||
|
|
||||||
This library supports TOML version
|
This library supports TOML version
|
||||||
[v0.4.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.4.0.md)
|
[v1.0.0-rc.1](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v1.0.0-rc.1.md)
|
||||||
|
|
||||||
[](http://godoc.org/github.com/pelletier/go-toml)
|
[](http://godoc.org/github.com/pelletier/go-toml)
|
||||||
[](https://github.com/pelletier/go-toml/blob/master/LICENSE)
|
[](https://github.com/pelletier/go-toml/blob/master/LICENSE)
|
||||||
[](https://travis-ci.org/pelletier/go-toml)
|
[](https://dev.azure.com/pelletierthomas/go-toml-ci/_build/latest?definitionId=1&branchName=master)
|
||||||
[](https://ci.appveyor.com/project/pelletier/go-toml/branch/master)
|
|
||||||
[](https://codecov.io/gh/pelletier/go-toml)
|
[](https://codecov.io/gh/pelletier/go-toml)
|
||||||
[](https://goreportcard.com/report/github.com/pelletier/go-toml)
|
[](https://goreportcard.com/report/github.com/pelletier/go-toml)
|
||||||
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fpelletier%2Fgo-toml?ref=badge_shield)
|
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fpelletier%2Fgo-toml?ref=badge_shield)
|
||||||
@@ -19,7 +18,7 @@ Go-toml provides the following features for using data parsed from TOML document
|
|||||||
|
|
||||||
* Load TOML documents from files and string data
|
* Load TOML documents from files and string data
|
||||||
* Easily navigate TOML structure using Tree
|
* Easily navigate TOML structure using Tree
|
||||||
* Mashaling and unmarshaling to and from data structures
|
* Marshaling and unmarshaling to and from data structures
|
||||||
* Line & column position data for all parsed elements
|
* Line & column position data for all parsed elements
|
||||||
* [Query support similar to JSON-Path](query/)
|
* [Query support similar to JSON-Path](query/)
|
||||||
* Syntax errors contain line and column numbers
|
* Syntax errors contain line and column numbers
|
||||||
@@ -75,7 +74,7 @@ Or use a query:
|
|||||||
q, _ := query.Compile("$..[user,password]")
|
q, _ := query.Compile("$..[user,password]")
|
||||||
results := q.Execute(config)
|
results := q.Execute(config)
|
||||||
for ii, item := range results.Values() {
|
for ii, item := range results.Values() {
|
||||||
fmt.Println("Query result %d: %v", ii, item)
|
fmt.Printf("Query result %d: %v\n", ii, item)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -88,7 +87,7 @@ The documentation and additional examples are available at
|
|||||||
|
|
||||||
Go-toml provides two handy command line tools:
|
Go-toml provides two handy command line tools:
|
||||||
|
|
||||||
* `tomll`: Reads TOML files and lint them.
|
* `tomll`: Reads TOML files and lints them.
|
||||||
|
|
||||||
```
|
```
|
||||||
go install github.com/pelletier/go-toml/cmd/tomll
|
go install github.com/pelletier/go-toml/cmd/tomll
|
||||||
@@ -101,6 +100,13 @@ Go-toml provides two handy command line tools:
|
|||||||
tomljson --help
|
tomljson --help
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* `jsontoml`: Reads a JSON file and outputs a TOML representation.
|
||||||
|
|
||||||
|
```
|
||||||
|
go install github.com/pelletier/go-toml/cmd/jsontoml
|
||||||
|
jsontoml --help
|
||||||
|
```
|
||||||
|
|
||||||
### Docker image
|
### Docker image
|
||||||
|
|
||||||
Those tools are also availble as a Docker image from
|
Those tools are also availble as a Docker image from
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
version: "{build}"
|
|
||||||
|
|
||||||
# Source Config
|
|
||||||
clone_folder: c:\gopath\src\github.com\pelletier\go-toml
|
|
||||||
|
|
||||||
# Build host
|
|
||||||
environment:
|
|
||||||
GOPATH: c:\gopath
|
|
||||||
DEPTESTBYPASS501: 1
|
|
||||||
GOVERSION: 1.12
|
|
||||||
GO111MODULE: on
|
|
||||||
|
|
||||||
init:
|
|
||||||
- git config --global core.autocrlf input
|
|
||||||
|
|
||||||
# Build
|
|
||||||
install:
|
|
||||||
# Install the specific Go version.
|
|
||||||
- rmdir c:\go /s /q
|
|
||||||
- appveyor DownloadFile https://storage.googleapis.com/golang/go%GOVERSION%.windows-amd64.msi
|
|
||||||
- msiexec /i go%GOVERSION%.windows-amd64.msi /q
|
|
||||||
- choco install bzr
|
|
||||||
- set Path=c:\go\bin;c:\gopath\bin;C:\Program Files (x86)\Bazaar\;C:\Program Files\Mercurial\%Path%
|
|
||||||
- go version
|
|
||||||
- go env
|
|
||||||
|
|
||||||
build: false
|
|
||||||
deploy: false
|
|
||||||
|
|
||||||
test_script:
|
|
||||||
- go test github.com/pelletier/go-toml
|
|
||||||
- go test github.com/pelletier/go-toml/cmd/tomljson
|
|
||||||
- go test github.com/pelletier/go-toml/cmd/tomll
|
|
||||||
- go test github.com/pelletier/go-toml/query
|
|
||||||
@@ -0,0 +1,230 @@
|
|||||||
|
trigger:
|
||||||
|
- master
|
||||||
|
|
||||||
|
stages:
|
||||||
|
- stage: fuzzit
|
||||||
|
displayName: "Run Fuzzit"
|
||||||
|
dependsOn: []
|
||||||
|
condition: and(succeeded(), eq(variables['Build.SourceBranchName'], 'master'))
|
||||||
|
jobs:
|
||||||
|
- job: submit
|
||||||
|
displayName: "Submit"
|
||||||
|
pool:
|
||||||
|
vmImage: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- task: GoTool@0
|
||||||
|
displayName: "Install Go 1.15"
|
||||||
|
inputs:
|
||||||
|
version: "1.15"
|
||||||
|
- script: echo "##vso[task.setvariable variable=PATH]${PATH}:/home/vsts/go/bin/"
|
||||||
|
- script: mkdir -p ${HOME}/go/src/github.com/pelletier/go-toml
|
||||||
|
- script: cp -R . ${HOME}/go/src/github.com/pelletier/go-toml
|
||||||
|
- task: Bash@3
|
||||||
|
inputs:
|
||||||
|
filePath: './fuzzit.sh'
|
||||||
|
env:
|
||||||
|
TYPE: fuzzing
|
||||||
|
FUZZIT_API_KEY: $(FUZZIT_API_KEY)
|
||||||
|
|
||||||
|
- stage: run_checks
|
||||||
|
displayName: "Check"
|
||||||
|
dependsOn: []
|
||||||
|
jobs:
|
||||||
|
- job: fmt
|
||||||
|
displayName: "fmt"
|
||||||
|
pool:
|
||||||
|
vmImage: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- task: GoTool@0
|
||||||
|
displayName: "Install Go 1.15"
|
||||||
|
inputs:
|
||||||
|
version: "1.15"
|
||||||
|
- task: Go@0
|
||||||
|
displayName: "go fmt ./..."
|
||||||
|
inputs:
|
||||||
|
command: 'custom'
|
||||||
|
customCommand: 'fmt'
|
||||||
|
arguments: './...'
|
||||||
|
- job: coverage
|
||||||
|
displayName: "coverage"
|
||||||
|
pool:
|
||||||
|
vmImage: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- task: GoTool@0
|
||||||
|
displayName: "Install Go 1.15"
|
||||||
|
inputs:
|
||||||
|
version: "1.15"
|
||||||
|
- task: Go@0
|
||||||
|
displayName: "Generate coverage"
|
||||||
|
inputs:
|
||||||
|
command: 'test'
|
||||||
|
arguments: "-race -coverprofile=coverage.txt -covermode=atomic"
|
||||||
|
- task: Bash@3
|
||||||
|
inputs:
|
||||||
|
targetType: 'inline'
|
||||||
|
script: 'bash <(curl -s https://codecov.io/bash) -t ${CODECOV_TOKEN}'
|
||||||
|
env:
|
||||||
|
CODECOV_TOKEN: $(CODECOV_TOKEN)
|
||||||
|
- job: benchmark
|
||||||
|
displayName: "benchmark"
|
||||||
|
pool:
|
||||||
|
vmImage: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- task: GoTool@0
|
||||||
|
displayName: "Install Go 1.15"
|
||||||
|
inputs:
|
||||||
|
version: "1.15"
|
||||||
|
- script: echo "##vso[task.setvariable variable=PATH]${PATH}:/home/vsts/go/bin/"
|
||||||
|
- task: Bash@3
|
||||||
|
inputs:
|
||||||
|
filePath: './benchmark.sh'
|
||||||
|
arguments: "master $(Build.Repository.Uri)"
|
||||||
|
|
||||||
|
- job: fuzzing
|
||||||
|
displayName: "fuzzing"
|
||||||
|
pool:
|
||||||
|
vmImage: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- task: GoTool@0
|
||||||
|
displayName: "Install Go 1.15"
|
||||||
|
inputs:
|
||||||
|
version: "1.15"
|
||||||
|
- script: echo "##vso[task.setvariable variable=PATH]${PATH}:/home/vsts/go/bin/"
|
||||||
|
- script: mkdir -p ${HOME}/go/src/github.com/pelletier/go-toml
|
||||||
|
- script: cp -R . ${HOME}/go/src/github.com/pelletier/go-toml
|
||||||
|
- task: Bash@3
|
||||||
|
inputs:
|
||||||
|
filePath: './fuzzit.sh'
|
||||||
|
env:
|
||||||
|
TYPE: local-regression
|
||||||
|
|
||||||
|
- job: go_unit_tests
|
||||||
|
displayName: "unit tests"
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
linux 1.15:
|
||||||
|
goVersion: '1.15'
|
||||||
|
imageName: 'ubuntu-latest'
|
||||||
|
mac 1.15:
|
||||||
|
goVersion: '1.15'
|
||||||
|
imageName: 'macOS-latest'
|
||||||
|
windows 1.15:
|
||||||
|
goVersion: '1.15'
|
||||||
|
imageName: 'windows-latest'
|
||||||
|
linux 1.14:
|
||||||
|
goVersion: '1.14'
|
||||||
|
imageName: 'ubuntu-latest'
|
||||||
|
mac 1.14:
|
||||||
|
goVersion: '1.14'
|
||||||
|
imageName: 'macOS-latest'
|
||||||
|
windows 1.14:
|
||||||
|
goVersion: '1.14'
|
||||||
|
imageName: 'windows-latest'
|
||||||
|
pool:
|
||||||
|
vmImage: $(imageName)
|
||||||
|
steps:
|
||||||
|
- task: GoTool@0
|
||||||
|
displayName: "Install Go $(goVersion)"
|
||||||
|
inputs:
|
||||||
|
version: $(goVersion)
|
||||||
|
- task: Go@0
|
||||||
|
displayName: "go test ./..."
|
||||||
|
inputs:
|
||||||
|
command: 'test'
|
||||||
|
arguments: './...'
|
||||||
|
- stage: build_binaries
|
||||||
|
displayName: "Build binaries"
|
||||||
|
dependsOn: run_checks
|
||||||
|
jobs:
|
||||||
|
- job: build_binary
|
||||||
|
displayName: "Build binary"
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
linux_amd64:
|
||||||
|
GOOS: linux
|
||||||
|
GOARCH: amd64
|
||||||
|
darwin_amd64:
|
||||||
|
GOOS: darwin
|
||||||
|
GOARCH: amd64
|
||||||
|
windows_amd64:
|
||||||
|
GOOS: windows
|
||||||
|
GOARCH: amd64
|
||||||
|
pool:
|
||||||
|
vmImage: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- task: GoTool@0
|
||||||
|
displayName: "Install Go"
|
||||||
|
inputs:
|
||||||
|
version: 1.15
|
||||||
|
- task: Bash@3
|
||||||
|
inputs:
|
||||||
|
targetType: inline
|
||||||
|
script: "make dist"
|
||||||
|
env:
|
||||||
|
go.goos: $(GOOS)
|
||||||
|
go.goarch: $(GOARCH)
|
||||||
|
- task: CopyFiles@2
|
||||||
|
inputs:
|
||||||
|
sourceFolder: '$(Build.SourcesDirectory)'
|
||||||
|
contents: '*.tar.xz'
|
||||||
|
TargetFolder: '$(Build.ArtifactStagingDirectory)'
|
||||||
|
- task: PublishBuildArtifacts@1
|
||||||
|
inputs:
|
||||||
|
pathtoPublish: '$(Build.ArtifactStagingDirectory)'
|
||||||
|
artifactName: binaries
|
||||||
|
- stage: build_binaries_manifest
|
||||||
|
displayName: "Build binaries manifest"
|
||||||
|
dependsOn: build_binaries
|
||||||
|
jobs:
|
||||||
|
- job: build_manifest
|
||||||
|
displayName: "Build binaries manifest"
|
||||||
|
steps:
|
||||||
|
- task: DownloadBuildArtifacts@0
|
||||||
|
inputs:
|
||||||
|
buildType: 'current'
|
||||||
|
downloadType: 'single'
|
||||||
|
artifactName: 'binaries'
|
||||||
|
downloadPath: '$(Build.SourcesDirectory)'
|
||||||
|
- task: Bash@3
|
||||||
|
inputs:
|
||||||
|
targetType: inline
|
||||||
|
script: "cd binaries && sha256sum --binary *.tar.xz | tee $(Build.ArtifactStagingDirectory)/sha256sums.txt"
|
||||||
|
- task: PublishBuildArtifacts@1
|
||||||
|
inputs:
|
||||||
|
pathtoPublish: '$(Build.ArtifactStagingDirectory)'
|
||||||
|
artifactName: manifest
|
||||||
|
|
||||||
|
- stage: build_docker_image
|
||||||
|
displayName: "Build Docker image"
|
||||||
|
dependsOn: run_checks
|
||||||
|
jobs:
|
||||||
|
- job: build
|
||||||
|
displayName: "Build"
|
||||||
|
pool:
|
||||||
|
vmImage: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- task: Docker@2
|
||||||
|
inputs:
|
||||||
|
command: 'build'
|
||||||
|
Dockerfile: 'Dockerfile'
|
||||||
|
buildContext: '.'
|
||||||
|
addPipelineData: false
|
||||||
|
|
||||||
|
- stage: publish_docker_image
|
||||||
|
displayName: "Publish Docker image"
|
||||||
|
dependsOn: build_docker_image
|
||||||
|
condition: and(succeeded(), eq(variables['Build.SourceBranchName'], 'master'))
|
||||||
|
jobs:
|
||||||
|
- job: publish
|
||||||
|
displayName: "Publish"
|
||||||
|
pool:
|
||||||
|
vmImage: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- task: Docker@2
|
||||||
|
inputs:
|
||||||
|
containerRegistry: 'DockerHub'
|
||||||
|
repository: 'pelletier/go-toml'
|
||||||
|
command: 'buildAndPush'
|
||||||
|
Dockerfile: 'Dockerfile'
|
||||||
|
buildContext: '.'
|
||||||
|
tags: 'latest'
|
||||||
+5
-2
@@ -1,6 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
set -e
|
set -ex
|
||||||
|
|
||||||
reference_ref=${1:-master}
|
reference_ref=${1:-master}
|
||||||
reference_git=${2:-.}
|
reference_git=${2:-.}
|
||||||
@@ -8,7 +8,6 @@ reference_git=${2:-.}
|
|||||||
if ! `hash benchstat 2>/dev/null`; then
|
if ! `hash benchstat 2>/dev/null`; then
|
||||||
echo "Installing benchstat"
|
echo "Installing benchstat"
|
||||||
go get golang.org/x/perf/cmd/benchstat
|
go get golang.org/x/perf/cmd/benchstat
|
||||||
go install golang.org/x/perf/cmd/benchstat
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
tempdir=`mktemp -d /tmp/go-toml-benchmark-XXXXXX`
|
tempdir=`mktemp -d /tmp/go-toml-benchmark-XXXXXX`
|
||||||
@@ -21,11 +20,15 @@ git clone ${reference_git} ${ref_tempdir} >/dev/null 2>/dev/null
|
|||||||
pushd ${ref_tempdir} >/dev/null
|
pushd ${ref_tempdir} >/dev/null
|
||||||
git checkout ${reference_ref} >/dev/null 2>/dev/null
|
git checkout ${reference_ref} >/dev/null 2>/dev/null
|
||||||
go test -bench=. -benchmem | tee ${ref_benchmark}
|
go test -bench=. -benchmem | tee ${ref_benchmark}
|
||||||
|
cd benchmark
|
||||||
|
go test -bench=. -benchmem | tee -a ${ref_benchmark}
|
||||||
popd >/dev/null
|
popd >/dev/null
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
echo "=== local"
|
echo "=== local"
|
||||||
go test -bench=. -benchmem | tee ${local_benchmark}
|
go test -bench=. -benchmem | tee ${local_benchmark}
|
||||||
|
cd benchmark
|
||||||
|
go test -bench=. -benchmem | tee -a ${local_benchmark}
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
echo "=== diff"
|
echo "=== diff"
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package toml
|
package benchmark
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -8,7 +8,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
burntsushi "github.com/BurntSushi/toml"
|
burntsushi "github.com/BurntSushi/toml"
|
||||||
yaml "gopkg.in/yaml.v2"
|
"github.com/pelletier/go-toml"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type benchmarkDoc struct {
|
type benchmarkDoc struct {
|
||||||
@@ -124,7 +125,7 @@ func BenchmarkParseToml(b *testing.B) {
|
|||||||
}
|
}
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
_, err := LoadReader(bytes.NewReader(fileBytes))
|
_, err := toml.LoadReader(bytes.NewReader(fileBytes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -139,7 +140,7 @@ func BenchmarkUnmarshalToml(b *testing.B) {
|
|||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
target := benchmarkDoc{}
|
target := benchmarkDoc{}
|
||||||
err := Unmarshal(bytes, &target)
|
err := toml.Unmarshal(bytes, &target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
module github.com/pelletier/go-toml/benchmark
|
||||||
|
|
||||||
|
go 1.12
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/BurntSushi/toml v0.3.1
|
||||||
|
github.com/pelletier/go-toml v0.0.0
|
||||||
|
gopkg.in/yaml.v2 v2.3.0
|
||||||
|
)
|
||||||
|
|
||||||
|
replace github.com/pelletier/go-toml => ../
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||||
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||||
|
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
// Jsontoml reads JSON and converts to TOML.
|
||||||
|
//
|
||||||
|
// Usage:
|
||||||
|
// cat file.toml | jsontoml > file.json
|
||||||
|
// jsontoml file1.toml > file.json
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/pelletier/go-toml"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Usage = func() {
|
||||||
|
fmt.Fprintln(os.Stderr, "jsontoml can be used in two ways:")
|
||||||
|
fmt.Fprintln(os.Stderr, "Writing to STDIN and reading from STDOUT:")
|
||||||
|
fmt.Fprintln(os.Stderr, "")
|
||||||
|
fmt.Fprintln(os.Stderr, "")
|
||||||
|
fmt.Fprintln(os.Stderr, "Reading from a file name:")
|
||||||
|
fmt.Fprintln(os.Stderr, " tomljson file.toml")
|
||||||
|
}
|
||||||
|
flag.Parse()
|
||||||
|
os.Exit(processMain(flag.Args(), os.Stdin, os.Stdout, os.Stderr))
|
||||||
|
}
|
||||||
|
|
||||||
|
func processMain(files []string, defaultInput io.Reader, output io.Writer, errorOutput io.Writer) int {
|
||||||
|
// read from stdin and print to stdout
|
||||||
|
inputReader := defaultInput
|
||||||
|
|
||||||
|
if len(files) > 0 {
|
||||||
|
file, err := os.Open(files[0])
|
||||||
|
if err != nil {
|
||||||
|
printError(err, errorOutput)
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
inputReader = file
|
||||||
|
defer file.Close()
|
||||||
|
}
|
||||||
|
s, err := reader(inputReader)
|
||||||
|
if err != nil {
|
||||||
|
printError(err, errorOutput)
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
io.WriteString(output, s)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func printError(err error, output io.Writer) {
|
||||||
|
io.WriteString(output, err.Error()+"\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func reader(r io.Reader) (string, error) {
|
||||||
|
jsonMap := make(map[string]interface{})
|
||||||
|
jsonBytes, err := ioutil.ReadAll(r)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(jsonBytes, &jsonMap)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
tree, err := toml.TreeFromMap(jsonMap)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return mapToTOML(tree)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mapToTOML(t *toml.Tree) (string, error) {
|
||||||
|
tomlBytes, err := t.ToTomlString()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(tomlBytes[:]), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func expectBufferEquality(t *testing.T, name string, buffer *bytes.Buffer, expected string) {
|
||||||
|
output := buffer.String()
|
||||||
|
if output != expected {
|
||||||
|
t.Errorf("incorrect %s: \n%sexpected %s: \n%s", name, output, name, expected)
|
||||||
|
t.Log([]rune(output))
|
||||||
|
t.Log([]rune(expected))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func expectProcessMainResults(t *testing.T, input string, args []string, exitCode int, expectedOutput string, expectedError string) {
|
||||||
|
inputReader := strings.NewReader(input)
|
||||||
|
|
||||||
|
outputBuffer := new(bytes.Buffer)
|
||||||
|
errorBuffer := new(bytes.Buffer)
|
||||||
|
|
||||||
|
returnCode := processMain(args, inputReader, outputBuffer, errorBuffer)
|
||||||
|
|
||||||
|
expectBufferEquality(t, "output", outputBuffer, expectedOutput)
|
||||||
|
expectBufferEquality(t, "error", errorBuffer, expectedError)
|
||||||
|
|
||||||
|
if returnCode != exitCode {
|
||||||
|
t.Error("incorrect return code:", returnCode, "expected", exitCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProcessMainReadFromStdin(t *testing.T) {
|
||||||
|
expectedOutput := `
|
||||||
|
[mytoml]
|
||||||
|
a = 42.0
|
||||||
|
`
|
||||||
|
input := `{
|
||||||
|
"mytoml": {
|
||||||
|
"a": 42
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
expectedError := ``
|
||||||
|
expectedExitCode := 0
|
||||||
|
|
||||||
|
expectProcessMainResults(t, input, []string{}, expectedExitCode, expectedOutput, expectedError)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProcessMainReadFromFile(t *testing.T) {
|
||||||
|
input := `{
|
||||||
|
"mytoml": {
|
||||||
|
"a": 42
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
tmpfile, err := ioutil.TempFile("", "example.json")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := tmpfile.Write([]byte(input)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer os.Remove(tmpfile.Name())
|
||||||
|
|
||||||
|
expectedOutput := `
|
||||||
|
[mytoml]
|
||||||
|
a = 42.0
|
||||||
|
`
|
||||||
|
expectedError := ``
|
||||||
|
expectedExitCode := 0
|
||||||
|
|
||||||
|
expectProcessMainResults(t, ``, []string{tmpfile.Name()}, expectedExitCode, expectedOutput, expectedError)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProcessMainReadFromMissingFile(t *testing.T) {
|
||||||
|
var expectedError string
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
expectedError = `open /this/file/does/not/exist: The system cannot find the path specified.
|
||||||
|
`
|
||||||
|
} else {
|
||||||
|
expectedError = `open /this/file/does/not/exist: no such file or directory
|
||||||
|
`
|
||||||
|
}
|
||||||
|
|
||||||
|
expectProcessMainResults(t, ``, []string{"/this/file/does/not/exist"}, -1, ``, expectedError)
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
// Package toml is a TOML parser and manipulation library.
|
// Package toml is a TOML parser and manipulation library.
|
||||||
//
|
//
|
||||||
// This version supports the specification as described in
|
// This version supports the specification as described in
|
||||||
// https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.4.0.md
|
// https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.5.0.md
|
||||||
//
|
//
|
||||||
// Marshaling
|
// Marshaling
|
||||||
//
|
//
|
||||||
|
|||||||
+64
@@ -5,6 +5,7 @@ package toml_test
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
toml "github.com/pelletier/go-toml"
|
toml "github.com/pelletier/go-toml"
|
||||||
)
|
)
|
||||||
@@ -104,3 +105,66 @@ func ExampleUnmarshal() {
|
|||||||
// Output:
|
// Output:
|
||||||
// user= pelletier
|
// user= pelletier
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleEncoder_anonymous() {
|
||||||
|
type Credentials struct {
|
||||||
|
User string `toml:"user"`
|
||||||
|
Password string `toml:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Protocol struct {
|
||||||
|
Name string `toml:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Version int `toml:"version"`
|
||||||
|
Credentials
|
||||||
|
Protocol `toml:"Protocol"`
|
||||||
|
}
|
||||||
|
config := Config{
|
||||||
|
Version: 2,
|
||||||
|
Credentials: Credentials{
|
||||||
|
User: "pelletier",
|
||||||
|
Password: "mypassword",
|
||||||
|
},
|
||||||
|
Protocol: Protocol{
|
||||||
|
Name: "tcp",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
fmt.Println("Default:")
|
||||||
|
fmt.Println("---------------")
|
||||||
|
|
||||||
|
def := toml.NewEncoder(os.Stdout)
|
||||||
|
if err := def.Encode(config); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("---------------")
|
||||||
|
fmt.Println("With promotion:")
|
||||||
|
fmt.Println("---------------")
|
||||||
|
|
||||||
|
prom := toml.NewEncoder(os.Stdout).PromoteAnonymous(true)
|
||||||
|
if err := prom.Encode(config); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
// Output:
|
||||||
|
// Default:
|
||||||
|
// ---------------
|
||||||
|
// password = "mypassword"
|
||||||
|
// user = "pelletier"
|
||||||
|
// version = 2
|
||||||
|
//
|
||||||
|
// [Protocol]
|
||||||
|
// name = "tcp"
|
||||||
|
// ---------------
|
||||||
|
// With promotion:
|
||||||
|
// ---------------
|
||||||
|
// version = 2
|
||||||
|
//
|
||||||
|
// [Credentials]
|
||||||
|
// password = "mypassword"
|
||||||
|
// user = "pelletier"
|
||||||
|
//
|
||||||
|
// [Protocol]
|
||||||
|
// name = "tcp"
|
||||||
|
}
|
||||||
|
|||||||
@@ -27,3 +27,4 @@ enabled = true
|
|||||||
|
|
||||||
[clients]
|
[clients]
|
||||||
data = [ ["gamma", "delta"], [1, 2] ] # just an update to make sure parsers support it
|
data = [ ["gamma", "delta"], [1, 2] ] # just an update to make sure parsers support it
|
||||||
|
score = 4e-08 # to make sure leading zeroes in exponent parts of floats are supported
|
||||||
@@ -27,3 +27,4 @@ enabled = true
|
|||||||
|
|
||||||
[clients]
|
[clients]
|
||||||
data = [ ["gamma", "delta"], [1, 2] ] # just an update to make sure parsers support it
|
data = [ ["gamma", "delta"], [1, 2] ] # just an update to make sure parsers support it
|
||||||
|
score = 4e-08 # to make sure leading zeroes in exponent parts of floats are supported
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -xe
|
||||||
|
|
||||||
|
# go-fuzz doesn't support modules yet, so ensure we do everything
|
||||||
|
# in the old style GOPATH way
|
||||||
|
export GO111MODULE="off"
|
||||||
|
|
||||||
|
# install go-fuzz
|
||||||
|
go get -u github.com/dvyukov/go-fuzz/go-fuzz github.com/dvyukov/go-fuzz/go-fuzz-build
|
||||||
|
|
||||||
|
# target name can only contain lower-case letters (a-z), digits (0-9) and a dash (-)
|
||||||
|
# to add another target, make sure to create it with `fuzzit create target`
|
||||||
|
# before using `fuzzit create job`
|
||||||
|
TARGET=toml-fuzzer
|
||||||
|
|
||||||
|
go-fuzz-build -libfuzzer -o ${TARGET}.a github.com/pelletier/go-toml
|
||||||
|
clang -fsanitize=fuzzer ${TARGET}.a -o ${TARGET}
|
||||||
|
|
||||||
|
# install fuzzit for talking to fuzzit.dev service
|
||||||
|
# or latest version:
|
||||||
|
# https://github.com/fuzzitdev/fuzzit/releases/latest/download/fuzzit_Linux_x86_64
|
||||||
|
wget -q -O fuzzit https://github.com/fuzzitdev/fuzzit/releases/download/v2.4.52/fuzzit_Linux_x86_64
|
||||||
|
chmod a+x fuzzit
|
||||||
|
|
||||||
|
# TODO: change kkowalczyk to go-toml and create toml-fuzzer target there
|
||||||
|
./fuzzit create job --type $TYPE go-toml/${TARGET} ${TARGET}
|
||||||
@@ -2,8 +2,4 @@ module github.com/pelletier/go-toml
|
|||||||
|
|
||||||
go 1.12
|
go 1.12
|
||||||
|
|
||||||
require (
|
require github.com/davecgh/go-spew v1.1.1
|
||||||
github.com/BurntSushi/toml v0.3.1
|
|
||||||
github.com/davecgh/go-spew v1.1.1
|
|
||||||
gopkg.in/yaml.v2 v2.2.2
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -5,3 +5,15 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
|||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
|
||||||
|
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c=
|
||||||
|
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
|
||||||
|
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||||
|
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
|||||||
+1
-2
@@ -5,7 +5,6 @@ package toml
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"unicode"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Convert the bare key group string to an array.
|
// Convert the bare key group string to an array.
|
||||||
@@ -109,5 +108,5 @@ func parseKey(key string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isValidBareChar(r rune) bool {
|
func isValidBareChar(r rune) bool {
|
||||||
return isAlphanumeric(r) || r == '-' || unicode.IsNumber(r)
|
return isAlphanumeric(r) || r == '-' || isDigit(r)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ type tomlLexer struct {
|
|||||||
currentTokenStart int
|
currentTokenStart int
|
||||||
currentTokenStop int
|
currentTokenStop int
|
||||||
tokens []token
|
tokens []token
|
||||||
depth int
|
brackets []rune
|
||||||
line int
|
line int
|
||||||
col int
|
col int
|
||||||
endbufferLine int
|
endbufferLine int
|
||||||
@@ -123,6 +123,8 @@ func (l *tomlLexer) lexVoid() tomlLexStateFn {
|
|||||||
for {
|
for {
|
||||||
next := l.peek()
|
next := l.peek()
|
||||||
switch next {
|
switch next {
|
||||||
|
case '}': // after '{'
|
||||||
|
return l.lexRightCurlyBrace
|
||||||
case '[':
|
case '[':
|
||||||
return l.lexTableKey
|
return l.lexTableKey
|
||||||
case '#':
|
case '#':
|
||||||
@@ -140,10 +142,6 @@ func (l *tomlLexer) lexVoid() tomlLexStateFn {
|
|||||||
l.skip()
|
l.skip()
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.depth > 0 {
|
|
||||||
return l.lexRvalue
|
|
||||||
}
|
|
||||||
|
|
||||||
if isKeyStartChar(next) {
|
if isKeyStartChar(next) {
|
||||||
return l.lexKey
|
return l.lexKey
|
||||||
}
|
}
|
||||||
@@ -167,10 +165,8 @@ func (l *tomlLexer) lexRvalue() tomlLexStateFn {
|
|||||||
case '=':
|
case '=':
|
||||||
return l.lexEqual
|
return l.lexEqual
|
||||||
case '[':
|
case '[':
|
||||||
l.depth++
|
|
||||||
return l.lexLeftBracket
|
return l.lexLeftBracket
|
||||||
case ']':
|
case ']':
|
||||||
l.depth--
|
|
||||||
return l.lexRightBracket
|
return l.lexRightBracket
|
||||||
case '{':
|
case '{':
|
||||||
return l.lexLeftCurlyBrace
|
return l.lexLeftCurlyBrace
|
||||||
@@ -188,12 +184,10 @@ func (l *tomlLexer) lexRvalue() tomlLexStateFn {
|
|||||||
fallthrough
|
fallthrough
|
||||||
case '\n':
|
case '\n':
|
||||||
l.skip()
|
l.skip()
|
||||||
if l.depth == 0 {
|
if len(l.brackets) > 0 && l.brackets[len(l.brackets)-1] == '[' {
|
||||||
return l.lexVoid
|
|
||||||
}
|
|
||||||
return l.lexRvalue
|
return l.lexRvalue
|
||||||
case '_':
|
}
|
||||||
return l.errorf("cannot start number with underscore")
|
return l.lexVoid
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.follow("true") {
|
if l.follow("true") {
|
||||||
@@ -223,9 +217,12 @@ func (l *tomlLexer) lexRvalue() tomlLexStateFn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
possibleDate := l.peekString(35)
|
possibleDate := l.peekString(35)
|
||||||
dateMatch := dateRegexp.FindString(possibleDate)
|
dateSubmatches := dateRegexp.FindStringSubmatch(possibleDate)
|
||||||
if dateMatch != "" {
|
if dateSubmatches != nil && dateSubmatches[0] != "" {
|
||||||
l.fastForward(len(dateMatch))
|
l.fastForward(len(dateSubmatches[0]))
|
||||||
|
if dateSubmatches[2] == "" { // no timezone information => local date
|
||||||
|
return l.lexLocalDate
|
||||||
|
}
|
||||||
return l.lexDate
|
return l.lexDate
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,10 +230,6 @@ func (l *tomlLexer) lexRvalue() tomlLexStateFn {
|
|||||||
return l.lexNumber
|
return l.lexNumber
|
||||||
}
|
}
|
||||||
|
|
||||||
if isAlphanumeric(next) {
|
|
||||||
return l.lexKey
|
|
||||||
}
|
|
||||||
|
|
||||||
return l.errorf("no value can start with %c", next)
|
return l.errorf("no value can start with %c", next)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,12 +240,17 @@ func (l *tomlLexer) lexRvalue() tomlLexStateFn {
|
|||||||
func (l *tomlLexer) lexLeftCurlyBrace() tomlLexStateFn {
|
func (l *tomlLexer) lexLeftCurlyBrace() tomlLexStateFn {
|
||||||
l.next()
|
l.next()
|
||||||
l.emit(tokenLeftCurlyBrace)
|
l.emit(tokenLeftCurlyBrace)
|
||||||
return l.lexRvalue
|
l.brackets = append(l.brackets, '{')
|
||||||
|
return l.lexVoid
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *tomlLexer) lexRightCurlyBrace() tomlLexStateFn {
|
func (l *tomlLexer) lexRightCurlyBrace() tomlLexStateFn {
|
||||||
l.next()
|
l.next()
|
||||||
l.emit(tokenRightCurlyBrace)
|
l.emit(tokenRightCurlyBrace)
|
||||||
|
if len(l.brackets) == 0 || l.brackets[len(l.brackets)-1] != '{' {
|
||||||
|
return l.errorf("cannot have '}' here")
|
||||||
|
}
|
||||||
|
l.brackets = l.brackets[:len(l.brackets)-1]
|
||||||
return l.lexRvalue
|
return l.lexRvalue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,6 +259,11 @@ func (l *tomlLexer) lexDate() tomlLexStateFn {
|
|||||||
return l.lexRvalue
|
return l.lexRvalue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *tomlLexer) lexLocalDate() tomlLexStateFn {
|
||||||
|
l.emit(tokenLocalDate)
|
||||||
|
return l.lexRvalue
|
||||||
|
}
|
||||||
|
|
||||||
func (l *tomlLexer) lexTrue() tomlLexStateFn {
|
func (l *tomlLexer) lexTrue() tomlLexStateFn {
|
||||||
l.fastForward(4)
|
l.fastForward(4)
|
||||||
l.emit(tokenTrue)
|
l.emit(tokenTrue)
|
||||||
@@ -294,13 +297,16 @@ func (l *tomlLexer) lexEqual() tomlLexStateFn {
|
|||||||
func (l *tomlLexer) lexComma() tomlLexStateFn {
|
func (l *tomlLexer) lexComma() tomlLexStateFn {
|
||||||
l.next()
|
l.next()
|
||||||
l.emit(tokenComma)
|
l.emit(tokenComma)
|
||||||
|
if len(l.brackets) > 0 && l.brackets[len(l.brackets)-1] == '{' {
|
||||||
|
return l.lexVoid
|
||||||
|
}
|
||||||
return l.lexRvalue
|
return l.lexRvalue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the key and emits its value without escape sequences.
|
// Parse the key and emits its value without escape sequences.
|
||||||
// bare keys, basic string keys and literal string keys are supported.
|
// bare keys, basic string keys and literal string keys are supported.
|
||||||
func (l *tomlLexer) lexKey() tomlLexStateFn {
|
func (l *tomlLexer) lexKey() tomlLexStateFn {
|
||||||
growingString := ""
|
var sb strings.Builder
|
||||||
|
|
||||||
for r := l.peek(); isKeyChar(r) || r == '\n' || r == '\r'; r = l.peek() {
|
for r := l.peek(); isKeyChar(r) || r == '\n' || r == '\r'; r = l.peek() {
|
||||||
if r == '"' {
|
if r == '"' {
|
||||||
@@ -309,7 +315,9 @@ func (l *tomlLexer) lexKey() tomlLexStateFn {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return l.errorf(err.Error())
|
return l.errorf(err.Error())
|
||||||
}
|
}
|
||||||
growingString += "\"" + str + "\""
|
sb.WriteString("\"")
|
||||||
|
sb.WriteString(str)
|
||||||
|
sb.WriteString("\"")
|
||||||
l.next()
|
l.next()
|
||||||
continue
|
continue
|
||||||
} else if r == '\'' {
|
} else if r == '\'' {
|
||||||
@@ -318,22 +326,45 @@ func (l *tomlLexer) lexKey() tomlLexStateFn {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return l.errorf(err.Error())
|
return l.errorf(err.Error())
|
||||||
}
|
}
|
||||||
growingString += "'" + str + "'"
|
sb.WriteString("'")
|
||||||
|
sb.WriteString(str)
|
||||||
|
sb.WriteString("'")
|
||||||
l.next()
|
l.next()
|
||||||
continue
|
continue
|
||||||
} else if r == '\n' {
|
} else if r == '\n' {
|
||||||
return l.errorf("keys cannot contain new lines")
|
return l.errorf("keys cannot contain new lines")
|
||||||
} else if isSpace(r) {
|
} else if isSpace(r) {
|
||||||
|
var str strings.Builder
|
||||||
|
str.WriteString(" ")
|
||||||
|
|
||||||
|
// skip trailing whitespace
|
||||||
|
l.next()
|
||||||
|
for r = l.peek(); isSpace(r); r = l.peek() {
|
||||||
|
str.WriteRune(r)
|
||||||
|
l.next()
|
||||||
|
}
|
||||||
|
// break loop if not a dot
|
||||||
|
if r != '.' {
|
||||||
break
|
break
|
||||||
|
}
|
||||||
|
str.WriteString(".")
|
||||||
|
// skip trailing whitespace after dot
|
||||||
|
l.next()
|
||||||
|
for r = l.peek(); isSpace(r); r = l.peek() {
|
||||||
|
str.WriteRune(r)
|
||||||
|
l.next()
|
||||||
|
}
|
||||||
|
sb.WriteString(str.String())
|
||||||
|
continue
|
||||||
} else if r == '.' {
|
} else if r == '.' {
|
||||||
// skip
|
// skip
|
||||||
} else if !isValidBareChar(r) {
|
} else if !isValidBareChar(r) {
|
||||||
return l.errorf("keys cannot contain %c character", r)
|
return l.errorf("keys cannot contain %c character", r)
|
||||||
}
|
}
|
||||||
growingString += string(r)
|
sb.WriteRune(r)
|
||||||
l.next()
|
l.next()
|
||||||
}
|
}
|
||||||
l.emitWithValue(tokenKey, growingString)
|
l.emitWithValue(tokenKey, sb.String())
|
||||||
return l.lexVoid
|
return l.lexVoid
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -353,11 +384,12 @@ func (l *tomlLexer) lexComment(previousState tomlLexStateFn) tomlLexStateFn {
|
|||||||
func (l *tomlLexer) lexLeftBracket() tomlLexStateFn {
|
func (l *tomlLexer) lexLeftBracket() tomlLexStateFn {
|
||||||
l.next()
|
l.next()
|
||||||
l.emit(tokenLeftBracket)
|
l.emit(tokenLeftBracket)
|
||||||
|
l.brackets = append(l.brackets, '[')
|
||||||
return l.lexRvalue
|
return l.lexRvalue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *tomlLexer) lexLiteralStringAsString(terminator string, discardLeadingNewLine bool) (string, error) {
|
func (l *tomlLexer) lexLiteralStringAsString(terminator string, discardLeadingNewLine bool) (string, error) {
|
||||||
growingString := ""
|
var sb strings.Builder
|
||||||
|
|
||||||
if discardLeadingNewLine {
|
if discardLeadingNewLine {
|
||||||
if l.follow("\r\n") {
|
if l.follow("\r\n") {
|
||||||
@@ -371,14 +403,14 @@ func (l *tomlLexer) lexLiteralStringAsString(terminator string, discardLeadingNe
|
|||||||
// find end of string
|
// find end of string
|
||||||
for {
|
for {
|
||||||
if l.follow(terminator) {
|
if l.follow(terminator) {
|
||||||
return growingString, nil
|
return sb.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
next := l.peek()
|
next := l.peek()
|
||||||
if next == eof {
|
if next == eof {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
growingString += string(l.next())
|
sb.WriteRune(l.next())
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", errors.New("unclosed string")
|
return "", errors.New("unclosed string")
|
||||||
@@ -412,7 +444,7 @@ func (l *tomlLexer) lexLiteralString() tomlLexStateFn {
|
|||||||
// Terminator is the substring indicating the end of the token.
|
// Terminator is the substring indicating the end of the token.
|
||||||
// The resulting string does not include the terminator.
|
// The resulting string does not include the terminator.
|
||||||
func (l *tomlLexer) lexStringAsString(terminator string, discardLeadingNewLine, acceptNewLines bool) (string, error) {
|
func (l *tomlLexer) lexStringAsString(terminator string, discardLeadingNewLine, acceptNewLines bool) (string, error) {
|
||||||
growingString := ""
|
var sb strings.Builder
|
||||||
|
|
||||||
if discardLeadingNewLine {
|
if discardLeadingNewLine {
|
||||||
if l.follow("\r\n") {
|
if l.follow("\r\n") {
|
||||||
@@ -425,7 +457,7 @@ func (l *tomlLexer) lexStringAsString(terminator string, discardLeadingNewLine,
|
|||||||
|
|
||||||
for {
|
for {
|
||||||
if l.follow(terminator) {
|
if l.follow(terminator) {
|
||||||
return growingString, nil
|
return sb.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.follow("\\") {
|
if l.follow("\\") {
|
||||||
@@ -443,72 +475,72 @@ func (l *tomlLexer) lexStringAsString(terminator string, discardLeadingNewLine,
|
|||||||
l.next()
|
l.next()
|
||||||
}
|
}
|
||||||
case '"':
|
case '"':
|
||||||
growingString += "\""
|
sb.WriteString("\"")
|
||||||
l.next()
|
l.next()
|
||||||
case 'n':
|
case 'n':
|
||||||
growingString += "\n"
|
sb.WriteString("\n")
|
||||||
l.next()
|
l.next()
|
||||||
case 'b':
|
case 'b':
|
||||||
growingString += "\b"
|
sb.WriteString("\b")
|
||||||
l.next()
|
l.next()
|
||||||
case 'f':
|
case 'f':
|
||||||
growingString += "\f"
|
sb.WriteString("\f")
|
||||||
l.next()
|
l.next()
|
||||||
case '/':
|
case '/':
|
||||||
growingString += "/"
|
sb.WriteString("/")
|
||||||
l.next()
|
l.next()
|
||||||
case 't':
|
case 't':
|
||||||
growingString += "\t"
|
sb.WriteString("\t")
|
||||||
l.next()
|
l.next()
|
||||||
case 'r':
|
case 'r':
|
||||||
growingString += "\r"
|
sb.WriteString("\r")
|
||||||
l.next()
|
l.next()
|
||||||
case '\\':
|
case '\\':
|
||||||
growingString += "\\"
|
sb.WriteString("\\")
|
||||||
l.next()
|
l.next()
|
||||||
case 'u':
|
case 'u':
|
||||||
l.next()
|
l.next()
|
||||||
code := ""
|
var code strings.Builder
|
||||||
for i := 0; i < 4; i++ {
|
for i := 0; i < 4; i++ {
|
||||||
c := l.peek()
|
c := l.peek()
|
||||||
if !isHexDigit(c) {
|
if !isHexDigit(c) {
|
||||||
return "", errors.New("unfinished unicode escape")
|
return "", errors.New("unfinished unicode escape")
|
||||||
}
|
}
|
||||||
l.next()
|
l.next()
|
||||||
code = code + string(c)
|
code.WriteRune(c)
|
||||||
}
|
}
|
||||||
intcode, err := strconv.ParseInt(code, 16, 32)
|
intcode, err := strconv.ParseInt(code.String(), 16, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New("invalid unicode escape: \\u" + code)
|
return "", errors.New("invalid unicode escape: \\u" + code.String())
|
||||||
}
|
}
|
||||||
growingString += string(rune(intcode))
|
sb.WriteRune(rune(intcode))
|
||||||
case 'U':
|
case 'U':
|
||||||
l.next()
|
l.next()
|
||||||
code := ""
|
var code strings.Builder
|
||||||
for i := 0; i < 8; i++ {
|
for i := 0; i < 8; i++ {
|
||||||
c := l.peek()
|
c := l.peek()
|
||||||
if !isHexDigit(c) {
|
if !isHexDigit(c) {
|
||||||
return "", errors.New("unfinished unicode escape")
|
return "", errors.New("unfinished unicode escape")
|
||||||
}
|
}
|
||||||
l.next()
|
l.next()
|
||||||
code = code + string(c)
|
code.WriteRune(c)
|
||||||
}
|
}
|
||||||
intcode, err := strconv.ParseInt(code, 16, 64)
|
intcode, err := strconv.ParseInt(code.String(), 16, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New("invalid unicode escape: \\U" + code)
|
return "", errors.New("invalid unicode escape: \\U" + code.String())
|
||||||
}
|
}
|
||||||
growingString += string(rune(intcode))
|
sb.WriteRune(rune(intcode))
|
||||||
default:
|
default:
|
||||||
return "", errors.New("invalid escape sequence: \\" + string(l.peek()))
|
return "", errors.New("invalid escape sequence: \\" + string(l.peek()))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
r := l.peek()
|
r := l.peek()
|
||||||
|
|
||||||
if 0x00 <= r && r <= 0x1F && !(acceptNewLines && (r == '\n' || r == '\r')) {
|
if 0x00 <= r && r <= 0x1F && r != '\t' && !(acceptNewLines && (r == '\n' || r == '\r')) {
|
||||||
return "", fmt.Errorf("unescaped control character %U", r)
|
return "", fmt.Errorf("unescaped control character %U", r)
|
||||||
}
|
}
|
||||||
l.next()
|
l.next()
|
||||||
growingString += string(r)
|
sb.WriteRune(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.peek() == eof {
|
if l.peek() == eof {
|
||||||
@@ -535,7 +567,6 @@ func (l *tomlLexer) lexString() tomlLexStateFn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
str, err := l.lexStringAsString(terminator, discardLeadingNewLine, acceptNewLines)
|
str, err := l.lexStringAsString(terminator, discardLeadingNewLine, acceptNewLines)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return l.errorf(err.Error())
|
return l.errorf(err.Error())
|
||||||
}
|
}
|
||||||
@@ -607,6 +638,10 @@ func (l *tomlLexer) lexInsideTableKey() tomlLexStateFn {
|
|||||||
func (l *tomlLexer) lexRightBracket() tomlLexStateFn {
|
func (l *tomlLexer) lexRightBracket() tomlLexStateFn {
|
||||||
l.next()
|
l.next()
|
||||||
l.emit(tokenRightBracket)
|
l.emit(tokenRightBracket)
|
||||||
|
if len(l.brackets) == 0 || l.brackets[len(l.brackets)-1] != '[' {
|
||||||
|
return l.errorf("cannot have ']' here")
|
||||||
|
}
|
||||||
|
l.brackets = l.brackets[:len(l.brackets)-1]
|
||||||
return l.lexRvalue
|
return l.lexRvalue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -733,7 +768,27 @@ func (l *tomlLexer) run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
dateRegexp = regexp.MustCompile(`^\d{1,4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,9})?(Z|[+-]\d{2}:\d{2})`)
|
// Regexp for all date/time formats supported by TOML.
|
||||||
|
// Group 1: nano precision
|
||||||
|
// Group 2: timezone
|
||||||
|
//
|
||||||
|
// /!\ also matches the empty string
|
||||||
|
//
|
||||||
|
// Example matches:
|
||||||
|
// 1979-05-27T07:32:00Z
|
||||||
|
// 1979-05-27T00:32:00-07:00
|
||||||
|
// 1979-05-27T00:32:00.999999-07:00
|
||||||
|
// 1979-05-27 07:32:00Z
|
||||||
|
// 1979-05-27 00:32:00-07:00
|
||||||
|
// 1979-05-27 00:32:00.999999-07:00
|
||||||
|
// 1979-05-27T07:32:00
|
||||||
|
// 1979-05-27T00:32:00.999999
|
||||||
|
// 1979-05-27 07:32:00
|
||||||
|
// 1979-05-27 00:32:00.999999
|
||||||
|
// 1979-05-27
|
||||||
|
// 07:32:00
|
||||||
|
// 00:32:00.999999
|
||||||
|
dateRegexp = regexp.MustCompile(`^(?:\d{1,4}-\d{2}-\d{2})?(?:[T ]?\d{2}:\d{2}:\d{2}(\.\d{1,9})?(Z|[+-]\d{2}:\d{2})?)?`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Entry point
|
// Entry point
|
||||||
|
|||||||
+242
-14
@@ -8,7 +8,7 @@ import (
|
|||||||
func testFlow(t *testing.T, input string, expectedFlow []token) {
|
func testFlow(t *testing.T, input string, expectedFlow []token) {
|
||||||
tokens := lexToml([]byte(input))
|
tokens := lexToml([]byte(input))
|
||||||
if !reflect.DeepEqual(tokens, expectedFlow) {
|
if !reflect.DeepEqual(tokens, expectedFlow) {
|
||||||
t.Fatal("Different flows. Expected\n", expectedFlow, "\nGot:\n", tokens)
|
t.Fatalf("Different flows.\nExpected:\n%v\nGot:\n%v", expectedFlow, tokens)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,11 +22,20 @@ func TestValidKeyGroup(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNestedQuotedUnicodeKeyGroup(t *testing.T) {
|
func TestNestedQuotedUnicodeKeyGroup(t *testing.T) {
|
||||||
testFlow(t, `[ j . "ʞ" . l ]`, []token{
|
testFlow(t, `[ j . "ʞ" . l . 'ɯ' ]`, []token{
|
||||||
{Position{1, 1}, tokenLeftBracket, "["},
|
{Position{1, 1}, tokenLeftBracket, "["},
|
||||||
{Position{1, 2}, tokenKeyGroup, ` j . "ʞ" . l `},
|
{Position{1, 2}, tokenKeyGroup, ` j . "ʞ" . l . 'ɯ' `},
|
||||||
{Position{1, 15}, tokenRightBracket, "]"},
|
{Position{1, 21}, tokenRightBracket, "]"},
|
||||||
{Position{1, 16}, tokenEOF, ""},
|
{Position{1, 22}, tokenEOF, ""},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNestedQuotedUnicodeKeyAssign(t *testing.T) {
|
||||||
|
testFlow(t, ` j . "ʞ" . l . 'ɯ' = 3`, []token{
|
||||||
|
{Position{1, 2}, tokenKey, `j . "ʞ" . l . 'ɯ'`},
|
||||||
|
{Position{1, 20}, tokenEqual, "="},
|
||||||
|
{Position{1, 22}, tokenInteger, "3"},
|
||||||
|
{Position{1, 23}, tokenEOF, ""},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,9 +114,9 @@ func TestBasicKeyWithUppercaseMix(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestBasicKeyWithInternationalCharacters(t *testing.T) {
|
func TestBasicKeyWithInternationalCharacters(t *testing.T) {
|
||||||
testFlow(t, "héllÖ", []token{
|
testFlow(t, "'héllÖ'", []token{
|
||||||
{Position{1, 1}, tokenKey, "héllÖ"},
|
{Position{1, 1}, tokenKey, "'héllÖ'"},
|
||||||
{Position{1, 6}, tokenEOF, ""},
|
{Position{1, 8}, tokenEOF, ""},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,14 +299,29 @@ func TestKeyEqualArrayBoolsWithComments(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDateRegexp(t *testing.T) {
|
func TestDateRegexp(t *testing.T) {
|
||||||
if dateRegexp.FindString("1979-05-27T07:32:00Z") == "" {
|
cases := map[string]string{
|
||||||
t.Error("basic lexing")
|
"basic": "1979-05-27T07:32:00Z",
|
||||||
|
"offset": "1979-05-27T00:32:00-07:00",
|
||||||
|
"nano precision": "1979-05-27T00:32:00.999999-07:00",
|
||||||
|
"basic-no-T": "1979-05-27 07:32:00Z",
|
||||||
|
"offset-no-T": "1979-05-27 00:32:00-07:00",
|
||||||
|
"nano precision-no-T": "1979-05-27 00:32:00.999999-07:00",
|
||||||
|
"no-tz": "1979-05-27T07:32:00",
|
||||||
|
"no-tz-nano": "1979-05-27T00:32:00.999999",
|
||||||
|
"no-tz-no-t": "1979-05-27 07:32:00",
|
||||||
|
"no-tz-no-t-nano": "1979-05-27 00:32:00.999999",
|
||||||
|
"date-no-tz": "1979-05-27",
|
||||||
|
"time-no-tz": "07:32:00",
|
||||||
|
"time-no-tz-nano": "00:32:00.999999",
|
||||||
}
|
}
|
||||||
if dateRegexp.FindString("1979-05-27T00:32:00-07:00") == "" {
|
|
||||||
t.Error("offset lexing")
|
for name, value := range cases {
|
||||||
|
if dateRegexp.FindString(value) == "" {
|
||||||
|
t.Error("failed date regexp test", name)
|
||||||
}
|
}
|
||||||
if dateRegexp.FindString("1979-05-27T00:32:00.999999-07:00") == "" {
|
}
|
||||||
t.Error("nano precision lexing")
|
if dateRegexp.FindString("1979-05-27 07:32:00Z") == "" {
|
||||||
|
t.Error("space delimiter lexing")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -320,6 +344,12 @@ func TestKeyEqualDate(t *testing.T) {
|
|||||||
{Position{1, 7}, tokenDate, "1979-05-27T00:32:00.999999-07:00"},
|
{Position{1, 7}, tokenDate, "1979-05-27T00:32:00.999999-07:00"},
|
||||||
{Position{1, 39}, tokenEOF, ""},
|
{Position{1, 39}, tokenEOF, ""},
|
||||||
})
|
})
|
||||||
|
testFlow(t, "foo = 1979-05-27 07:32:00Z", []token{
|
||||||
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
|
{Position{1, 5}, tokenEqual, "="},
|
||||||
|
{Position{1, 7}, tokenDate, "1979-05-27 07:32:00Z"},
|
||||||
|
{Position{1, 27}, tokenEOF, ""},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFloatEndingWithDot(t *testing.T) {
|
func TestFloatEndingWithDot(t *testing.T) {
|
||||||
@@ -633,6 +663,13 @@ func TestMultilineString(t *testing.T) {
|
|||||||
{Position{6, 9}, tokenEOF, ""},
|
{Position{6, 9}, tokenEOF, ""},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
testFlow(t, `foo = """hello world"""`, []token{
|
||||||
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
|
{Position{1, 5}, tokenEqual, "="},
|
||||||
|
{Position{1, 10}, tokenString, "hello\tworld"},
|
||||||
|
{Position{1, 24}, tokenEOF, ""},
|
||||||
|
})
|
||||||
|
|
||||||
testFlow(t, "key2 = \"\"\"\nThe quick brown \\\n\n\n fox jumps over \\\n the lazy dog.\"\"\"", []token{
|
testFlow(t, "key2 = \"\"\"\nThe quick brown \\\n\n\n fox jumps over \\\n the lazy dog.\"\"\"", []token{
|
||||||
{Position{1, 1}, tokenKey, "key2"},
|
{Position{1, 1}, tokenKey, "key2"},
|
||||||
{Position{1, 6}, tokenEqual, "="},
|
{Position{1, 6}, tokenEqual, "="},
|
||||||
@@ -670,6 +707,7 @@ func TestUnicodeString(t *testing.T) {
|
|||||||
{Position{1, 22}, tokenEOF, ""},
|
{Position{1, 22}, tokenEOF, ""},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEscapeInString(t *testing.T) {
|
func TestEscapeInString(t *testing.T) {
|
||||||
testFlow(t, `foo = "\b\f\/"`, []token{
|
testFlow(t, `foo = "\b\f\/"`, []token{
|
||||||
{Position{1, 1}, tokenKey, "foo"},
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
@@ -679,6 +717,15 @@ func TestEscapeInString(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTabInString(t *testing.T) {
|
||||||
|
testFlow(t, `foo = "hello world"`, []token{
|
||||||
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
|
{Position{1, 5}, tokenEqual, "="},
|
||||||
|
{Position{1, 8}, tokenString, "hello\tworld"},
|
||||||
|
{Position{1, 20}, tokenEOF, ""},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestKeyGroupArray(t *testing.T) {
|
func TestKeyGroupArray(t *testing.T) {
|
||||||
testFlow(t, "[[foo]]", []token{
|
testFlow(t, "[[foo]]", []token{
|
||||||
{Position{1, 1}, tokenDoubleLeftBracket, "[["},
|
{Position{1, 1}, tokenDoubleLeftBracket, "[["},
|
||||||
@@ -697,6 +744,15 @@ func TestQuotedKey(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestQuotedKeyTab(t *testing.T) {
|
||||||
|
testFlow(t, "\"num\tber\" = 123", []token{
|
||||||
|
{Position{1, 1}, tokenKey, "\"num\tber\""},
|
||||||
|
{Position{1, 11}, tokenEqual, "="},
|
||||||
|
{Position{1, 13}, tokenInteger, "123"},
|
||||||
|
{Position{1, 16}, tokenEOF, ""},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestKeyNewline(t *testing.T) {
|
func TestKeyNewline(t *testing.T) {
|
||||||
testFlow(t, "a\n= 4", []token{
|
testFlow(t, "a\n= 4", []token{
|
||||||
{Position{1, 1}, tokenError, "keys cannot contain new lines"},
|
{Position{1, 1}, tokenError, "keys cannot contain new lines"},
|
||||||
@@ -726,6 +782,178 @@ func TestLexUnknownRvalue(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLexInlineTableEmpty(t *testing.T) {
|
||||||
|
testFlow(t, `foo = {}`, []token{
|
||||||
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
|
{Position{1, 5}, tokenEqual, "="},
|
||||||
|
{Position{1, 7}, tokenLeftCurlyBrace, "{"},
|
||||||
|
{Position{1, 8}, tokenRightCurlyBrace, "}"},
|
||||||
|
{Position{1, 9}, tokenEOF, ""},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLexInlineTableBareKey(t *testing.T) {
|
||||||
|
testFlow(t, `foo = { bar = "baz" }`, []token{
|
||||||
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
|
{Position{1, 5}, tokenEqual, "="},
|
||||||
|
{Position{1, 7}, tokenLeftCurlyBrace, "{"},
|
||||||
|
{Position{1, 9}, tokenKey, "bar"},
|
||||||
|
{Position{1, 13}, tokenEqual, "="},
|
||||||
|
{Position{1, 16}, tokenString, "baz"},
|
||||||
|
{Position{1, 21}, tokenRightCurlyBrace, "}"},
|
||||||
|
{Position{1, 22}, tokenEOF, ""},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLexInlineTableBareKeyDash(t *testing.T) {
|
||||||
|
testFlow(t, `foo = { -bar = "baz" }`, []token{
|
||||||
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
|
{Position{1, 5}, tokenEqual, "="},
|
||||||
|
{Position{1, 7}, tokenLeftCurlyBrace, "{"},
|
||||||
|
{Position{1, 9}, tokenKey, "-bar"},
|
||||||
|
{Position{1, 14}, tokenEqual, "="},
|
||||||
|
{Position{1, 17}, tokenString, "baz"},
|
||||||
|
{Position{1, 22}, tokenRightCurlyBrace, "}"},
|
||||||
|
{Position{1, 23}, tokenEOF, ""},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLexInlineTableBareKeyInArray(t *testing.T) {
|
||||||
|
testFlow(t, `foo = [{ -bar_ = "baz" }]`, []token{
|
||||||
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
|
{Position{1, 5}, tokenEqual, "="},
|
||||||
|
{Position{1, 7}, tokenLeftBracket, "["},
|
||||||
|
{Position{1, 8}, tokenLeftCurlyBrace, "{"},
|
||||||
|
{Position{1, 10}, tokenKey, "-bar_"},
|
||||||
|
{Position{1, 16}, tokenEqual, "="},
|
||||||
|
{Position{1, 19}, tokenString, "baz"},
|
||||||
|
{Position{1, 24}, tokenRightCurlyBrace, "}"},
|
||||||
|
{Position{1, 25}, tokenRightBracket, "]"},
|
||||||
|
{Position{1, 26}, tokenEOF, ""},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLexInlineTableError1(t *testing.T) {
|
||||||
|
testFlow(t, `foo = { 123 = 0 ]`, []token{
|
||||||
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
|
{Position{1, 5}, tokenEqual, "="},
|
||||||
|
{Position{1, 7}, tokenLeftCurlyBrace, "{"},
|
||||||
|
{Position{1, 9}, tokenKey, "123"},
|
||||||
|
{Position{1, 13}, tokenEqual, "="},
|
||||||
|
{Position{1, 15}, tokenInteger, "0"},
|
||||||
|
{Position{1, 17}, tokenRightBracket, "]"},
|
||||||
|
{Position{1, 18}, tokenError, "cannot have ']' here"},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLexInlineTableError2(t *testing.T) {
|
||||||
|
testFlow(t, `foo = { 123 = 0 }}`, []token{
|
||||||
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
|
{Position{1, 5}, tokenEqual, "="},
|
||||||
|
{Position{1, 7}, tokenLeftCurlyBrace, "{"},
|
||||||
|
{Position{1, 9}, tokenKey, "123"},
|
||||||
|
{Position{1, 13}, tokenEqual, "="},
|
||||||
|
{Position{1, 15}, tokenInteger, "0"},
|
||||||
|
{Position{1, 17}, tokenRightCurlyBrace, "}"},
|
||||||
|
{Position{1, 18}, tokenRightCurlyBrace, "}"},
|
||||||
|
{Position{1, 19}, tokenError, "cannot have '}' here"},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLexInlineTableDottedKey1(t *testing.T) {
|
||||||
|
testFlow(t, `foo = { a = 0, 123.45abc = 0 }`, []token{
|
||||||
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
|
{Position{1, 5}, tokenEqual, "="},
|
||||||
|
{Position{1, 7}, tokenLeftCurlyBrace, "{"},
|
||||||
|
{Position{1, 9}, tokenKey, "a"},
|
||||||
|
{Position{1, 11}, tokenEqual, "="},
|
||||||
|
{Position{1, 13}, tokenInteger, "0"},
|
||||||
|
{Position{1, 14}, tokenComma, ","},
|
||||||
|
{Position{1, 16}, tokenKey, "123.45abc"},
|
||||||
|
{Position{1, 26}, tokenEqual, "="},
|
||||||
|
{Position{1, 28}, tokenInteger, "0"},
|
||||||
|
{Position{1, 30}, tokenRightCurlyBrace, "}"},
|
||||||
|
{Position{1, 31}, tokenEOF, ""},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLexInlineTableDottedKey2(t *testing.T) {
|
||||||
|
testFlow(t, `foo = { a = 0, '123'.'45abc' = 0 }`, []token{
|
||||||
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
|
{Position{1, 5}, tokenEqual, "="},
|
||||||
|
{Position{1, 7}, tokenLeftCurlyBrace, "{"},
|
||||||
|
{Position{1, 9}, tokenKey, "a"},
|
||||||
|
{Position{1, 11}, tokenEqual, "="},
|
||||||
|
{Position{1, 13}, tokenInteger, "0"},
|
||||||
|
{Position{1, 14}, tokenComma, ","},
|
||||||
|
{Position{1, 16}, tokenKey, "'123'.'45abc'"},
|
||||||
|
{Position{1, 30}, tokenEqual, "="},
|
||||||
|
{Position{1, 32}, tokenInteger, "0"},
|
||||||
|
{Position{1, 34}, tokenRightCurlyBrace, "}"},
|
||||||
|
{Position{1, 35}, tokenEOF, ""},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLexInlineTableDottedKey3(t *testing.T) {
|
||||||
|
testFlow(t, `foo = { a = 0, "123"."45ʎǝʞ" = 0 }`, []token{
|
||||||
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
|
{Position{1, 5}, tokenEqual, "="},
|
||||||
|
{Position{1, 7}, tokenLeftCurlyBrace, "{"},
|
||||||
|
{Position{1, 9}, tokenKey, "a"},
|
||||||
|
{Position{1, 11}, tokenEqual, "="},
|
||||||
|
{Position{1, 13}, tokenInteger, "0"},
|
||||||
|
{Position{1, 14}, tokenComma, ","},
|
||||||
|
{Position{1, 16}, tokenKey, `"123"."45ʎǝʞ"`},
|
||||||
|
{Position{1, 30}, tokenEqual, "="},
|
||||||
|
{Position{1, 32}, tokenInteger, "0"},
|
||||||
|
{Position{1, 34}, tokenRightCurlyBrace, "}"},
|
||||||
|
{Position{1, 35}, tokenEOF, ""},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLexInlineTableBareKeyWithComma(t *testing.T) {
|
||||||
|
testFlow(t, `foo = { -bar1 = "baz", -bar_ = "baz" }`, []token{
|
||||||
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
|
{Position{1, 5}, tokenEqual, "="},
|
||||||
|
{Position{1, 7}, tokenLeftCurlyBrace, "{"},
|
||||||
|
{Position{1, 9}, tokenKey, "-bar1"},
|
||||||
|
{Position{1, 15}, tokenEqual, "="},
|
||||||
|
{Position{1, 18}, tokenString, "baz"},
|
||||||
|
{Position{1, 22}, tokenComma, ","},
|
||||||
|
{Position{1, 24}, tokenKey, "-bar_"},
|
||||||
|
{Position{1, 30}, tokenEqual, "="},
|
||||||
|
{Position{1, 33}, tokenString, "baz"},
|
||||||
|
{Position{1, 38}, tokenRightCurlyBrace, "}"},
|
||||||
|
{Position{1, 39}, tokenEOF, ""},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLexInlineTableBareKeyUnderscore(t *testing.T) {
|
||||||
|
testFlow(t, `foo = { _bar = "baz" }`, []token{
|
||||||
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
|
{Position{1, 5}, tokenEqual, "="},
|
||||||
|
{Position{1, 7}, tokenLeftCurlyBrace, "{"},
|
||||||
|
{Position{1, 9}, tokenKey, "_bar"},
|
||||||
|
{Position{1, 14}, tokenEqual, "="},
|
||||||
|
{Position{1, 17}, tokenString, "baz"},
|
||||||
|
{Position{1, 22}, tokenRightCurlyBrace, "}"},
|
||||||
|
{Position{1, 23}, tokenEOF, ""},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLexInlineTableQuotedKey(t *testing.T) {
|
||||||
|
testFlow(t, `foo = { "bar" = "baz" }`, []token{
|
||||||
|
{Position{1, 1}, tokenKey, "foo"},
|
||||||
|
{Position{1, 5}, tokenEqual, "="},
|
||||||
|
{Position{1, 7}, tokenLeftCurlyBrace, "{"},
|
||||||
|
{Position{1, 9}, tokenKey, "\"bar\""},
|
||||||
|
{Position{1, 15}, tokenEqual, "="},
|
||||||
|
{Position{1, 18}, tokenString, "baz"},
|
||||||
|
{Position{1, 23}, tokenRightCurlyBrace, "}"},
|
||||||
|
{Position{1, 24}, tokenEOF, ""},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkLexer(b *testing.B) {
|
func BenchmarkLexer(b *testing.B) {
|
||||||
sample := `title = "Hugo: A Fast and Flexible Website Generator"
|
sample := `title = "Hugo: A Fast and Flexible Website Generator"
|
||||||
baseurl = "http://gohugo.io/"
|
baseurl = "http://gohugo.io/"
|
||||||
|
|||||||
+281
@@ -0,0 +1,281 @@
|
|||||||
|
// Implementation of TOML's local date/time.
|
||||||
|
// Copied over from https://github.com/googleapis/google-cloud-go/blob/master/civil/civil.go
|
||||||
|
// to avoid pulling all the Google dependencies.
|
||||||
|
//
|
||||||
|
// Copyright 2016 Google LLC
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
// Package civil implements types for civil time, a time-zone-independent
|
||||||
|
// representation of time that follows the rules of the proleptic
|
||||||
|
// Gregorian calendar with exactly 24-hour days, 60-minute hours, and 60-second
|
||||||
|
// minutes.
|
||||||
|
//
|
||||||
|
// Because they lack location information, these types do not represent unique
|
||||||
|
// moments or intervals of time. Use time.Time for that purpose.
|
||||||
|
package toml
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A LocalDate represents a date (year, month, day).
|
||||||
|
//
|
||||||
|
// This type does not include location information, and therefore does not
|
||||||
|
// describe a unique 24-hour timespan.
|
||||||
|
type LocalDate struct {
|
||||||
|
Year int // Year (e.g., 2014).
|
||||||
|
Month time.Month // Month of the year (January = 1, ...).
|
||||||
|
Day int // Day of the month, starting at 1.
|
||||||
|
}
|
||||||
|
|
||||||
|
// LocalDateOf returns the LocalDate in which a time occurs in that time's location.
|
||||||
|
func LocalDateOf(t time.Time) LocalDate {
|
||||||
|
var d LocalDate
|
||||||
|
d.Year, d.Month, d.Day = t.Date()
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseLocalDate parses a string in RFC3339 full-date format and returns the date value it represents.
|
||||||
|
func ParseLocalDate(s string) (LocalDate, error) {
|
||||||
|
t, err := time.Parse("2006-01-02", s)
|
||||||
|
if err != nil {
|
||||||
|
return LocalDate{}, err
|
||||||
|
}
|
||||||
|
return LocalDateOf(t), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the date in RFC3339 full-date format.
|
||||||
|
func (d LocalDate) String() string {
|
||||||
|
return fmt.Sprintf("%04d-%02d-%02d", d.Year, d.Month, d.Day)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValid reports whether the date is valid.
|
||||||
|
func (d LocalDate) IsValid() bool {
|
||||||
|
return LocalDateOf(d.In(time.UTC)) == d
|
||||||
|
}
|
||||||
|
|
||||||
|
// In returns the time corresponding to time 00:00:00 of the date in the location.
|
||||||
|
//
|
||||||
|
// In is always consistent with time.LocalDate, even when time.LocalDate returns a time
|
||||||
|
// on a different day. For example, if loc is America/Indiana/Vincennes, then both
|
||||||
|
// time.LocalDate(1955, time.May, 1, 0, 0, 0, 0, loc)
|
||||||
|
// and
|
||||||
|
// civil.LocalDate{Year: 1955, Month: time.May, Day: 1}.In(loc)
|
||||||
|
// return 23:00:00 on April 30, 1955.
|
||||||
|
//
|
||||||
|
// In panics if loc is nil.
|
||||||
|
func (d LocalDate) In(loc *time.Location) time.Time {
|
||||||
|
return time.Date(d.Year, d.Month, d.Day, 0, 0, 0, 0, loc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddDays returns the date that is n days in the future.
|
||||||
|
// n can also be negative to go into the past.
|
||||||
|
func (d LocalDate) AddDays(n int) LocalDate {
|
||||||
|
return LocalDateOf(d.In(time.UTC).AddDate(0, 0, n))
|
||||||
|
}
|
||||||
|
|
||||||
|
// DaysSince returns the signed number of days between the date and s, not including the end day.
|
||||||
|
// This is the inverse operation to AddDays.
|
||||||
|
func (d LocalDate) DaysSince(s LocalDate) (days int) {
|
||||||
|
// We convert to Unix time so we do not have to worry about leap seconds:
|
||||||
|
// Unix time increases by exactly 86400 seconds per day.
|
||||||
|
deltaUnix := d.In(time.UTC).Unix() - s.In(time.UTC).Unix()
|
||||||
|
return int(deltaUnix / 86400)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Before reports whether d1 occurs before d2.
|
||||||
|
func (d1 LocalDate) Before(d2 LocalDate) bool {
|
||||||
|
if d1.Year != d2.Year {
|
||||||
|
return d1.Year < d2.Year
|
||||||
|
}
|
||||||
|
if d1.Month != d2.Month {
|
||||||
|
return d1.Month < d2.Month
|
||||||
|
}
|
||||||
|
return d1.Day < d2.Day
|
||||||
|
}
|
||||||
|
|
||||||
|
// After reports whether d1 occurs after d2.
|
||||||
|
func (d1 LocalDate) After(d2 LocalDate) bool {
|
||||||
|
return d2.Before(d1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalText implements the encoding.TextMarshaler interface.
|
||||||
|
// The output is the result of d.String().
|
||||||
|
func (d LocalDate) MarshalText() ([]byte, error) {
|
||||||
|
return []byte(d.String()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalText implements the encoding.TextUnmarshaler interface.
|
||||||
|
// The date is expected to be a string in a format accepted by ParseLocalDate.
|
||||||
|
func (d *LocalDate) UnmarshalText(data []byte) error {
|
||||||
|
var err error
|
||||||
|
*d, err = ParseLocalDate(string(data))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// A LocalTime represents a time with nanosecond precision.
|
||||||
|
//
|
||||||
|
// This type does not include location information, and therefore does not
|
||||||
|
// describe a unique moment in time.
|
||||||
|
//
|
||||||
|
// This type exists to represent the TIME type in storage-based APIs like BigQuery.
|
||||||
|
// Most operations on Times are unlikely to be meaningful. Prefer the LocalDateTime type.
|
||||||
|
type LocalTime struct {
|
||||||
|
Hour int // The hour of the day in 24-hour format; range [0-23]
|
||||||
|
Minute int // The minute of the hour; range [0-59]
|
||||||
|
Second int // The second of the minute; range [0-59]
|
||||||
|
Nanosecond int // The nanosecond of the second; range [0-999999999]
|
||||||
|
}
|
||||||
|
|
||||||
|
// LocalTimeOf returns the LocalTime representing the time of day in which a time occurs
|
||||||
|
// in that time's location. It ignores the date.
|
||||||
|
func LocalTimeOf(t time.Time) LocalTime {
|
||||||
|
var tm LocalTime
|
||||||
|
tm.Hour, tm.Minute, tm.Second = t.Clock()
|
||||||
|
tm.Nanosecond = t.Nanosecond()
|
||||||
|
return tm
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseLocalTime parses a string and returns the time value it represents.
|
||||||
|
// ParseLocalTime accepts an extended form of the RFC3339 partial-time format. After
|
||||||
|
// the HH:MM:SS part of the string, an optional fractional part may appear,
|
||||||
|
// consisting of a decimal point followed by one to nine decimal digits.
|
||||||
|
// (RFC3339 admits only one digit after the decimal point).
|
||||||
|
func ParseLocalTime(s string) (LocalTime, error) {
|
||||||
|
t, err := time.Parse("15:04:05.999999999", s)
|
||||||
|
if err != nil {
|
||||||
|
return LocalTime{}, err
|
||||||
|
}
|
||||||
|
return LocalTimeOf(t), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the date in the format described in ParseLocalTime. If Nanoseconds
|
||||||
|
// is zero, no fractional part will be generated. Otherwise, the result will
|
||||||
|
// end with a fractional part consisting of a decimal point and nine digits.
|
||||||
|
func (t LocalTime) String() string {
|
||||||
|
s := fmt.Sprintf("%02d:%02d:%02d", t.Hour, t.Minute, t.Second)
|
||||||
|
if t.Nanosecond == 0 {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return s + fmt.Sprintf(".%09d", t.Nanosecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValid reports whether the time is valid.
|
||||||
|
func (t LocalTime) IsValid() bool {
|
||||||
|
// Construct a non-zero time.
|
||||||
|
tm := time.Date(2, 2, 2, t.Hour, t.Minute, t.Second, t.Nanosecond, time.UTC)
|
||||||
|
return LocalTimeOf(tm) == t
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalText implements the encoding.TextMarshaler interface.
|
||||||
|
// The output is the result of t.String().
|
||||||
|
func (t LocalTime) MarshalText() ([]byte, error) {
|
||||||
|
return []byte(t.String()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalText implements the encoding.TextUnmarshaler interface.
|
||||||
|
// The time is expected to be a string in a format accepted by ParseLocalTime.
|
||||||
|
func (t *LocalTime) UnmarshalText(data []byte) error {
|
||||||
|
var err error
|
||||||
|
*t, err = ParseLocalTime(string(data))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// A LocalDateTime represents a date and time.
|
||||||
|
//
|
||||||
|
// This type does not include location information, and therefore does not
|
||||||
|
// describe a unique moment in time.
|
||||||
|
type LocalDateTime struct {
|
||||||
|
Date LocalDate
|
||||||
|
Time LocalTime
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: We deliberately do not embed LocalDate into LocalDateTime, to avoid promoting AddDays and Sub.
|
||||||
|
|
||||||
|
// LocalDateTimeOf returns the LocalDateTime in which a time occurs in that time's location.
|
||||||
|
func LocalDateTimeOf(t time.Time) LocalDateTime {
|
||||||
|
return LocalDateTime{
|
||||||
|
Date: LocalDateOf(t),
|
||||||
|
Time: LocalTimeOf(t),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseLocalDateTime parses a string and returns the LocalDateTime it represents.
|
||||||
|
// ParseLocalDateTime accepts a variant of the RFC3339 date-time format that omits
|
||||||
|
// the time offset but includes an optional fractional time, as described in
|
||||||
|
// ParseLocalTime. Informally, the accepted format is
|
||||||
|
// YYYY-MM-DDTHH:MM:SS[.FFFFFFFFF]
|
||||||
|
// where the 'T' may be a lower-case 't'.
|
||||||
|
func ParseLocalDateTime(s string) (LocalDateTime, error) {
|
||||||
|
t, err := time.Parse("2006-01-02T15:04:05.999999999", s)
|
||||||
|
if err != nil {
|
||||||
|
t, err = time.Parse("2006-01-02t15:04:05.999999999", s)
|
||||||
|
if err != nil {
|
||||||
|
return LocalDateTime{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return LocalDateTimeOf(t), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the date in the format described in ParseLocalDate.
|
||||||
|
func (dt LocalDateTime) String() string {
|
||||||
|
return dt.Date.String() + "T" + dt.Time.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValid reports whether the datetime is valid.
|
||||||
|
func (dt LocalDateTime) IsValid() bool {
|
||||||
|
return dt.Date.IsValid() && dt.Time.IsValid()
|
||||||
|
}
|
||||||
|
|
||||||
|
// In returns the time corresponding to the LocalDateTime in the given location.
|
||||||
|
//
|
||||||
|
// If the time is missing or ambigous at the location, In returns the same
|
||||||
|
// result as time.LocalDate. For example, if loc is America/Indiana/Vincennes, then
|
||||||
|
// both
|
||||||
|
// time.LocalDate(1955, time.May, 1, 0, 30, 0, 0, loc)
|
||||||
|
// and
|
||||||
|
// civil.LocalDateTime{
|
||||||
|
// civil.LocalDate{Year: 1955, Month: time.May, Day: 1}},
|
||||||
|
// civil.LocalTime{Minute: 30}}.In(loc)
|
||||||
|
// return 23:30:00 on April 30, 1955.
|
||||||
|
//
|
||||||
|
// In panics if loc is nil.
|
||||||
|
func (dt LocalDateTime) In(loc *time.Location) time.Time {
|
||||||
|
return time.Date(dt.Date.Year, dt.Date.Month, dt.Date.Day, dt.Time.Hour, dt.Time.Minute, dt.Time.Second, dt.Time.Nanosecond, loc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Before reports whether dt1 occurs before dt2.
|
||||||
|
func (dt1 LocalDateTime) Before(dt2 LocalDateTime) bool {
|
||||||
|
return dt1.In(time.UTC).Before(dt2.In(time.UTC))
|
||||||
|
}
|
||||||
|
|
||||||
|
// After reports whether dt1 occurs after dt2.
|
||||||
|
func (dt1 LocalDateTime) After(dt2 LocalDateTime) bool {
|
||||||
|
return dt2.Before(dt1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalText implements the encoding.TextMarshaler interface.
|
||||||
|
// The output is the result of dt.String().
|
||||||
|
func (dt LocalDateTime) MarshalText() ([]byte, error) {
|
||||||
|
return []byte(dt.String()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalText implements the encoding.TextUnmarshaler interface.
|
||||||
|
// The datetime is expected to be a string in a format accepted by ParseLocalDateTime
|
||||||
|
func (dt *LocalDateTime) UnmarshalText(data []byte) error {
|
||||||
|
var err error
|
||||||
|
*dt, err = ParseLocalDateTime(string(data))
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,446 @@
|
|||||||
|
// Copyright 2016 Google LLC
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package toml
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func cmpEqual(x, y interface{}) bool {
|
||||||
|
return reflect.DeepEqual(x, y)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDates(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
date LocalDate
|
||||||
|
loc *time.Location
|
||||||
|
wantStr string
|
||||||
|
wantTime time.Time
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
date: LocalDate{2014, 7, 29},
|
||||||
|
loc: time.Local,
|
||||||
|
wantStr: "2014-07-29",
|
||||||
|
wantTime: time.Date(2014, time.July, 29, 0, 0, 0, 0, time.Local),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: LocalDateOf(time.Date(2014, 8, 20, 15, 8, 43, 1, time.Local)),
|
||||||
|
loc: time.UTC,
|
||||||
|
wantStr: "2014-08-20",
|
||||||
|
wantTime: time.Date(2014, 8, 20, 0, 0, 0, 0, time.UTC),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: LocalDateOf(time.Date(999, time.January, 26, 0, 0, 0, 0, time.Local)),
|
||||||
|
loc: time.UTC,
|
||||||
|
wantStr: "0999-01-26",
|
||||||
|
wantTime: time.Date(999, 1, 26, 0, 0, 0, 0, time.UTC),
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
if got := test.date.String(); got != test.wantStr {
|
||||||
|
t.Errorf("%#v.String() = %q, want %q", test.date, got, test.wantStr)
|
||||||
|
}
|
||||||
|
if got := test.date.In(test.loc); !got.Equal(test.wantTime) {
|
||||||
|
t.Errorf("%#v.In(%v) = %v, want %v", test.date, test.loc, got, test.wantTime)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDateIsValid(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
date LocalDate
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{LocalDate{2014, 7, 29}, true},
|
||||||
|
{LocalDate{2000, 2, 29}, true},
|
||||||
|
{LocalDate{10000, 12, 31}, true},
|
||||||
|
{LocalDate{1, 1, 1}, true},
|
||||||
|
{LocalDate{0, 1, 1}, true}, // year zero is OK
|
||||||
|
{LocalDate{-1, 1, 1}, true}, // negative year is OK
|
||||||
|
{LocalDate{1, 0, 1}, false},
|
||||||
|
{LocalDate{1, 1, 0}, false},
|
||||||
|
{LocalDate{2016, 1, 32}, false},
|
||||||
|
{LocalDate{2016, 13, 1}, false},
|
||||||
|
{LocalDate{1, -1, 1}, false},
|
||||||
|
{LocalDate{1, 1, -1}, false},
|
||||||
|
} {
|
||||||
|
got := test.date.IsValid()
|
||||||
|
if got != test.want {
|
||||||
|
t.Errorf("%#v: got %t, want %t", test.date, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseDate(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
str string
|
||||||
|
want LocalDate // if empty, expect an error
|
||||||
|
}{
|
||||||
|
{"2016-01-02", LocalDate{2016, 1, 2}},
|
||||||
|
{"2016-12-31", LocalDate{2016, 12, 31}},
|
||||||
|
{"0003-02-04", LocalDate{3, 2, 4}},
|
||||||
|
{"999-01-26", LocalDate{}},
|
||||||
|
{"", LocalDate{}},
|
||||||
|
{"2016-01-02x", LocalDate{}},
|
||||||
|
} {
|
||||||
|
got, err := ParseLocalDate(test.str)
|
||||||
|
if got != test.want {
|
||||||
|
t.Errorf("ParseLocalDate(%q) = %+v, want %+v", test.str, got, test.want)
|
||||||
|
}
|
||||||
|
if err != nil && test.want != (LocalDate{}) {
|
||||||
|
t.Errorf("Unexpected error %v from ParseLocalDate(%q)", err, test.str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDateArithmetic(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
desc string
|
||||||
|
start LocalDate
|
||||||
|
end LocalDate
|
||||||
|
days int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "zero days noop",
|
||||||
|
start: LocalDate{2014, 5, 9},
|
||||||
|
end: LocalDate{2014, 5, 9},
|
||||||
|
days: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "crossing a year boundary",
|
||||||
|
start: LocalDate{2014, 12, 31},
|
||||||
|
end: LocalDate{2015, 1, 1},
|
||||||
|
days: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "negative number of days",
|
||||||
|
start: LocalDate{2015, 1, 1},
|
||||||
|
end: LocalDate{2014, 12, 31},
|
||||||
|
days: -1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "full leap year",
|
||||||
|
start: LocalDate{2004, 1, 1},
|
||||||
|
end: LocalDate{2005, 1, 1},
|
||||||
|
days: 366,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "full non-leap year",
|
||||||
|
start: LocalDate{2001, 1, 1},
|
||||||
|
end: LocalDate{2002, 1, 1},
|
||||||
|
days: 365,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "crossing a leap second",
|
||||||
|
start: LocalDate{1972, 6, 30},
|
||||||
|
end: LocalDate{1972, 7, 1},
|
||||||
|
days: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "dates before the unix epoch",
|
||||||
|
start: LocalDate{101, 1, 1},
|
||||||
|
end: LocalDate{102, 1, 1},
|
||||||
|
days: 365,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
if got := test.start.AddDays(test.days); got != test.end {
|
||||||
|
t.Errorf("[%s] %#v.AddDays(%v) = %#v, want %#v", test.desc, test.start, test.days, got, test.end)
|
||||||
|
}
|
||||||
|
if got := test.end.DaysSince(test.start); got != test.days {
|
||||||
|
t.Errorf("[%s] %#v.Sub(%#v) = %v, want %v", test.desc, test.end, test.start, got, test.days)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDateBefore(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
d1, d2 LocalDate
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{LocalDate{2016, 12, 31}, LocalDate{2017, 1, 1}, true},
|
||||||
|
{LocalDate{2016, 1, 1}, LocalDate{2016, 1, 1}, false},
|
||||||
|
{LocalDate{2016, 12, 30}, LocalDate{2016, 12, 31}, true},
|
||||||
|
{LocalDate{2016, 1, 30}, LocalDate{2016, 12, 31}, true},
|
||||||
|
} {
|
||||||
|
if got := test.d1.Before(test.d2); got != test.want {
|
||||||
|
t.Errorf("%v.Before(%v): got %t, want %t", test.d1, test.d2, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDateAfter(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
d1, d2 LocalDate
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{LocalDate{2016, 12, 31}, LocalDate{2017, 1, 1}, false},
|
||||||
|
{LocalDate{2016, 1, 1}, LocalDate{2016, 1, 1}, false},
|
||||||
|
{LocalDate{2016, 12, 30}, LocalDate{2016, 12, 31}, false},
|
||||||
|
} {
|
||||||
|
if got := test.d1.After(test.d2); got != test.want {
|
||||||
|
t.Errorf("%v.After(%v): got %t, want %t", test.d1, test.d2, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTimeToString(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
str string
|
||||||
|
time LocalTime
|
||||||
|
roundTrip bool // ParseLocalTime(str).String() == str?
|
||||||
|
}{
|
||||||
|
{"13:26:33", LocalTime{13, 26, 33, 0}, true},
|
||||||
|
{"01:02:03.000023456", LocalTime{1, 2, 3, 23456}, true},
|
||||||
|
{"00:00:00.000000001", LocalTime{0, 0, 0, 1}, true},
|
||||||
|
{"13:26:03.1", LocalTime{13, 26, 3, 100000000}, false},
|
||||||
|
{"13:26:33.0000003", LocalTime{13, 26, 33, 300}, false},
|
||||||
|
} {
|
||||||
|
gotTime, err := ParseLocalTime(test.str)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("ParseLocalTime(%q): got error: %v", test.str, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if gotTime != test.time {
|
||||||
|
t.Errorf("ParseLocalTime(%q) = %+v, want %+v", test.str, gotTime, test.time)
|
||||||
|
}
|
||||||
|
if test.roundTrip {
|
||||||
|
gotStr := test.time.String()
|
||||||
|
if gotStr != test.str {
|
||||||
|
t.Errorf("%#v.String() = %q, want %q", test.time, gotStr, test.str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTimeOf(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
time time.Time
|
||||||
|
want LocalTime
|
||||||
|
}{
|
||||||
|
{time.Date(2014, 8, 20, 15, 8, 43, 1, time.Local), LocalTime{15, 8, 43, 1}},
|
||||||
|
{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC), LocalTime{0, 0, 0, 0}},
|
||||||
|
} {
|
||||||
|
if got := LocalTimeOf(test.time); got != test.want {
|
||||||
|
t.Errorf("LocalTimeOf(%v) = %+v, want %+v", test.time, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTimeIsValid(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
time LocalTime
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{LocalTime{0, 0, 0, 0}, true},
|
||||||
|
{LocalTime{23, 0, 0, 0}, true},
|
||||||
|
{LocalTime{23, 59, 59, 999999999}, true},
|
||||||
|
{LocalTime{24, 59, 59, 999999999}, false},
|
||||||
|
{LocalTime{23, 60, 59, 999999999}, false},
|
||||||
|
{LocalTime{23, 59, 60, 999999999}, false},
|
||||||
|
{LocalTime{23, 59, 59, 1000000000}, false},
|
||||||
|
{LocalTime{-1, 0, 0, 0}, false},
|
||||||
|
{LocalTime{0, -1, 0, 0}, false},
|
||||||
|
{LocalTime{0, 0, -1, 0}, false},
|
||||||
|
{LocalTime{0, 0, 0, -1}, false},
|
||||||
|
} {
|
||||||
|
got := test.time.IsValid()
|
||||||
|
if got != test.want {
|
||||||
|
t.Errorf("%#v: got %t, want %t", test.time, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDateTimeToString(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
str string
|
||||||
|
dateTime LocalDateTime
|
||||||
|
roundTrip bool // ParseLocalDateTime(str).String() == str?
|
||||||
|
}{
|
||||||
|
{"2016-03-22T13:26:33", LocalDateTime{LocalDate{2016, 03, 22}, LocalTime{13, 26, 33, 0}}, true},
|
||||||
|
{"2016-03-22T13:26:33.000000600", LocalDateTime{LocalDate{2016, 03, 22}, LocalTime{13, 26, 33, 600}}, true},
|
||||||
|
{"2016-03-22t13:26:33", LocalDateTime{LocalDate{2016, 03, 22}, LocalTime{13, 26, 33, 0}}, false},
|
||||||
|
} {
|
||||||
|
gotDateTime, err := ParseLocalDateTime(test.str)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("ParseLocalDateTime(%q): got error: %v", test.str, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if gotDateTime != test.dateTime {
|
||||||
|
t.Errorf("ParseLocalDateTime(%q) = %+v, want %+v", test.str, gotDateTime, test.dateTime)
|
||||||
|
}
|
||||||
|
if test.roundTrip {
|
||||||
|
gotStr := test.dateTime.String()
|
||||||
|
if gotStr != test.str {
|
||||||
|
t.Errorf("%#v.String() = %q, want %q", test.dateTime, gotStr, test.str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseDateTimeErrors(t *testing.T) {
|
||||||
|
for _, str := range []string{
|
||||||
|
"",
|
||||||
|
"2016-03-22", // just a date
|
||||||
|
"13:26:33", // just a time
|
||||||
|
"2016-03-22 13:26:33", // wrong separating character
|
||||||
|
"2016-03-22T13:26:33x", // extra at end
|
||||||
|
} {
|
||||||
|
if _, err := ParseLocalDateTime(str); err == nil {
|
||||||
|
t.Errorf("ParseLocalDateTime(%q) succeeded, want error", str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDateTimeOf(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
time time.Time
|
||||||
|
want LocalDateTime
|
||||||
|
}{
|
||||||
|
{time.Date(2014, 8, 20, 15, 8, 43, 1, time.Local),
|
||||||
|
LocalDateTime{LocalDate{2014, 8, 20}, LocalTime{15, 8, 43, 1}}},
|
||||||
|
{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
LocalDateTime{LocalDate{1, 1, 1}, LocalTime{0, 0, 0, 0}}},
|
||||||
|
} {
|
||||||
|
if got := LocalDateTimeOf(test.time); got != test.want {
|
||||||
|
t.Errorf("LocalDateTimeOf(%v) = %+v, want %+v", test.time, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDateTimeIsValid(t *testing.T) {
|
||||||
|
// No need to be exhaustive here; it's just LocalDate.IsValid && LocalTime.IsValid.
|
||||||
|
for _, test := range []struct {
|
||||||
|
dt LocalDateTime
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{LocalDateTime{LocalDate{2016, 3, 20}, LocalTime{0, 0, 0, 0}}, true},
|
||||||
|
{LocalDateTime{LocalDate{2016, -3, 20}, LocalTime{0, 0, 0, 0}}, false},
|
||||||
|
{LocalDateTime{LocalDate{2016, 3, 20}, LocalTime{24, 0, 0, 0}}, false},
|
||||||
|
} {
|
||||||
|
got := test.dt.IsValid()
|
||||||
|
if got != test.want {
|
||||||
|
t.Errorf("%#v: got %t, want %t", test.dt, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDateTimeIn(t *testing.T) {
|
||||||
|
dt := LocalDateTime{LocalDate{2016, 1, 2}, LocalTime{3, 4, 5, 6}}
|
||||||
|
got := dt.In(time.UTC)
|
||||||
|
want := time.Date(2016, 1, 2, 3, 4, 5, 6, time.UTC)
|
||||||
|
if !got.Equal(want) {
|
||||||
|
t.Errorf("got %v, want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDateTimeBefore(t *testing.T) {
|
||||||
|
d1 := LocalDate{2016, 12, 31}
|
||||||
|
d2 := LocalDate{2017, 1, 1}
|
||||||
|
t1 := LocalTime{5, 6, 7, 8}
|
||||||
|
t2 := LocalTime{5, 6, 7, 9}
|
||||||
|
for _, test := range []struct {
|
||||||
|
dt1, dt2 LocalDateTime
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{LocalDateTime{d1, t1}, LocalDateTime{d2, t1}, true},
|
||||||
|
{LocalDateTime{d1, t1}, LocalDateTime{d1, t2}, true},
|
||||||
|
{LocalDateTime{d2, t1}, LocalDateTime{d1, t1}, false},
|
||||||
|
{LocalDateTime{d2, t1}, LocalDateTime{d2, t1}, false},
|
||||||
|
} {
|
||||||
|
if got := test.dt1.Before(test.dt2); got != test.want {
|
||||||
|
t.Errorf("%v.Before(%v): got %t, want %t", test.dt1, test.dt2, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDateTimeAfter(t *testing.T) {
|
||||||
|
d1 := LocalDate{2016, 12, 31}
|
||||||
|
d2 := LocalDate{2017, 1, 1}
|
||||||
|
t1 := LocalTime{5, 6, 7, 8}
|
||||||
|
t2 := LocalTime{5, 6, 7, 9}
|
||||||
|
for _, test := range []struct {
|
||||||
|
dt1, dt2 LocalDateTime
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{LocalDateTime{d1, t1}, LocalDateTime{d2, t1}, false},
|
||||||
|
{LocalDateTime{d1, t1}, LocalDateTime{d1, t2}, false},
|
||||||
|
{LocalDateTime{d2, t1}, LocalDateTime{d1, t1}, true},
|
||||||
|
{LocalDateTime{d2, t1}, LocalDateTime{d2, t1}, false},
|
||||||
|
} {
|
||||||
|
if got := test.dt1.After(test.dt2); got != test.want {
|
||||||
|
t.Errorf("%v.After(%v): got %t, want %t", test.dt1, test.dt2, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarshalJSON(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
value interface{}
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{LocalDate{1987, 4, 15}, `"1987-04-15"`},
|
||||||
|
{LocalTime{18, 54, 2, 0}, `"18:54:02"`},
|
||||||
|
{LocalDateTime{LocalDate{1987, 4, 15}, LocalTime{18, 54, 2, 0}}, `"1987-04-15T18:54:02"`},
|
||||||
|
} {
|
||||||
|
bgot, err := json.Marshal(test.value)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got := string(bgot); got != test.want {
|
||||||
|
t.Errorf("%#v: got %s, want %s", test.value, got, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalJSON(t *testing.T) {
|
||||||
|
var d LocalDate
|
||||||
|
var tm LocalTime
|
||||||
|
var dt LocalDateTime
|
||||||
|
for _, test := range []struct {
|
||||||
|
data string
|
||||||
|
ptr interface{}
|
||||||
|
want interface{}
|
||||||
|
}{
|
||||||
|
{`"1987-04-15"`, &d, &LocalDate{1987, 4, 15}},
|
||||||
|
{`"1987-04-\u0031\u0035"`, &d, &LocalDate{1987, 4, 15}},
|
||||||
|
{`"18:54:02"`, &tm, &LocalTime{18, 54, 2, 0}},
|
||||||
|
{`"1987-04-15T18:54:02"`, &dt, &LocalDateTime{LocalDate{1987, 4, 15}, LocalTime{18, 54, 2, 0}}},
|
||||||
|
} {
|
||||||
|
if err := json.Unmarshal([]byte(test.data), test.ptr); err != nil {
|
||||||
|
t.Fatalf("%s: %v", test.data, err)
|
||||||
|
}
|
||||||
|
if !cmpEqual(test.ptr, test.want) {
|
||||||
|
t.Errorf("%s: got %#v, want %#v", test.data, test.ptr, test.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, bad := range []string{"", `""`, `"bad"`, `"1987-04-15x"`,
|
||||||
|
`19870415`, // a JSON number
|
||||||
|
`11987-04-15x`, // not a JSON string
|
||||||
|
|
||||||
|
} {
|
||||||
|
if json.Unmarshal([]byte(bad), &d) == nil {
|
||||||
|
t.Errorf("%q, LocalDate: got nil, want error", bad)
|
||||||
|
}
|
||||||
|
if json.Unmarshal([]byte(bad), &tm) == nil {
|
||||||
|
t.Errorf("%q, LocalTime: got nil, want error", bad)
|
||||||
|
}
|
||||||
|
if json.Unmarshal([]byte(bad), &dt) == nil {
|
||||||
|
t.Errorf("%q, LocalDateTime: got nil, want error", bad)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+550
-84
@@ -2,6 +2,7 @@ package toml
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -22,6 +23,7 @@ const (
|
|||||||
|
|
||||||
type tomlOpts struct {
|
type tomlOpts struct {
|
||||||
name string
|
name string
|
||||||
|
nameFromTag bool
|
||||||
comment string
|
comment string
|
||||||
commented bool
|
commented bool
|
||||||
multiline bool
|
multiline bool
|
||||||
@@ -68,6 +70,13 @@ const (
|
|||||||
|
|
||||||
var timeType = reflect.TypeOf(time.Time{})
|
var timeType = reflect.TypeOf(time.Time{})
|
||||||
var marshalerType = reflect.TypeOf(new(Marshaler)).Elem()
|
var marshalerType = reflect.TypeOf(new(Marshaler)).Elem()
|
||||||
|
var unmarshalerType = reflect.TypeOf(new(Unmarshaler)).Elem()
|
||||||
|
var textMarshalerType = reflect.TypeOf(new(encoding.TextMarshaler)).Elem()
|
||||||
|
var textUnmarshalerType = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
|
||||||
|
var localDateType = reflect.TypeOf(LocalDate{})
|
||||||
|
var localTimeType = reflect.TypeOf(LocalTime{})
|
||||||
|
var localDateTimeType = reflect.TypeOf(LocalDateTime{})
|
||||||
|
var mapStringInterfaceType = reflect.TypeOf(map[string]interface{}{})
|
||||||
|
|
||||||
// Check if the given marshal type maps to a Tree primitive
|
// Check if the given marshal type maps to a Tree primitive
|
||||||
func isPrimitive(mtype reflect.Type) bool {
|
func isPrimitive(mtype reflect.Type) bool {
|
||||||
@@ -85,29 +94,59 @@ func isPrimitive(mtype reflect.Type) bool {
|
|||||||
case reflect.String:
|
case reflect.String:
|
||||||
return true
|
return true
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
return mtype == timeType || isCustomMarshaler(mtype)
|
return isTimeType(mtype)
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the given marshal type maps to a Tree slice
|
func isTimeType(mtype reflect.Type) bool {
|
||||||
func isTreeSlice(mtype reflect.Type) bool {
|
return mtype == timeType || mtype == localDateType || mtype == localDateTimeType || mtype == localTimeType
|
||||||
switch mtype.Kind() {
|
|
||||||
case reflect.Slice:
|
|
||||||
return !isOtherSlice(mtype)
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the given marshal type maps to a non-Tree slice
|
// Check if the given marshal type maps to a Tree slice or array
|
||||||
func isOtherSlice(mtype reflect.Type) bool {
|
func isTreeSequence(mtype reflect.Type) bool {
|
||||||
switch mtype.Kind() {
|
switch mtype.Kind() {
|
||||||
case reflect.Ptr:
|
case reflect.Ptr:
|
||||||
return isOtherSlice(mtype.Elem())
|
return isTreeSequence(mtype.Elem())
|
||||||
case reflect.Slice:
|
case reflect.Slice, reflect.Array:
|
||||||
return isPrimitive(mtype.Elem()) || isOtherSlice(mtype.Elem())
|
return isTree(mtype.Elem())
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the given marshal type maps to a slice or array of a custom marshaler type
|
||||||
|
func isCustomMarshalerSequence(mtype reflect.Type) bool {
|
||||||
|
switch mtype.Kind() {
|
||||||
|
case reflect.Ptr:
|
||||||
|
return isCustomMarshalerSequence(mtype.Elem())
|
||||||
|
case reflect.Slice, reflect.Array:
|
||||||
|
return isCustomMarshaler(mtype.Elem()) || isCustomMarshaler(reflect.New(mtype.Elem()).Type())
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the given marshal type maps to a slice or array of a text marshaler type
|
||||||
|
func isTextMarshalerSequence(mtype reflect.Type) bool {
|
||||||
|
switch mtype.Kind() {
|
||||||
|
case reflect.Ptr:
|
||||||
|
return isTextMarshalerSequence(mtype.Elem())
|
||||||
|
case reflect.Slice, reflect.Array:
|
||||||
|
return isTextMarshaler(mtype.Elem()) || isTextMarshaler(reflect.New(mtype.Elem()).Type())
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the given marshal type maps to a non-Tree slice or array
|
||||||
|
func isOtherSequence(mtype reflect.Type) bool {
|
||||||
|
switch mtype.Kind() {
|
||||||
|
case reflect.Ptr:
|
||||||
|
return isOtherSequence(mtype.Elem())
|
||||||
|
case reflect.Slice, reflect.Array:
|
||||||
|
return !isTreeSequence(mtype)
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -116,6 +155,8 @@ func isOtherSlice(mtype reflect.Type) bool {
|
|||||||
// Check if the given marshal type maps to a Tree
|
// Check if the given marshal type maps to a Tree
|
||||||
func isTree(mtype reflect.Type) bool {
|
func isTree(mtype reflect.Type) bool {
|
||||||
switch mtype.Kind() {
|
switch mtype.Kind() {
|
||||||
|
case reflect.Ptr:
|
||||||
|
return isTree(mtype.Elem())
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
return true
|
return true
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
@@ -133,12 +174,42 @@ func callCustomMarshaler(mval reflect.Value) ([]byte, error) {
|
|||||||
return mval.Interface().(Marshaler).MarshalTOML()
|
return mval.Interface().(Marshaler).MarshalTOML()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isTextMarshaler(mtype reflect.Type) bool {
|
||||||
|
return mtype.Implements(textMarshalerType) && !isTimeType(mtype)
|
||||||
|
}
|
||||||
|
|
||||||
|
func callTextMarshaler(mval reflect.Value) ([]byte, error) {
|
||||||
|
return mval.Interface().(encoding.TextMarshaler).MarshalText()
|
||||||
|
}
|
||||||
|
|
||||||
|
func isCustomUnmarshaler(mtype reflect.Type) bool {
|
||||||
|
return mtype.Implements(unmarshalerType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func callCustomUnmarshaler(mval reflect.Value, tval interface{}) error {
|
||||||
|
return mval.Interface().(Unmarshaler).UnmarshalTOML(tval)
|
||||||
|
}
|
||||||
|
|
||||||
|
func isTextUnmarshaler(mtype reflect.Type) bool {
|
||||||
|
return mtype.Implements(textUnmarshalerType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func callTextUnmarshaler(mval reflect.Value, text []byte) error {
|
||||||
|
return mval.Interface().(encoding.TextUnmarshaler).UnmarshalText(text)
|
||||||
|
}
|
||||||
|
|
||||||
// Marshaler is the interface implemented by types that
|
// Marshaler is the interface implemented by types that
|
||||||
// can marshal themselves into valid TOML.
|
// can marshal themselves into valid TOML.
|
||||||
type Marshaler interface {
|
type Marshaler interface {
|
||||||
MarshalTOML() ([]byte, error)
|
MarshalTOML() ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unmarshaler is the interface implemented by types that
|
||||||
|
// can unmarshal a TOML description of themselves.
|
||||||
|
type Unmarshaler interface {
|
||||||
|
UnmarshalTOML(interface{}) error
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Marshal returns the TOML encoding of v. Behavior is similar to the Go json
|
Marshal returns the TOML encoding of v. Behavior is similar to the Go json
|
||||||
encoder, except that there is no concept of a Marshaler interface or MarshalTOML
|
encoder, except that there is no concept of a Marshaler interface or MarshalTOML
|
||||||
@@ -170,7 +241,7 @@ Tree primitive types and corresponding marshal types:
|
|||||||
float64 float32, float64, pointers to same
|
float64 float32, float64, pointers to same
|
||||||
string string, pointers to same
|
string string, pointers to same
|
||||||
bool bool, pointers to same
|
bool bool, pointers to same
|
||||||
time.Time time.Time{}, pointers to same
|
time.LocalTime time.LocalTime{}, pointers to same
|
||||||
|
|
||||||
For additional flexibility, use the Encoder API.
|
For additional flexibility, use the Encoder API.
|
||||||
*/
|
*/
|
||||||
@@ -186,6 +257,8 @@ type Encoder struct {
|
|||||||
line int
|
line int
|
||||||
col int
|
col int
|
||||||
order marshalOrder
|
order marshalOrder
|
||||||
|
promoteAnon bool
|
||||||
|
indentation string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewEncoder returns a new encoder that writes to w.
|
// NewEncoder returns a new encoder that writes to w.
|
||||||
@@ -197,6 +270,7 @@ func NewEncoder(w io.Writer) *Encoder {
|
|||||||
line: 0,
|
line: 0,
|
||||||
col: 1,
|
col: 1,
|
||||||
order: OrderAlphabetical,
|
order: OrderAlphabetical,
|
||||||
|
indentation: " ",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,6 +322,12 @@ func (e *Encoder) Order(ord marshalOrder) *Encoder {
|
|||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Indentation allows to change indentation when marshalling.
|
||||||
|
func (e *Encoder) Indentation(indent string) *Encoder {
|
||||||
|
e.indentation = indent
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
// SetTagName allows changing default tag "toml"
|
// SetTagName allows changing default tag "toml"
|
||||||
func (e *Encoder) SetTagName(v string) *Encoder {
|
func (e *Encoder) SetTagName(v string) *Encoder {
|
||||||
e.tag = v
|
e.tag = v
|
||||||
@@ -272,8 +352,31 @@ func (e *Encoder) SetTagMultiline(v string) *Encoder {
|
|||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PromoteAnonymous allows to change how anonymous struct fields are marshaled.
|
||||||
|
// Usually, they are marshaled as if the inner exported fields were fields in
|
||||||
|
// the outer struct. However, if an anonymous struct field is given a name in
|
||||||
|
// its TOML tag, it is treated like a regular struct field with that name.
|
||||||
|
// rather than being anonymous.
|
||||||
|
//
|
||||||
|
// In case anonymous promotion is enabled, all anonymous structs are promoted
|
||||||
|
// and treated like regular struct fields.
|
||||||
|
func (e *Encoder) PromoteAnonymous(promote bool) *Encoder {
|
||||||
|
e.promoteAnon = promote
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
func (e *Encoder) marshal(v interface{}) ([]byte, error) {
|
func (e *Encoder) marshal(v interface{}) ([]byte, error) {
|
||||||
|
// Check if indentation is valid
|
||||||
|
for _, char := range e.indentation {
|
||||||
|
if !isSpace(char) {
|
||||||
|
return []byte{}, fmt.Errorf("invalid indentation: must only contains space or tab characters")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mtype := reflect.TypeOf(v)
|
mtype := reflect.TypeOf(v)
|
||||||
|
if mtype == nil {
|
||||||
|
return []byte{}, errors.New("nil cannot be marshaled to TOML")
|
||||||
|
}
|
||||||
|
|
||||||
switch mtype.Kind() {
|
switch mtype.Kind() {
|
||||||
case reflect.Struct, reflect.Map:
|
case reflect.Struct, reflect.Map:
|
||||||
@@ -281,6 +384,9 @@ func (e *Encoder) marshal(v interface{}) ([]byte, error) {
|
|||||||
if mtype.Elem().Kind() != reflect.Struct {
|
if mtype.Elem().Kind() != reflect.Struct {
|
||||||
return []byte{}, errors.New("Only pointer to struct can be marshaled to TOML")
|
return []byte{}, errors.New("Only pointer to struct can be marshaled to TOML")
|
||||||
}
|
}
|
||||||
|
if reflect.ValueOf(v).IsNil() {
|
||||||
|
return []byte{}, errors.New("nil pointer cannot be marshaled to TOML")
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return []byte{}, errors.New("Only a struct or map can be marshaled to TOML")
|
return []byte{}, errors.New("Only a struct or map can be marshaled to TOML")
|
||||||
}
|
}
|
||||||
@@ -289,13 +395,16 @@ func (e *Encoder) marshal(v interface{}) ([]byte, error) {
|
|||||||
if isCustomMarshaler(mtype) {
|
if isCustomMarshaler(mtype) {
|
||||||
return callCustomMarshaler(sval)
|
return callCustomMarshaler(sval)
|
||||||
}
|
}
|
||||||
|
if isTextMarshaler(mtype) {
|
||||||
|
return callTextMarshaler(sval)
|
||||||
|
}
|
||||||
t, err := e.valueToTree(mtype, sval)
|
t, err := e.valueToTree(mtype, sval)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte{}, err
|
return []byte{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
_, err = t.writeToOrdered(&buf, "", "", 0, e.arraysOneElementPerLine, e.order)
|
_, err = t.writeToOrdered(&buf, "", "", 0, e.arraysOneElementPerLine, e.order, e.indentation, false)
|
||||||
|
|
||||||
return buf.Bytes(), err
|
return buf.Bytes(), err
|
||||||
}
|
}
|
||||||
@@ -313,22 +422,31 @@ func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, er
|
|||||||
tval := e.nextTree()
|
tval := e.nextTree()
|
||||||
switch mtype.Kind() {
|
switch mtype.Kind() {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
|
switch mval.Interface().(type) {
|
||||||
|
case Tree:
|
||||||
|
reflect.ValueOf(tval).Elem().Set(mval)
|
||||||
|
default:
|
||||||
for i := 0; i < mtype.NumField(); i++ {
|
for i := 0; i < mtype.NumField(); i++ {
|
||||||
mtypef, mvalf := mtype.Field(i), mval.Field(i)
|
mtypef, mvalf := mtype.Field(i), mval.Field(i)
|
||||||
opts := tomlOptions(mtypef, e.annotation)
|
opts := tomlOptions(mtypef, e.annotation)
|
||||||
if opts.include && (!opts.omitempty || !isZero(mvalf)) {
|
if opts.include && ((mtypef.Type.Kind() != reflect.Interface && !opts.omitempty) || !isZero(mvalf)) {
|
||||||
val, err := e.valueToToml(mtypef.Type, mvalf)
|
val, err := e.valueToToml(mtypef.Type, mvalf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if tree, ok := val.(*Tree); ok && mtypef.Anonymous && !opts.nameFromTag && !e.promoteAnon {
|
||||||
tval.SetWithOptions(opts.name, SetOptions{
|
e.appendTree(tval, tree)
|
||||||
|
} else {
|
||||||
|
val = e.wrapTomlValue(val, tval)
|
||||||
|
tval.SetPathWithOptions([]string{opts.name}, SetOptions{
|
||||||
Comment: opts.comment,
|
Comment: opts.comment,
|
||||||
Commented: opts.commented,
|
Commented: opts.commented,
|
||||||
Multiline: opts.multiline,
|
Multiline: opts.multiline,
|
||||||
}, val)
|
}, val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
keys := mval.MapKeys()
|
keys := mval.MapKeys()
|
||||||
if e.order == OrderPreserve && len(keys) > 0 {
|
if e.order == OrderPreserve && len(keys) > 0 {
|
||||||
@@ -351,18 +469,22 @@ func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, er
|
|||||||
}
|
}
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
mvalf := mval.MapIndex(key)
|
mvalf := mval.MapIndex(key)
|
||||||
|
if (mtype.Elem().Kind() == reflect.Ptr || mtype.Elem().Kind() == reflect.Interface) && mvalf.IsNil() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
val, err := e.valueToToml(mtype.Elem(), mvalf)
|
val, err := e.valueToToml(mtype.Elem(), mvalf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
val = e.wrapTomlValue(val, tval)
|
||||||
if e.quoteMapKeys {
|
if e.quoteMapKeys {
|
||||||
keyStr, err := tomlValueStringRepresentation(key.String(), "", e.arraysOneElementPerLine)
|
keyStr, err := tomlValueStringRepresentation(key.String(), "", "", e.order, e.arraysOneElementPerLine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tval.SetPath([]string{keyStr}, val)
|
tval.SetPath([]string{keyStr}, val)
|
||||||
} else {
|
} else {
|
||||||
tval.Set(key.String(), val)
|
tval.SetPath([]string{key.String()}, val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -397,19 +519,32 @@ func (e *Encoder) valueToOtherSlice(mtype reflect.Type, mval reflect.Value) (int
|
|||||||
|
|
||||||
// Convert given marshal value to toml value
|
// Convert given marshal value to toml value
|
||||||
func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
|
func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
|
||||||
e.line++
|
|
||||||
if mtype.Kind() == reflect.Ptr {
|
if mtype.Kind() == reflect.Ptr {
|
||||||
|
switch {
|
||||||
|
case isCustomMarshaler(mtype):
|
||||||
|
return callCustomMarshaler(mval)
|
||||||
|
case isTextMarshaler(mtype):
|
||||||
|
b, err := callTextMarshaler(mval)
|
||||||
|
return string(b), err
|
||||||
|
default:
|
||||||
return e.valueToToml(mtype.Elem(), mval.Elem())
|
return e.valueToToml(mtype.Elem(), mval.Elem())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if mtype.Kind() == reflect.Interface {
|
||||||
|
return e.valueToToml(mval.Elem().Type(), mval.Elem())
|
||||||
|
}
|
||||||
switch {
|
switch {
|
||||||
case isCustomMarshaler(mtype):
|
case isCustomMarshaler(mtype):
|
||||||
return callCustomMarshaler(mval)
|
return callCustomMarshaler(mval)
|
||||||
|
case isTextMarshaler(mtype):
|
||||||
|
b, err := callTextMarshaler(mval)
|
||||||
|
return string(b), err
|
||||||
case isTree(mtype):
|
case isTree(mtype):
|
||||||
return e.valueToTree(mtype, mval)
|
return e.valueToTree(mtype, mval)
|
||||||
case isTreeSlice(mtype):
|
case isOtherSequence(mtype), isCustomMarshalerSequence(mtype), isTextMarshalerSequence(mtype):
|
||||||
return e.valueToTreeSlice(mtype, mval)
|
|
||||||
case isOtherSlice(mtype):
|
|
||||||
return e.valueToOtherSlice(mtype, mval)
|
return e.valueToOtherSlice(mtype, mval)
|
||||||
|
case isTreeSequence(mtype):
|
||||||
|
return e.valueToTreeSlice(mtype, mval)
|
||||||
default:
|
default:
|
||||||
switch mtype.Kind() {
|
switch mtype.Kind() {
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
@@ -426,13 +561,45 @@ func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface
|
|||||||
case reflect.String:
|
case reflect.String:
|
||||||
return mval.String(), nil
|
return mval.String(), nil
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
return mval.Interface().(time.Time), nil
|
return mval.Interface(), nil
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("Marshal can't handle %v(%v)", mtype, mtype.Kind())
|
return nil, fmt.Errorf("Marshal can't handle %v(%v)", mtype, mtype.Kind())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Encoder) appendTree(t, o *Tree) error {
|
||||||
|
for key, value := range o.values {
|
||||||
|
if _, ok := t.values[key]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if tomlValue, ok := value.(*tomlValue); ok {
|
||||||
|
tomlValue.position.Col = t.position.Col
|
||||||
|
}
|
||||||
|
t.values[key] = value
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a toml value with the current line number as the position line
|
||||||
|
func (e *Encoder) wrapTomlValue(val interface{}, parent *Tree) interface{} {
|
||||||
|
_, isTree := val.(*Tree)
|
||||||
|
_, isTreeS := val.([]*Tree)
|
||||||
|
if isTree || isTreeS {
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := &tomlValue{
|
||||||
|
value: val,
|
||||||
|
position: Position{
|
||||||
|
e.line,
|
||||||
|
parent.position.Col,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
e.line++
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
// Unmarshal attempts to unmarshal the Tree into a Go struct pointed by v.
|
// Unmarshal attempts to unmarshal the Tree into a Go struct pointed by v.
|
||||||
// Neither Unmarshaler interfaces nor UnmarshalTOML functions are supported for
|
// Neither Unmarshaler interfaces nor UnmarshalTOML functions are supported for
|
||||||
// sub-structs, and only definite types can be unmarshaled.
|
// sub-structs, and only definite types can be unmarshaled.
|
||||||
@@ -445,8 +612,11 @@ func (t *Tree) Unmarshal(v interface{}) error {
|
|||||||
// See Marshal() documentation for types mapping table.
|
// See Marshal() documentation for types mapping table.
|
||||||
func (t *Tree) Marshal() ([]byte, error) {
|
func (t *Tree) Marshal() ([]byte, error) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err := NewEncoder(&buf).Encode(t)
|
_, err := t.WriteTo(&buf)
|
||||||
return buf.Bytes(), err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal parses the TOML-encoded data and stores the result in the value
|
// Unmarshal parses the TOML-encoded data and stores the result in the value
|
||||||
@@ -482,6 +652,8 @@ type Decoder struct {
|
|||||||
tval *Tree
|
tval *Tree
|
||||||
encOpts
|
encOpts
|
||||||
tagName string
|
tagName string
|
||||||
|
strict bool
|
||||||
|
visitor visitorState
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDecoder returns a new decoder that reads from r.
|
// NewDecoder returns a new decoder that reads from r.
|
||||||
@@ -512,8 +684,18 @@ func (d *Decoder) SetTagName(v string) *Decoder {
|
|||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Strict allows changing to strict decoding. Any fields that are found in the
|
||||||
|
// input data and do not have a corresponding struct member cause an error.
|
||||||
|
func (d *Decoder) Strict(strict bool) *Decoder {
|
||||||
|
d.strict = strict
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
func (d *Decoder) unmarshal(v interface{}) error {
|
func (d *Decoder) unmarshal(v interface{}) error {
|
||||||
mtype := reflect.TypeOf(v)
|
mtype := reflect.TypeOf(v)
|
||||||
|
if mtype == nil {
|
||||||
|
return errors.New("nil cannot be unmarshaled from TOML")
|
||||||
|
}
|
||||||
if mtype.Kind() != reflect.Ptr {
|
if mtype.Kind() != reflect.Ptr {
|
||||||
return errors.New("only a pointer to struct or map can be unmarshaled from TOML")
|
return errors.New("only a pointer to struct or map can be unmarshaled from TOML")
|
||||||
}
|
}
|
||||||
@@ -522,32 +704,74 @@ func (d *Decoder) unmarshal(v interface{}) error {
|
|||||||
|
|
||||||
switch elem.Kind() {
|
switch elem.Kind() {
|
||||||
case reflect.Struct, reflect.Map:
|
case reflect.Struct, reflect.Map:
|
||||||
|
case reflect.Interface:
|
||||||
|
elem = mapStringInterfaceType
|
||||||
default:
|
default:
|
||||||
return errors.New("only a pointer to struct or map can be unmarshaled from TOML")
|
return errors.New("only a pointer to struct or map can be unmarshaled from TOML")
|
||||||
}
|
}
|
||||||
|
|
||||||
sval, err := d.valueFromTree(elem, d.tval)
|
if reflect.ValueOf(v).IsNil() {
|
||||||
|
return errors.New("nil pointer cannot be unmarshaled from TOML")
|
||||||
|
}
|
||||||
|
|
||||||
|
vv := reflect.ValueOf(v).Elem()
|
||||||
|
|
||||||
|
if d.strict {
|
||||||
|
d.visitor = newVisitorState(d.tval)
|
||||||
|
}
|
||||||
|
|
||||||
|
sval, err := d.valueFromTree(elem, d.tval, &vv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err := d.visitor.validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
reflect.ValueOf(v).Elem().Set(sval)
|
reflect.ValueOf(v).Elem().Set(sval)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert toml tree to marshal struct or map, using marshal type
|
// Convert toml tree to marshal struct or map, using marshal type. When mval1
|
||||||
func (d *Decoder) valueFromTree(mtype reflect.Type, tval *Tree) (reflect.Value, error) {
|
// is non-nil, merge fields into the given value instead of allocating a new one.
|
||||||
|
func (d *Decoder) valueFromTree(mtype reflect.Type, tval *Tree, mval1 *reflect.Value) (reflect.Value, error) {
|
||||||
if mtype.Kind() == reflect.Ptr {
|
if mtype.Kind() == reflect.Ptr {
|
||||||
return d.unwrapPointer(mtype, tval)
|
return d.unwrapPointer(mtype, tval, mval1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if pointer to value implements the Unmarshaler interface.
|
||||||
|
if mvalPtr := reflect.New(mtype); isCustomUnmarshaler(mvalPtr.Type()) {
|
||||||
|
d.visitor.visitAll()
|
||||||
|
|
||||||
|
if tval == nil {
|
||||||
|
return mvalPtr.Elem(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := callCustomUnmarshaler(mvalPtr, tval.ToMap()); err != nil {
|
||||||
|
return reflect.ValueOf(nil), fmt.Errorf("unmarshal toml: %v", err)
|
||||||
|
}
|
||||||
|
return mvalPtr.Elem(), nil
|
||||||
|
}
|
||||||
|
|
||||||
var mval reflect.Value
|
var mval reflect.Value
|
||||||
switch mtype.Kind() {
|
switch mtype.Kind() {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
|
if mval1 != nil {
|
||||||
|
mval = *mval1
|
||||||
|
} else {
|
||||||
mval = reflect.New(mtype).Elem()
|
mval = reflect.New(mtype).Elem()
|
||||||
|
}
|
||||||
|
|
||||||
|
switch mval.Interface().(type) {
|
||||||
|
case Tree:
|
||||||
|
mval.Set(reflect.ValueOf(tval).Elem())
|
||||||
|
default:
|
||||||
for i := 0; i < mtype.NumField(); i++ {
|
for i := 0; i < mtype.NumField(); i++ {
|
||||||
mtypef := mtype.Field(i)
|
mtypef := mtype.Field(i)
|
||||||
an := annotation{tag: d.tagName}
|
an := annotation{tag: d.tagName}
|
||||||
opts := tomlOptions(mtypef, an)
|
opts := tomlOptions(mtypef, an)
|
||||||
if opts.include {
|
if !opts.include {
|
||||||
|
continue
|
||||||
|
}
|
||||||
baseKey := opts.name
|
baseKey := opts.name
|
||||||
keysToTry := []string{
|
keysToTry := []string{
|
||||||
baseKey,
|
baseKey,
|
||||||
@@ -557,65 +781,97 @@ func (d *Decoder) valueFromTree(mtype reflect.Type, tval *Tree) (reflect.Value,
|
|||||||
}
|
}
|
||||||
|
|
||||||
found := false
|
found := false
|
||||||
|
if tval != nil {
|
||||||
for _, key := range keysToTry {
|
for _, key := range keysToTry {
|
||||||
exists := tval.Has(key)
|
exists := tval.HasPath([]string{key})
|
||||||
if !exists {
|
if !exists {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
val := tval.Get(key)
|
|
||||||
mvalf, err := d.valueFromToml(mtypef.Type, val)
|
d.visitor.push(key)
|
||||||
|
val := tval.GetPath([]string{key})
|
||||||
|
fval := mval.Field(i)
|
||||||
|
mvalf, err := d.valueFromToml(mtypef.Type, val, &fval)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mval, formatError(err, tval.GetPosition(key))
|
return mval, formatError(err, tval.GetPositionPath([]string{key}))
|
||||||
}
|
}
|
||||||
mval.Field(i).Set(mvalf)
|
mval.Field(i).Set(mvalf)
|
||||||
found = true
|
found = true
|
||||||
|
d.visitor.pop()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !found && opts.defaultValue != "" {
|
if !found && opts.defaultValue != "" {
|
||||||
mvalf := mval.Field(i)
|
mvalf := mval.Field(i)
|
||||||
var val interface{}
|
var val interface{}
|
||||||
var err error
|
var err error
|
||||||
switch mvalf.Kind() {
|
switch mvalf.Kind() {
|
||||||
case reflect.Bool:
|
|
||||||
val, err = strconv.ParseBool(opts.defaultValue)
|
|
||||||
if err != nil {
|
|
||||||
return mval.Field(i), err
|
|
||||||
}
|
|
||||||
case reflect.Int:
|
|
||||||
val, err = strconv.Atoi(opts.defaultValue)
|
|
||||||
if err != nil {
|
|
||||||
return mval.Field(i), err
|
|
||||||
}
|
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
val = opts.defaultValue
|
val = opts.defaultValue
|
||||||
|
case reflect.Bool:
|
||||||
|
val, err = strconv.ParseBool(opts.defaultValue)
|
||||||
|
case reflect.Uint:
|
||||||
|
val, err = strconv.ParseUint(opts.defaultValue, 10, 0)
|
||||||
|
case reflect.Uint8:
|
||||||
|
val, err = strconv.ParseUint(opts.defaultValue, 10, 8)
|
||||||
|
case reflect.Uint16:
|
||||||
|
val, err = strconv.ParseUint(opts.defaultValue, 10, 16)
|
||||||
|
case reflect.Uint32:
|
||||||
|
val, err = strconv.ParseUint(opts.defaultValue, 10, 32)
|
||||||
|
case reflect.Uint64:
|
||||||
|
val, err = strconv.ParseUint(opts.defaultValue, 10, 64)
|
||||||
|
case reflect.Int:
|
||||||
|
val, err = strconv.ParseInt(opts.defaultValue, 10, 0)
|
||||||
|
case reflect.Int8:
|
||||||
|
val, err = strconv.ParseInt(opts.defaultValue, 10, 8)
|
||||||
|
case reflect.Int16:
|
||||||
|
val, err = strconv.ParseInt(opts.defaultValue, 10, 16)
|
||||||
|
case reflect.Int32:
|
||||||
|
val, err = strconv.ParseInt(opts.defaultValue, 10, 32)
|
||||||
case reflect.Int64:
|
case reflect.Int64:
|
||||||
val, err = strconv.ParseInt(opts.defaultValue, 10, 64)
|
val, err = strconv.ParseInt(opts.defaultValue, 10, 64)
|
||||||
if err != nil {
|
case reflect.Float32:
|
||||||
return mval.Field(i), err
|
val, err = strconv.ParseFloat(opts.defaultValue, 32)
|
||||||
}
|
|
||||||
case reflect.Float64:
|
case reflect.Float64:
|
||||||
val, err = strconv.ParseFloat(opts.defaultValue, 64)
|
val, err = strconv.ParseFloat(opts.defaultValue, 64)
|
||||||
if err != nil {
|
|
||||||
return mval.Field(i), err
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return mval.Field(i), fmt.Errorf("unsuported field type for default option")
|
return mvalf, fmt.Errorf("unsupported field type for default option")
|
||||||
}
|
}
|
||||||
mval.Field(i).Set(reflect.ValueOf(val))
|
|
||||||
|
if err != nil {
|
||||||
|
return mvalf, err
|
||||||
|
}
|
||||||
|
mvalf.Set(reflect.ValueOf(val).Convert(mvalf.Type()))
|
||||||
|
}
|
||||||
|
|
||||||
|
// save the old behavior above and try to check structs
|
||||||
|
if !found && opts.defaultValue == "" && mtypef.Type.Kind() == reflect.Struct {
|
||||||
|
tmpTval := tval
|
||||||
|
if !mtypef.Anonymous {
|
||||||
|
tmpTval = nil
|
||||||
|
}
|
||||||
|
fval := mval.Field(i)
|
||||||
|
v, err := d.valueFromTree(mtypef.Type, tmpTval, &fval)
|
||||||
|
if err != nil {
|
||||||
|
return v, err
|
||||||
|
}
|
||||||
|
mval.Field(i).Set(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
mval = reflect.MakeMap(mtype)
|
mval = reflect.MakeMap(mtype)
|
||||||
for _, key := range tval.Keys() {
|
for _, key := range tval.Keys() {
|
||||||
|
d.visitor.push(key)
|
||||||
// TODO: path splits key
|
// TODO: path splits key
|
||||||
val := tval.GetPath([]string{key})
|
val := tval.GetPath([]string{key})
|
||||||
mvalf, err := d.valueFromToml(mtype.Elem(), val)
|
mvalf, err := d.valueFromToml(mtype.Elem(), val, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mval, formatError(err, tval.GetPosition(key))
|
return mval, formatError(err, tval.GetPositionPath([]string{key}))
|
||||||
}
|
}
|
||||||
mval.SetMapIndex(reflect.ValueOf(key).Convert(mtype.Key()), mvalf)
|
mval.SetMapIndex(reflect.ValueOf(key).Convert(mtype.Key()), mvalf)
|
||||||
|
d.visitor.pop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return mval, nil
|
return mval, nil
|
||||||
@@ -623,9 +879,32 @@ func (d *Decoder) valueFromTree(mtype reflect.Type, tval *Tree) (reflect.Value,
|
|||||||
|
|
||||||
// Convert toml value to marshal struct/map slice, using marshal type
|
// Convert toml value to marshal struct/map slice, using marshal type
|
||||||
func (d *Decoder) valueFromTreeSlice(mtype reflect.Type, tval []*Tree) (reflect.Value, error) {
|
func (d *Decoder) valueFromTreeSlice(mtype reflect.Type, tval []*Tree) (reflect.Value, error) {
|
||||||
mval := reflect.MakeSlice(mtype, len(tval), len(tval))
|
mval, err := makeSliceOrArray(mtype, len(tval))
|
||||||
|
if err != nil {
|
||||||
|
return mval, err
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < len(tval); i++ {
|
for i := 0; i < len(tval); i++ {
|
||||||
val, err := d.valueFromTree(mtype.Elem(), tval[i])
|
d.visitor.push(strconv.Itoa(i))
|
||||||
|
val, err := d.valueFromTree(mtype.Elem(), tval[i], nil)
|
||||||
|
if err != nil {
|
||||||
|
return mval, err
|
||||||
|
}
|
||||||
|
mval.Index(i).Set(val)
|
||||||
|
d.visitor.pop()
|
||||||
|
}
|
||||||
|
return mval, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert toml value to marshal primitive slice, using marshal type
|
||||||
|
func (d *Decoder) valueFromOtherSlice(mtype reflect.Type, tval []interface{}) (reflect.Value, error) {
|
||||||
|
mval, err := makeSliceOrArray(mtype, len(tval))
|
||||||
|
if err != nil {
|
||||||
|
return mval, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(tval); i++ {
|
||||||
|
val, err := d.valueFromToml(mtype.Elem(), tval[i], nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mval, err
|
return mval, err
|
||||||
}
|
}
|
||||||
@@ -635,10 +914,17 @@ func (d *Decoder) valueFromTreeSlice(mtype reflect.Type, tval []*Tree) (reflect.
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Convert toml value to marshal primitive slice, using marshal type
|
// Convert toml value to marshal primitive slice, using marshal type
|
||||||
func (d *Decoder) valueFromOtherSlice(mtype reflect.Type, tval []interface{}) (reflect.Value, error) {
|
func (d *Decoder) valueFromOtherSliceI(mtype reflect.Type, tval interface{}) (reflect.Value, error) {
|
||||||
mval := reflect.MakeSlice(mtype, len(tval), len(tval))
|
val := reflect.ValueOf(tval)
|
||||||
for i := 0; i < len(tval); i++ {
|
length := val.Len()
|
||||||
val, err := d.valueFromToml(mtype.Elem(), tval[i])
|
|
||||||
|
mval, err := makeSliceOrArray(mtype, length)
|
||||||
|
if err != nil {
|
||||||
|
return mval, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < length; i++ {
|
||||||
|
val, err := d.valueFromToml(mtype.Elem(), val.Index(i).Interface(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mval, err
|
return mval, err
|
||||||
}
|
}
|
||||||
@@ -647,33 +933,113 @@ func (d *Decoder) valueFromOtherSlice(mtype reflect.Type, tval []interface{}) (r
|
|||||||
return mval, nil
|
return mval, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert toml value to marshal value, using marshal type
|
// Create a new slice or a new array with specified length
|
||||||
func (d *Decoder) valueFromToml(mtype reflect.Type, tval interface{}) (reflect.Value, error) {
|
func makeSliceOrArray(mtype reflect.Type, tLength int) (reflect.Value, error) {
|
||||||
|
var mval reflect.Value
|
||||||
|
switch mtype.Kind() {
|
||||||
|
case reflect.Slice:
|
||||||
|
mval = reflect.MakeSlice(mtype, tLength, tLength)
|
||||||
|
case reflect.Array:
|
||||||
|
mval = reflect.New(reflect.ArrayOf(mtype.Len(), mtype.Elem())).Elem()
|
||||||
|
if tLength > mtype.Len() {
|
||||||
|
return mval, fmt.Errorf("unmarshal: TOML array length (%v) exceeds destination array length (%v)", tLength, mtype.Len())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mval, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert toml value to marshal value, using marshal type. When mval1 is non-nil
|
||||||
|
// and the given type is a struct value, merge fields into it.
|
||||||
|
func (d *Decoder) valueFromToml(mtype reflect.Type, tval interface{}, mval1 *reflect.Value) (reflect.Value, error) {
|
||||||
if mtype.Kind() == reflect.Ptr {
|
if mtype.Kind() == reflect.Ptr {
|
||||||
return d.unwrapPointer(mtype, tval)
|
return d.unwrapPointer(mtype, tval, mval1)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch t := tval.(type) {
|
switch t := tval.(type) {
|
||||||
case *Tree:
|
case *Tree:
|
||||||
if isTree(mtype) {
|
var mval11 *reflect.Value
|
||||||
return d.valueFromTree(mtype, t)
|
if mtype.Kind() == reflect.Struct {
|
||||||
|
mval11 = mval1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isTree(mtype) {
|
||||||
|
return d.valueFromTree(mtype, t, mval11)
|
||||||
|
}
|
||||||
|
|
||||||
|
if mtype.Kind() == reflect.Interface {
|
||||||
|
if mval1 == nil || mval1.IsNil() {
|
||||||
|
return d.valueFromTree(reflect.TypeOf(map[string]interface{}{}), t, nil)
|
||||||
|
} else {
|
||||||
|
return d.valueFromToml(mval1.Elem().Type(), t, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to a tree", tval, tval)
|
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to a tree", tval, tval)
|
||||||
case []*Tree:
|
case []*Tree:
|
||||||
if isTreeSlice(mtype) {
|
if isTreeSequence(mtype) {
|
||||||
return d.valueFromTreeSlice(mtype, t)
|
return d.valueFromTreeSlice(mtype, t)
|
||||||
}
|
}
|
||||||
|
if mtype.Kind() == reflect.Interface {
|
||||||
|
if mval1 == nil || mval1.IsNil() {
|
||||||
|
return d.valueFromTreeSlice(reflect.TypeOf([]map[string]interface{}{}), t)
|
||||||
|
} else {
|
||||||
|
ival := mval1.Elem()
|
||||||
|
return d.valueFromToml(mval1.Elem().Type(), t, &ival)
|
||||||
|
}
|
||||||
|
}
|
||||||
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to trees", tval, tval)
|
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to trees", tval, tval)
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
if isOtherSlice(mtype) {
|
d.visitor.visit()
|
||||||
|
if isOtherSequence(mtype) {
|
||||||
return d.valueFromOtherSlice(mtype, t)
|
return d.valueFromOtherSlice(mtype, t)
|
||||||
}
|
}
|
||||||
|
if mtype.Kind() == reflect.Interface {
|
||||||
|
if mval1 == nil || mval1.IsNil() {
|
||||||
|
return d.valueFromOtherSlice(reflect.TypeOf([]interface{}{}), t)
|
||||||
|
} else {
|
||||||
|
ival := mval1.Elem()
|
||||||
|
return d.valueFromToml(mval1.Elem().Type(), t, &ival)
|
||||||
|
}
|
||||||
|
}
|
||||||
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to a slice", tval, tval)
|
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to a slice", tval, tval)
|
||||||
default:
|
default:
|
||||||
|
d.visitor.visit()
|
||||||
|
// Check if pointer to value implements the encoding.TextUnmarshaler.
|
||||||
|
if mvalPtr := reflect.New(mtype); isTextUnmarshaler(mvalPtr.Type()) && !isTimeType(mtype) {
|
||||||
|
if err := d.unmarshalText(tval, mvalPtr); err != nil {
|
||||||
|
return reflect.ValueOf(nil), fmt.Errorf("unmarshal text: %v", err)
|
||||||
|
}
|
||||||
|
return mvalPtr.Elem(), nil
|
||||||
|
}
|
||||||
|
|
||||||
switch mtype.Kind() {
|
switch mtype.Kind() {
|
||||||
case reflect.Bool, reflect.Struct:
|
case reflect.Bool, reflect.Struct:
|
||||||
val := reflect.ValueOf(tval)
|
val := reflect.ValueOf(tval)
|
||||||
// if this passes for when mtype is reflect.Struct, tval is a time.Time
|
|
||||||
|
switch val.Type() {
|
||||||
|
case localDateType:
|
||||||
|
localDate := val.Interface().(LocalDate)
|
||||||
|
switch mtype {
|
||||||
|
case timeType:
|
||||||
|
return reflect.ValueOf(time.Date(localDate.Year, localDate.Month, localDate.Day, 0, 0, 0, 0, time.Local)), nil
|
||||||
|
}
|
||||||
|
case localDateTimeType:
|
||||||
|
localDateTime := val.Interface().(LocalDateTime)
|
||||||
|
switch mtype {
|
||||||
|
case timeType:
|
||||||
|
return reflect.ValueOf(time.Date(
|
||||||
|
localDateTime.Date.Year,
|
||||||
|
localDateTime.Date.Month,
|
||||||
|
localDateTime.Date.Day,
|
||||||
|
localDateTime.Time.Hour,
|
||||||
|
localDateTime.Time.Minute,
|
||||||
|
localDateTime.Time.Second,
|
||||||
|
localDateTime.Time.Nanosecond,
|
||||||
|
time.Local)), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if this passes for when mtype is reflect.Struct, tval is a time.LocalTime
|
||||||
if !val.Type().ConvertibleTo(mtype) {
|
if !val.Type().ConvertibleTo(mtype) {
|
||||||
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v", tval, tval, mtype.String())
|
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v", tval, tval, mtype.String())
|
||||||
}
|
}
|
||||||
@@ -696,46 +1062,65 @@ func (d *Decoder) valueFromToml(mtype reflect.Type, tval interface{}) (reflect.V
|
|||||||
}
|
}
|
||||||
return reflect.ValueOf(d), nil
|
return reflect.ValueOf(d), nil
|
||||||
}
|
}
|
||||||
if !val.Type().ConvertibleTo(mtype) {
|
if !val.Type().ConvertibleTo(mtype) || val.Kind() == reflect.Float64 {
|
||||||
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v", tval, tval, mtype.String())
|
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v", tval, tval, mtype.String())
|
||||||
}
|
}
|
||||||
if reflect.Indirect(reflect.New(mtype)).OverflowInt(val.Convert(mtype).Int()) {
|
if reflect.Indirect(reflect.New(mtype)).OverflowInt(val.Convert(reflect.TypeOf(int64(0))).Int()) {
|
||||||
return reflect.ValueOf(nil), fmt.Errorf("%v(%T) would overflow %v", tval, tval, mtype.String())
|
return reflect.ValueOf(nil), fmt.Errorf("%v(%T) would overflow %v", tval, tval, mtype.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
return val.Convert(mtype), nil
|
return val.Convert(mtype), nil
|
||||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||||
val := reflect.ValueOf(tval)
|
val := reflect.ValueOf(tval)
|
||||||
if !val.Type().ConvertibleTo(mtype) {
|
if !val.Type().ConvertibleTo(mtype) || val.Kind() == reflect.Float64 {
|
||||||
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v", tval, tval, mtype.String())
|
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v", tval, tval, mtype.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
if val.Convert(reflect.TypeOf(int(1))).Int() < 0 {
|
if val.Convert(reflect.TypeOf(int(1))).Int() < 0 {
|
||||||
return reflect.ValueOf(nil), fmt.Errorf("%v(%T) is negative so does not fit in %v", tval, tval, mtype.String())
|
return reflect.ValueOf(nil), fmt.Errorf("%v(%T) is negative so does not fit in %v", tval, tval, mtype.String())
|
||||||
}
|
}
|
||||||
if reflect.Indirect(reflect.New(mtype)).OverflowUint(uint64(val.Convert(mtype).Uint())) {
|
if reflect.Indirect(reflect.New(mtype)).OverflowUint(val.Convert(reflect.TypeOf(uint64(0))).Uint()) {
|
||||||
return reflect.ValueOf(nil), fmt.Errorf("%v(%T) would overflow %v", tval, tval, mtype.String())
|
return reflect.ValueOf(nil), fmt.Errorf("%v(%T) would overflow %v", tval, tval, mtype.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
return val.Convert(mtype), nil
|
return val.Convert(mtype), nil
|
||||||
case reflect.Float32, reflect.Float64:
|
case reflect.Float32, reflect.Float64:
|
||||||
val := reflect.ValueOf(tval)
|
val := reflect.ValueOf(tval)
|
||||||
if !val.Type().ConvertibleTo(mtype) {
|
if !val.Type().ConvertibleTo(mtype) || val.Kind() == reflect.Int64 {
|
||||||
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v", tval, tval, mtype.String())
|
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v", tval, tval, mtype.String())
|
||||||
}
|
}
|
||||||
if reflect.Indirect(reflect.New(mtype)).OverflowFloat(val.Convert(mtype).Float()) {
|
if reflect.Indirect(reflect.New(mtype)).OverflowFloat(val.Convert(reflect.TypeOf(float64(0))).Float()) {
|
||||||
return reflect.ValueOf(nil), fmt.Errorf("%v(%T) would overflow %v", tval, tval, mtype.String())
|
return reflect.ValueOf(nil), fmt.Errorf("%v(%T) would overflow %v", tval, tval, mtype.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
return val.Convert(mtype), nil
|
return val.Convert(mtype), nil
|
||||||
|
case reflect.Interface:
|
||||||
|
if mval1 == nil || mval1.IsNil() {
|
||||||
|
return reflect.ValueOf(tval), nil
|
||||||
|
} else {
|
||||||
|
ival := mval1.Elem()
|
||||||
|
return d.valueFromToml(mval1.Elem().Type(), t, &ival)
|
||||||
|
}
|
||||||
|
case reflect.Slice, reflect.Array:
|
||||||
|
if isOtherSequence(mtype) && isOtherSequence(reflect.TypeOf(t)) {
|
||||||
|
return d.valueFromOtherSliceI(mtype, t)
|
||||||
|
}
|
||||||
|
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v(%v)", tval, tval, mtype, mtype.Kind())
|
||||||
default:
|
default:
|
||||||
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v(%v)", tval, tval, mtype, mtype.Kind())
|
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v(%v)", tval, tval, mtype, mtype.Kind())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) unwrapPointer(mtype reflect.Type, tval interface{}) (reflect.Value, error) {
|
func (d *Decoder) unwrapPointer(mtype reflect.Type, tval interface{}, mval1 *reflect.Value) (reflect.Value, error) {
|
||||||
val, err := d.valueFromToml(mtype.Elem(), tval)
|
var melem *reflect.Value
|
||||||
|
|
||||||
|
if mval1 != nil && !mval1.IsNil() && (mtype.Elem().Kind() == reflect.Struct || mtype.Elem().Kind() == reflect.Interface) {
|
||||||
|
elem := mval1.Elem()
|
||||||
|
melem = &elem
|
||||||
|
}
|
||||||
|
|
||||||
|
val, err := d.valueFromToml(mtype.Elem(), tval, melem)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return reflect.ValueOf(nil), err
|
return reflect.ValueOf(nil), err
|
||||||
}
|
}
|
||||||
@@ -744,6 +1129,12 @@ func (d *Decoder) unwrapPointer(mtype reflect.Type, tval interface{}) (reflect.V
|
|||||||
return mval, nil
|
return mval, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) unmarshalText(tval interface{}, mval reflect.Value) error {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
fmt.Fprint(&buf, tval)
|
||||||
|
return callTextUnmarshaler(mval, buf.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
func tomlOptions(vf reflect.StructField, an annotation) tomlOpts {
|
func tomlOptions(vf reflect.StructField, an annotation) tomlOpts {
|
||||||
tag := vf.Tag.Get(an.tag)
|
tag := vf.Tag.Get(an.tag)
|
||||||
parse := strings.Split(tag, ",")
|
parse := strings.Split(tag, ",")
|
||||||
@@ -756,6 +1147,7 @@ func tomlOptions(vf reflect.StructField, an annotation) tomlOpts {
|
|||||||
defaultValue := vf.Tag.Get(tagDefault)
|
defaultValue := vf.Tag.Get(tagDefault)
|
||||||
result := tomlOpts{
|
result := tomlOpts{
|
||||||
name: vf.Name,
|
name: vf.Name,
|
||||||
|
nameFromTag: false,
|
||||||
comment: comment,
|
comment: comment,
|
||||||
commented: commented,
|
commented: commented,
|
||||||
multiline: multiline,
|
multiline: multiline,
|
||||||
@@ -768,6 +1160,7 @@ func tomlOptions(vf reflect.StructField, an annotation) tomlOpts {
|
|||||||
result.include = false
|
result.include = false
|
||||||
} else {
|
} else {
|
||||||
result.name = strings.Trim(parse[0], " ")
|
result.name = strings.Trim(parse[0], " ")
|
||||||
|
result.nameFromTag = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if vf.PkgPath != "" {
|
if vf.PkgPath != "" {
|
||||||
@@ -784,11 +1177,7 @@ func tomlOptions(vf reflect.StructField, an annotation) tomlOpts {
|
|||||||
|
|
||||||
func isZero(val reflect.Value) bool {
|
func isZero(val reflect.Value) bool {
|
||||||
switch val.Type().Kind() {
|
switch val.Type().Kind() {
|
||||||
case reflect.Map:
|
case reflect.Slice, reflect.Array, reflect.Map:
|
||||||
fallthrough
|
|
||||||
case reflect.Array:
|
|
||||||
fallthrough
|
|
||||||
case reflect.Slice:
|
|
||||||
return val.Len() == 0
|
return val.Len() == 0
|
||||||
default:
|
default:
|
||||||
return reflect.DeepEqual(val.Interface(), reflect.Zero(val.Type()).Interface())
|
return reflect.DeepEqual(val.Interface(), reflect.Zero(val.Type()).Interface())
|
||||||
@@ -801,3 +1190,80 @@ func formatError(err error, pos Position) error {
|
|||||||
}
|
}
|
||||||
return fmt.Errorf("%s: %s", pos, err)
|
return fmt.Errorf("%s: %s", pos, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// visitorState keeps track of which keys were unmarshaled.
|
||||||
|
type visitorState struct {
|
||||||
|
tree *Tree
|
||||||
|
path []string
|
||||||
|
keys map[string]struct{}
|
||||||
|
active bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func newVisitorState(tree *Tree) visitorState {
|
||||||
|
path, result := []string{}, map[string]struct{}{}
|
||||||
|
insertKeys(path, result, tree)
|
||||||
|
return visitorState{
|
||||||
|
tree: tree,
|
||||||
|
path: path[:0],
|
||||||
|
keys: result,
|
||||||
|
active: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *visitorState) push(key string) {
|
||||||
|
if s.active {
|
||||||
|
s.path = append(s.path, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *visitorState) pop() {
|
||||||
|
if s.active {
|
||||||
|
s.path = s.path[:len(s.path)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *visitorState) visit() {
|
||||||
|
if s.active {
|
||||||
|
delete(s.keys, strings.Join(s.path, "."))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *visitorState) visitAll() {
|
||||||
|
if s.active {
|
||||||
|
for k := range s.keys {
|
||||||
|
if strings.HasPrefix(k, strings.Join(s.path, ".")) {
|
||||||
|
delete(s.keys, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *visitorState) validate() error {
|
||||||
|
if !s.active {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
undecoded := make([]string, 0, len(s.keys))
|
||||||
|
for key := range s.keys {
|
||||||
|
undecoded = append(undecoded, key)
|
||||||
|
}
|
||||||
|
sort.Strings(undecoded)
|
||||||
|
if len(undecoded) > 0 {
|
||||||
|
return fmt.Errorf("undecoded keys: %q", undecoded)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func insertKeys(path []string, m map[string]struct{}, tree *Tree) {
|
||||||
|
for k, v := range tree.values {
|
||||||
|
switch node := v.(type) {
|
||||||
|
case []*Tree:
|
||||||
|
for i, item := range node {
|
||||||
|
insertKeys(append(path, k, strconv.Itoa(i)), m, item)
|
||||||
|
}
|
||||||
|
case *Tree:
|
||||||
|
insertKeys(append(path, k), m, node)
|
||||||
|
case *tomlValue:
|
||||||
|
m[strings.Join(append(path, k), ".")] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
title = "TOML Marshal Testing"
|
|
||||||
|
|
||||||
[basic_map]
|
|
||||||
one = "one"
|
|
||||||
two = "two"
|
|
||||||
|
|
||||||
[long_map]
|
|
||||||
a7 = "1"
|
|
||||||
b3 = "2"
|
|
||||||
c8 = "3"
|
|
||||||
d4 = "4"
|
|
||||||
e6 = "5"
|
|
||||||
f5 = "6"
|
|
||||||
g10 = "7"
|
|
||||||
h1 = "8"
|
|
||||||
i2 = "9"
|
|
||||||
j9 = "10"
|
|
||||||
@@ -27,6 +27,7 @@ title = "TOML Marshal Testing"
|
|||||||
uint = 5001
|
uint = 5001
|
||||||
bool = true
|
bool = true
|
||||||
float = 123.4
|
float = 123.4
|
||||||
|
float64 = 123.456782132399
|
||||||
int = 5000
|
int = 5000
|
||||||
string = "Bite me"
|
string = "Bite me"
|
||||||
date = 1979-05-27T07:32:00Z
|
date = 1979-05-27T07:32:00Z
|
||||||
|
|||||||
+2616
-57
File diff suppressed because it is too large
Load Diff
@@ -4,6 +4,7 @@ title = "TOML Marshal Testing"
|
|||||||
bool = true
|
bool = true
|
||||||
date = 1979-05-27T07:32:00Z
|
date = 1979-05-27T07:32:00Z
|
||||||
float = 123.4
|
float = 123.4
|
||||||
|
float64 = 123.456782132399
|
||||||
int = 5000
|
int = 5000
|
||||||
string = "Bite me"
|
string = "Bite me"
|
||||||
uint = 5001
|
uint = 5001
|
||||||
|
|||||||
@@ -158,6 +158,11 @@ func (p *tomlParser) parseGroup() tomlParserStateFn {
|
|||||||
if err := p.tree.createSubTree(keys, startToken.Position); err != nil {
|
if err := p.tree.createSubTree(keys, startToken.Position); err != nil {
|
||||||
p.raiseError(key, "%s", err)
|
p.raiseError(key, "%s", err)
|
||||||
}
|
}
|
||||||
|
destTree := p.tree.GetPath(keys)
|
||||||
|
if target, ok := destTree.(*Tree); ok && target != nil && target.inline {
|
||||||
|
p.raiseError(key, "could not re-define exist inline table or its sub-table : %s",
|
||||||
|
strings.Join(keys, "."))
|
||||||
|
}
|
||||||
p.assume(tokenRightBracket)
|
p.assume(tokenRightBracket)
|
||||||
p.currentTable = keys
|
p.currentTable = keys
|
||||||
return p.parseStart
|
return p.parseStart
|
||||||
@@ -201,6 +206,11 @@ func (p *tomlParser) parseAssign() tomlParserStateFn {
|
|||||||
strings.Join(tableKey, "."))
|
strings.Join(tableKey, "."))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if targetNode.inline {
|
||||||
|
p.raiseError(key, "could not add key or sub-table to exist inline table or its sub-table : %s",
|
||||||
|
strings.Join(tableKey, "."))
|
||||||
|
}
|
||||||
|
|
||||||
// assign value to the found table
|
// assign value to the found table
|
||||||
keyVal := parsedKey[len(parsedKey)-1]
|
keyVal := parsedKey[len(parsedKey)-1]
|
||||||
localKey := []string{keyVal}
|
localKey := []string{keyVal}
|
||||||
@@ -313,7 +323,41 @@ func (p *tomlParser) parseRvalue() interface{} {
|
|||||||
}
|
}
|
||||||
return val
|
return val
|
||||||
case tokenDate:
|
case tokenDate:
|
||||||
val, err := time.ParseInLocation(time.RFC3339Nano, tok.val, time.UTC)
|
layout := time.RFC3339Nano
|
||||||
|
if !strings.Contains(tok.val, "T") {
|
||||||
|
layout = strings.Replace(layout, "T", " ", 1)
|
||||||
|
}
|
||||||
|
val, err := time.ParseInLocation(layout, tok.val, time.UTC)
|
||||||
|
if err != nil {
|
||||||
|
p.raiseError(tok, "%s", err)
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
case tokenLocalDate:
|
||||||
|
v := strings.Replace(tok.val, " ", "T", -1)
|
||||||
|
isDateTime := false
|
||||||
|
isTime := false
|
||||||
|
for _, c := range v {
|
||||||
|
if c == 'T' || c == 't' {
|
||||||
|
isDateTime = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if c == ':' {
|
||||||
|
isTime = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var val interface{}
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if isDateTime {
|
||||||
|
val, err = ParseLocalDateTime(v)
|
||||||
|
} else if isTime {
|
||||||
|
val, err = ParseLocalTime(v)
|
||||||
|
} else {
|
||||||
|
val, err = ParseLocalDate(v)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.raiseError(tok, "%s", err)
|
p.raiseError(tok, "%s", err)
|
||||||
}
|
}
|
||||||
@@ -356,12 +400,15 @@ Loop:
|
|||||||
}
|
}
|
||||||
key := p.getToken()
|
key := p.getToken()
|
||||||
p.assume(tokenEqual)
|
p.assume(tokenEqual)
|
||||||
value := p.parseRvalue()
|
|
||||||
tree.Set(key.val, value)
|
parsedKey, err := parseKey(key.val)
|
||||||
case tokenComma:
|
if err != nil {
|
||||||
if previous == nil {
|
p.raiseError(key, "invalid key: %s", err)
|
||||||
p.raiseError(follow, "inline table cannot start with a comma")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
value := p.parseRvalue()
|
||||||
|
tree.SetPath(parsedKey, value)
|
||||||
|
case tokenComma:
|
||||||
if tokenIsComma(previous) {
|
if tokenIsComma(previous) {
|
||||||
p.raiseError(follow, "need field between two commas in inline table")
|
p.raiseError(follow, "need field between two commas in inline table")
|
||||||
}
|
}
|
||||||
@@ -374,12 +421,13 @@ Loop:
|
|||||||
if tokenIsComma(previous) {
|
if tokenIsComma(previous) {
|
||||||
p.raiseError(previous, "trailing comma at the end of inline table")
|
p.raiseError(previous, "trailing comma at the end of inline table")
|
||||||
}
|
}
|
||||||
|
tree.inline = true
|
||||||
return tree
|
return tree
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *tomlParser) parseArray() interface{} {
|
func (p *tomlParser) parseArray() interface{} {
|
||||||
var array []interface{}
|
var array []interface{}
|
||||||
arrayType := reflect.TypeOf(nil)
|
arrayType := reflect.TypeOf(newTree())
|
||||||
for {
|
for {
|
||||||
follow := p.peek()
|
follow := p.peek()
|
||||||
if follow == nil || follow.typ == tokenEOF {
|
if follow == nil || follow.typ == tokenEOF {
|
||||||
@@ -390,11 +438,8 @@ func (p *tomlParser) parseArray() interface{} {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
val := p.parseRvalue()
|
val := p.parseRvalue()
|
||||||
if arrayType == nil {
|
|
||||||
arrayType = reflect.TypeOf(val)
|
|
||||||
}
|
|
||||||
if reflect.TypeOf(val) != arrayType {
|
if reflect.TypeOf(val) != arrayType {
|
||||||
p.raiseError(follow, "mixed types in array")
|
arrayType = nil
|
||||||
}
|
}
|
||||||
array = append(array, val)
|
array = append(array, val)
|
||||||
follow = p.peek()
|
follow = p.peek()
|
||||||
@@ -408,6 +453,12 @@ func (p *tomlParser) parseArray() interface{} {
|
|||||||
p.getToken()
|
p.getToken()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if the array is a mixed-type array or its length is 0,
|
||||||
|
// don't convert it to a table array
|
||||||
|
if len(array) <= 0 {
|
||||||
|
arrayType = nil
|
||||||
|
}
|
||||||
// An array of Trees is actually an array of inline
|
// An array of Trees is actually an array of inline
|
||||||
// tables, which is a shorthand for a table array. If the
|
// tables, which is a shorthand for a table array. If the
|
||||||
// array was not converted from []interface{} to []*Tree,
|
// array was not converted from []interface{} to []*Tree,
|
||||||
|
|||||||
+221
-22
@@ -197,7 +197,7 @@ func TestFloatsWithExponents(t *testing.T) {
|
|||||||
tree, err := Load("a = 5e+22\nb = 5E+22\nc = -5e+22\nd = -5e-22\ne = 6.626e-34")
|
tree, err := Load("a = 5e+22\nb = 5E+22\nc = -5e+22\nd = -5e-22\ne = 6.626e-34")
|
||||||
assertTree(t, tree, err, map[string]interface{}{
|
assertTree(t, tree, err, map[string]interface{}{
|
||||||
"a": float64(5e+22),
|
"a": float64(5e+22),
|
||||||
"b": float64(5E+22),
|
"b": float64(5e+22),
|
||||||
"c": float64(-5e+22),
|
"c": float64(-5e+22),
|
||||||
"d": float64(-5e-22),
|
"d": float64(-5e-22),
|
||||||
"e": float64(6.626e-34),
|
"e": float64(6.626e-34),
|
||||||
@@ -225,6 +225,79 @@ func TestDateNano(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLocalDateTime(t *testing.T) {
|
||||||
|
tree, err := Load("a = 1979-05-27T07:32:00")
|
||||||
|
assertTree(t, tree, err, map[string]interface{}{
|
||||||
|
"a": LocalDateTime{
|
||||||
|
Date: LocalDate{
|
||||||
|
Year: 1979,
|
||||||
|
Month: 5,
|
||||||
|
Day: 27,
|
||||||
|
},
|
||||||
|
Time: LocalTime{
|
||||||
|
Hour: 7,
|
||||||
|
Minute: 32,
|
||||||
|
Second: 0,
|
||||||
|
Nanosecond: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocalDateTimeNano(t *testing.T) {
|
||||||
|
tree, err := Load("a = 1979-05-27T07:32:00.999999")
|
||||||
|
assertTree(t, tree, err, map[string]interface{}{
|
||||||
|
"a": LocalDateTime{
|
||||||
|
Date: LocalDate{
|
||||||
|
Year: 1979,
|
||||||
|
Month: 5,
|
||||||
|
Day: 27,
|
||||||
|
},
|
||||||
|
Time: LocalTime{
|
||||||
|
Hour: 7,
|
||||||
|
Minute: 32,
|
||||||
|
Second: 0,
|
||||||
|
Nanosecond: 999999000,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocalDate(t *testing.T) {
|
||||||
|
tree, err := Load("a = 1979-05-27")
|
||||||
|
assertTree(t, tree, err, map[string]interface{}{
|
||||||
|
"a": LocalDate{
|
||||||
|
Year: 1979,
|
||||||
|
Month: 5,
|
||||||
|
Day: 27,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocalTime(t *testing.T) {
|
||||||
|
tree, err := Load("a = 07:32:00")
|
||||||
|
assertTree(t, tree, err, map[string]interface{}{
|
||||||
|
"a": LocalTime{
|
||||||
|
Hour: 7,
|
||||||
|
Minute: 32,
|
||||||
|
Second: 0,
|
||||||
|
Nanosecond: 0,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLocalTimeNano(t *testing.T) {
|
||||||
|
tree, err := Load("a = 00:32:00.999999")
|
||||||
|
assertTree(t, tree, err, map[string]interface{}{
|
||||||
|
"a": LocalTime{
|
||||||
|
Hour: 0,
|
||||||
|
Minute: 32,
|
||||||
|
Second: 0,
|
||||||
|
Nanosecond: 999999000,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestSimpleString(t *testing.T) {
|
func TestSimpleString(t *testing.T) {
|
||||||
tree, err := Load("a = \"hello world\"")
|
tree, err := Load("a = \"hello world\"")
|
||||||
assertTree(t, tree, err, map[string]interface{}{
|
assertTree(t, tree, err, map[string]interface{}{
|
||||||
@@ -415,18 +488,6 @@ func TestNestedEmptyArrays(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestArrayMixedTypes(t *testing.T) {
|
|
||||||
_, err := Load("a = [42, 16.0]")
|
|
||||||
if err.Error() != "(1, 10): mixed types in array" {
|
|
||||||
t.Error("Bad error message:", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = Load("a = [42, \"hello\"]")
|
|
||||||
if err.Error() != "(1, 11): mixed types in array" {
|
|
||||||
t.Error("Bad error message:", err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestArrayNestedStrings(t *testing.T) {
|
func TestArrayNestedStrings(t *testing.T) {
|
||||||
tree, err := Load("data = [ [\"gamma\", \"delta\"], [\"Foo\"] ]")
|
tree, err := Load("data = [ [\"gamma\", \"delta\"], [\"Foo\"] ]")
|
||||||
assertTree(t, tree, err, map[string]interface{}{
|
assertTree(t, tree, err, map[string]interface{}{
|
||||||
@@ -510,6 +571,39 @@ func TestDoubleInlineGroup(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNestedInlineGroup(t *testing.T) {
|
||||||
|
tree, err := Load("out = {block0 = {x = 99, y = 100}, block1 = {p = \"999\", q = \"1000\"}}")
|
||||||
|
assertTree(t, tree, err, map[string]interface{}{
|
||||||
|
"out": map[string]interface{}{
|
||||||
|
"block0": map[string]interface{}{
|
||||||
|
"x": int64(99),
|
||||||
|
"y": int64(100),
|
||||||
|
},
|
||||||
|
"block1": map[string]interface{}{
|
||||||
|
"p": "999",
|
||||||
|
"q": "1000",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestArrayInNestedInlineGroup(t *testing.T) {
|
||||||
|
tree, err := Load(`image = {name = "xxx", palette = {id = 100, colors = ["red", "blue", "green"]}}`)
|
||||||
|
assertTree(t, tree, err, map[string]interface{}{
|
||||||
|
"image": map[string]interface{}{
|
||||||
|
"name": "xxx",
|
||||||
|
"palette": map[string]interface{}{
|
||||||
|
"id": int64(100),
|
||||||
|
"colors": []string{
|
||||||
|
"red",
|
||||||
|
"blue",
|
||||||
|
"green",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestExampleInlineGroup(t *testing.T) {
|
func TestExampleInlineGroup(t *testing.T) {
|
||||||
tree, err := Load(`name = { first = "Tom", last = "Preston-Werner" }
|
tree, err := Load(`name = { first = "Tom", last = "Preston-Werner" }
|
||||||
point = { x = 1, y = 2 }`)
|
point = { x = 1, y = 2 }`)
|
||||||
@@ -525,6 +619,33 @@ point = { x = 1, y = 2 }`)
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInlineGroupBareKeysUnderscore(t *testing.T) {
|
||||||
|
tree, err := Load(`foo = { _bar = "buz" }`)
|
||||||
|
assertTree(t, tree, err, map[string]interface{}{
|
||||||
|
"foo": map[string]interface{}{
|
||||||
|
"_bar": "buz",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInlineGroupBareKeysDash(t *testing.T) {
|
||||||
|
tree, err := Load(`foo = { -bar = "buz" }`)
|
||||||
|
assertTree(t, tree, err, map[string]interface{}{
|
||||||
|
"foo": map[string]interface{}{
|
||||||
|
"-bar": "buz",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInlineGroupKeyQuoted(t *testing.T) {
|
||||||
|
tree, err := Load(`foo = { "bar" = "buz" }`)
|
||||||
|
assertTree(t, tree, err, map[string]interface{}{
|
||||||
|
"foo": map[string]interface{}{
|
||||||
|
"bar": "buz",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestExampleInlineGroupInArray(t *testing.T) {
|
func TestExampleInlineGroupInArray(t *testing.T) {
|
||||||
tree, err := Load(`points = [{ x = 1, y = 2 }]`)
|
tree, err := Load(`points = [{ x = 1, y = 2 }]`)
|
||||||
assertTree(t, tree, err, map[string]interface{}{
|
assertTree(t, tree, err, map[string]interface{}{
|
||||||
@@ -546,21 +667,56 @@ func TestInlineTableUnterminated(t *testing.T) {
|
|||||||
|
|
||||||
func TestInlineTableCommaExpected(t *testing.T) {
|
func TestInlineTableCommaExpected(t *testing.T) {
|
||||||
_, err := Load("foo = {hello = 53 test = foo}")
|
_, err := Load("foo = {hello = 53 test = foo}")
|
||||||
if err.Error() != "(1, 19): comma expected between fields in inline table" {
|
if err.Error() != "(1, 19): unexpected token type in inline table: no value can start with t" {
|
||||||
t.Error("Bad error message:", err.Error())
|
t.Error("Bad error message:", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInlineTableCommaStart(t *testing.T) {
|
func TestInlineTableCommaStart(t *testing.T) {
|
||||||
_, err := Load("foo = {, hello = 53}")
|
_, err := Load("foo = {, hello = 53}")
|
||||||
if err.Error() != "(1, 8): inline table cannot start with a comma" {
|
if err.Error() != "(1, 8): unexpected token type in inline table: keys cannot contain , character" {
|
||||||
t.Error("Bad error message:", err.Error())
|
t.Error("Bad error message:", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInlineTableDoubleComma(t *testing.T) {
|
func TestInlineTableDoubleComma(t *testing.T) {
|
||||||
_, err := Load("foo = {hello = 53,, foo = 17}")
|
_, err := Load("foo = {hello = 53,, foo = 17}")
|
||||||
if err.Error() != "(1, 19): need field between two commas in inline table" {
|
if err.Error() != "(1, 19): unexpected token type in inline table: keys cannot contain , character" {
|
||||||
|
t.Error("Bad error message:", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInlineTableTrailingComma(t *testing.T) {
|
||||||
|
_, err := Load("foo = {hello = 53, foo = 17,}")
|
||||||
|
if err.Error() != "(1, 28): trailing comma at the end of inline table" {
|
||||||
|
t.Error("Bad error message:", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddKeyToInlineTable(t *testing.T) {
|
||||||
|
_, err := Load("type = { name = \"Nail\" }\ntype.edible = false")
|
||||||
|
if err.Error() != "(2, 1): could not add key or sub-table to exist inline table or its sub-table : type" {
|
||||||
|
t.Error("Bad error message:", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddSubTableToInlineTable(t *testing.T) {
|
||||||
|
_, err := Load("a = { b = \"c\" }\na.d.e = \"f\"")
|
||||||
|
if err.Error() != "(2, 1): could not add key or sub-table to exist inline table or its sub-table : a.d" {
|
||||||
|
t.Error("Bad error message:", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddKeyToSubTableOfInlineTable(t *testing.T) {
|
||||||
|
_, err := Load("a = { b = { c = \"d\" } }\na.b.e = \"f\"")
|
||||||
|
if err.Error() != "(2, 1): could not add key or sub-table to exist inline table or its sub-table : a.b" {
|
||||||
|
t.Error("Bad error message:", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReDefineInlineTable(t *testing.T) {
|
||||||
|
_, err := Load("a = { b = \"c\" }\n[a]\n d = \"e\"")
|
||||||
|
if err.Error() != "(2, 2): could not re-define exist inline table or its sub-table : a" {
|
||||||
t.Error("Bad error message:", err.Error())
|
t.Error("Bad error message:", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -652,6 +808,7 @@ func TestParseFile(t *testing.T) {
|
|||||||
[]string{"gamma", "delta"},
|
[]string{"gamma", "delta"},
|
||||||
[]int64{1, 2},
|
[]int64{1, 2},
|
||||||
},
|
},
|
||||||
|
"score": 4e-08,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -688,6 +845,7 @@ func TestParseFileCRLF(t *testing.T) {
|
|||||||
[]string{"gamma", "delta"},
|
[]string{"gamma", "delta"},
|
||||||
[]int64{1, 2},
|
[]int64{1, 2},
|
||||||
},
|
},
|
||||||
|
"score": 4e-08,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -760,13 +918,11 @@ func TestTomlValueStringRepresentation(t *testing.T) {
|
|||||||
{"hello world", "\"hello world\""},
|
{"hello world", "\"hello world\""},
|
||||||
{"\b\t\n\f\r\"\\", "\"\\b\\t\\n\\f\\r\\\"\\\\\""},
|
{"\b\t\n\f\r\"\\", "\"\\b\\t\\n\\f\\r\\\"\\\\\""},
|
||||||
{"\x05", "\"\\u0005\""},
|
{"\x05", "\"\\u0005\""},
|
||||||
{time.Date(1979, time.May, 27, 7, 32, 0, 0, time.UTC),
|
{time.Date(1979, time.May, 27, 7, 32, 0, 0, time.UTC), "1979-05-27T07:32:00Z"},
|
||||||
"1979-05-27T07:32:00Z"},
|
{[]interface{}{"gamma", "delta"}, "[\"gamma\", \"delta\"]"},
|
||||||
{[]interface{}{"gamma", "delta"},
|
|
||||||
"[\"gamma\",\"delta\"]"},
|
|
||||||
{nil, ""},
|
{nil, ""},
|
||||||
} {
|
} {
|
||||||
result, err := tomlValueStringRepresentation(item.Value, "", false)
|
result, err := tomlValueStringRepresentation(item.Value, "", "", OrderAlphabetical, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Test %d - unexpected error: %s", idx, err)
|
t.Errorf("Test %d - unexpected error: %s", idx, err)
|
||||||
}
|
}
|
||||||
@@ -893,7 +1049,7 @@ func TestInvalidFloatParsing(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_, err = Load("a=_1_2")
|
_, err = Load("a=_1_2")
|
||||||
if err.Error() != "(1, 3): cannot start number with underscore" {
|
if err.Error() != "(1, 3): no value can start with _" {
|
||||||
t.Error("Bad error message:", err.Error())
|
t.Error("Bad error message:", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -909,6 +1065,13 @@ func TestMapKeyIsNum(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInvalidKeyInlineTable(t *testing.T) {
|
||||||
|
_, err := Load("table={invalid..key = 1}")
|
||||||
|
if err.Error() != "(1, 8): invalid key: expecting key part after dot" {
|
||||||
|
t.Error("Bad error message:", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDottedKeys(t *testing.T) {
|
func TestDottedKeys(t *testing.T) {
|
||||||
tree, err := Load(`
|
tree, err := Load(`
|
||||||
name = "Orange"
|
name = "Orange"
|
||||||
@@ -937,3 +1100,39 @@ func TestInvalidDottedKeyEmptyGroup(t *testing.T) {
|
|||||||
t.Fatalf("invalid error message: %s", err)
|
t.Fatalf("invalid error message: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAccidentalNewlines(t *testing.T) {
|
||||||
|
expected := "The quick brown fox jumps over the lazy dog."
|
||||||
|
tree, err := Load(`str1 = "The quick brown fox jumps over the lazy dog."
|
||||||
|
|
||||||
|
str2 = """
|
||||||
|
The quick brown \
|
||||||
|
|
||||||
|
|
||||||
|
fox jumps over \
|
||||||
|
the lazy dog."""
|
||||||
|
|
||||||
|
str3 = """\
|
||||||
|
The quick brown \` + " " + `
|
||||||
|
fox jumps over \` + " " + `
|
||||||
|
the lazy dog.\` + " " + `
|
||||||
|
"""`)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got := tree.Get("str1")
|
||||||
|
if got != expected {
|
||||||
|
t.Errorf("expected '%s', got '%s'", expected, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
got = tree.Get("str2")
|
||||||
|
if got != expected {
|
||||||
|
t.Errorf("expected '%s', got '%s'", expected, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
got = tree.Get("str3")
|
||||||
|
if got != expected {
|
||||||
|
t.Errorf("expected '%s', got '%s'", expected, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+201
@@ -0,0 +1,201 @@
|
|||||||
|
# Query package
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
Package query performs JSONPath-like queries on a TOML document.
|
||||||
|
|
||||||
|
The query path implementation is based loosely on the JSONPath specification:
|
||||||
|
http://goessner.net/articles/JsonPath/.
|
||||||
|
|
||||||
|
The idea behind a query path is to allow quick access to any element, or set
|
||||||
|
of elements within TOML document, with a single expression.
|
||||||
|
|
||||||
|
```go
|
||||||
|
result, err := query.CompileAndExecute("$.foo.bar.baz", tree)
|
||||||
|
```
|
||||||
|
|
||||||
|
This is roughly equivalent to:
|
||||||
|
|
||||||
|
```go
|
||||||
|
next := tree.Get("foo")
|
||||||
|
if next != nil {
|
||||||
|
next = next.Get("bar")
|
||||||
|
if next != nil {
|
||||||
|
next = next.Get("baz")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result := next
|
||||||
|
```
|
||||||
|
|
||||||
|
err is nil if any parsing exception occurs.
|
||||||
|
|
||||||
|
If no node in the tree matches the query, result will simply contain an empty list of
|
||||||
|
items.
|
||||||
|
|
||||||
|
As illustrated above, the query path is much more efficient, especially since
|
||||||
|
the structure of the TOML file can vary. Rather than making assumptions about
|
||||||
|
a document's structure, a query allows the programmer to make structured
|
||||||
|
requests into the document, and get zero or more values as a result.
|
||||||
|
|
||||||
|
## Query syntax
|
||||||
|
|
||||||
|
The syntax of a query begins with a root token, followed by any number
|
||||||
|
sub-expressions:
|
||||||
|
|
||||||
|
```
|
||||||
|
$
|
||||||
|
Root of the TOML tree. This must always come first.
|
||||||
|
.name
|
||||||
|
Selects child of this node, where 'name' is a TOML key
|
||||||
|
name.
|
||||||
|
['name']
|
||||||
|
Selects child of this node, where 'name' is a string
|
||||||
|
containing a TOML key name.
|
||||||
|
[index]
|
||||||
|
Selcts child array element at 'index'.
|
||||||
|
..expr
|
||||||
|
Recursively selects all children, filtered by an a union,
|
||||||
|
index, or slice expression.
|
||||||
|
..*
|
||||||
|
Recursive selection of all nodes at this point in the
|
||||||
|
tree.
|
||||||
|
.*
|
||||||
|
Selects all children of the current node.
|
||||||
|
[expr,expr]
|
||||||
|
Union operator - a logical 'or' grouping of two or more
|
||||||
|
sub-expressions: index, key name, or filter.
|
||||||
|
[start:end:step]
|
||||||
|
Slice operator - selects array elements from start to
|
||||||
|
end-1, at the given step. All three arguments are
|
||||||
|
optional.
|
||||||
|
[?(filter)]
|
||||||
|
Named filter expression - the function 'filter' is
|
||||||
|
used to filter children at this node.
|
||||||
|
```
|
||||||
|
|
||||||
|
## Query Indexes And Slices
|
||||||
|
|
||||||
|
Index expressions perform no bounds checking, and will contribute no
|
||||||
|
values to the result set if the provided index or index range is invalid.
|
||||||
|
Negative indexes represent values from the end of the array, counting backwards.
|
||||||
|
|
||||||
|
```go
|
||||||
|
// select the last index of the array named 'foo'
|
||||||
|
query.CompileAndExecute("$.foo[-1]", tree)
|
||||||
|
```
|
||||||
|
|
||||||
|
Slice expressions are supported, by using ':' to separate a start/end index pair.
|
||||||
|
|
||||||
|
```go
|
||||||
|
// select up to the first five elements in the array
|
||||||
|
query.CompileAndExecute("$.foo[0:5]", tree)
|
||||||
|
```
|
||||||
|
|
||||||
|
Slice expressions also allow negative indexes for the start and stop
|
||||||
|
arguments.
|
||||||
|
|
||||||
|
```go
|
||||||
|
// select all array elements except the last one.
|
||||||
|
query.CompileAndExecute("$.foo[0:-1]", tree)
|
||||||
|
```
|
||||||
|
|
||||||
|
Slice expressions may have an optional stride/step parameter:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// select every other element
|
||||||
|
query.CompileAndExecute("$.foo[0::2]", tree)
|
||||||
|
```
|
||||||
|
|
||||||
|
Slice start and end parameters are also optional:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// these are all equivalent and select all the values in the array
|
||||||
|
query.CompileAndExecute("$.foo[:]", tree)
|
||||||
|
query.CompileAndExecute("$.foo[::]", tree)
|
||||||
|
query.CompileAndExecute("$.foo[::1]", tree)
|
||||||
|
query.CompileAndExecute("$.foo[0:]", tree)
|
||||||
|
query.CompileAndExecute("$.foo[0::]", tree)
|
||||||
|
query.CompileAndExecute("$.foo[0::1]", tree)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Query Filters
|
||||||
|
|
||||||
|
Query filters are used within a Union [,] or single Filter [] expression.
|
||||||
|
A filter only allows nodes that qualify through to the next expression,
|
||||||
|
and/or into the result set.
|
||||||
|
|
||||||
|
```go
|
||||||
|
// returns children of foo that are permitted by the 'bar' filter.
|
||||||
|
query.CompileAndExecute("$.foo[?(bar)]", tree)
|
||||||
|
```
|
||||||
|
|
||||||
|
There are several filters provided with the library:
|
||||||
|
|
||||||
|
```
|
||||||
|
tree
|
||||||
|
Allows nodes of type Tree.
|
||||||
|
int
|
||||||
|
Allows nodes of type int64.
|
||||||
|
float
|
||||||
|
Allows nodes of type float64.
|
||||||
|
string
|
||||||
|
Allows nodes of type string.
|
||||||
|
time
|
||||||
|
Allows nodes of type time.Time.
|
||||||
|
bool
|
||||||
|
Allows nodes of type bool.
|
||||||
|
```
|
||||||
|
|
||||||
|
## Query Results
|
||||||
|
|
||||||
|
An executed query returns a Result object. This contains the nodes
|
||||||
|
in the TOML tree that qualify the query expression. Position information
|
||||||
|
is also available for each value in the set.
|
||||||
|
|
||||||
|
```go
|
||||||
|
// display the results of a query
|
||||||
|
results := query.CompileAndExecute("$.foo.bar.baz", tree)
|
||||||
|
for idx, value := results.Values() {
|
||||||
|
fmt.Println("%v: %v", results.Positions()[idx], value)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Compiled Queries
|
||||||
|
|
||||||
|
Queries may be executed directly on a Tree object, or compiled ahead
|
||||||
|
of time and executed discretely. The former is more convenient, but has the
|
||||||
|
penalty of having to recompile the query expression each time.
|
||||||
|
|
||||||
|
```go
|
||||||
|
// basic query
|
||||||
|
results := query.CompileAndExecute("$.foo.bar.baz", tree)
|
||||||
|
|
||||||
|
// compiled query
|
||||||
|
query, err := toml.Compile("$.foo.bar.baz")
|
||||||
|
results := query.Execute(tree)
|
||||||
|
|
||||||
|
// run the compiled query again on a different tree
|
||||||
|
moreResults := query.Execute(anotherTree)
|
||||||
|
```
|
||||||
|
|
||||||
|
## User Defined Query Filters
|
||||||
|
|
||||||
|
Filter expressions may also be user defined by using the SetFilter()
|
||||||
|
function on the Query object. The function must return true/false, which
|
||||||
|
signifies if the passed node is kept or discarded, respectively.
|
||||||
|
|
||||||
|
```go
|
||||||
|
// create a query that references a user-defined filter
|
||||||
|
query, _ := query.Compile("$[?(bazOnly)]")
|
||||||
|
|
||||||
|
// define the filter, and assign it to the query
|
||||||
|
query.SetFilter("bazOnly", func(node interface{}) bool{
|
||||||
|
if tree, ok := node.(*Tree); ok {
|
||||||
|
return tree.Has("baz")
|
||||||
|
}
|
||||||
|
return false // reject all other node types
|
||||||
|
})
|
||||||
|
|
||||||
|
// run the query
|
||||||
|
query.Execute(tree)
|
||||||
|
```
|
||||||
+5
-7
@@ -80,25 +80,23 @@
|
|||||||
// Slice expressions also allow negative indexes for the start and stop
|
// Slice expressions also allow negative indexes for the start and stop
|
||||||
// arguments.
|
// arguments.
|
||||||
//
|
//
|
||||||
// // select all array elements.
|
// // select all array elements except the last one.
|
||||||
// query.CompileAndExecute("$.foo[0:-1]", tree)
|
// query.CompileAndExecute("$.foo[0:-1]", tree)
|
||||||
//
|
//
|
||||||
// Slice expressions may have an optional stride/step parameter:
|
// Slice expressions may have an optional stride/step parameter:
|
||||||
//
|
//
|
||||||
// // select every other element
|
// // select every other element
|
||||||
// query.CompileAndExecute("$.foo[0:-1:2]", tree)
|
// query.CompileAndExecute("$.foo[0::2]", tree)
|
||||||
//
|
//
|
||||||
// Slice start and end parameters are also optional:
|
// Slice start and end parameters are also optional:
|
||||||
//
|
//
|
||||||
// // these are all equivalent and select all the values in the array
|
// // these are all equivalent and select all the values in the array
|
||||||
// query.CompileAndExecute("$.foo[:]", tree)
|
// query.CompileAndExecute("$.foo[:]", tree)
|
||||||
// query.CompileAndExecute("$.foo[0:]", tree)
|
// query.CompileAndExecute("$.foo[::]", tree)
|
||||||
// query.CompileAndExecute("$.foo[:-1]", tree)
|
|
||||||
// query.CompileAndExecute("$.foo[0:-1:]", tree)
|
|
||||||
// query.CompileAndExecute("$.foo[::1]", tree)
|
// query.CompileAndExecute("$.foo[::1]", tree)
|
||||||
|
// query.CompileAndExecute("$.foo[0:]", tree)
|
||||||
|
// query.CompileAndExecute("$.foo[0::]", tree)
|
||||||
// query.CompileAndExecute("$.foo[0::1]", tree)
|
// query.CompileAndExecute("$.foo[0::1]", tree)
|
||||||
// query.CompileAndExecute("$.foo[:-1:1]", tree)
|
|
||||||
// query.CompileAndExecute("$.foo[0:-1:1]", tree)
|
|
||||||
//
|
//
|
||||||
// Query Filters
|
// Query Filters
|
||||||
//
|
//
|
||||||
|
|||||||
+114
-35
@@ -2,6 +2,8 @@ package query
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/pelletier/go-toml"
|
"github.com/pelletier/go-toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -44,16 +46,16 @@ func newMatchKeyFn(name string) *matchKeyFn {
|
|||||||
func (f *matchKeyFn) call(node interface{}, ctx *queryContext) {
|
func (f *matchKeyFn) call(node interface{}, ctx *queryContext) {
|
||||||
if array, ok := node.([]*toml.Tree); ok {
|
if array, ok := node.([]*toml.Tree); ok {
|
||||||
for _, tree := range array {
|
for _, tree := range array {
|
||||||
item := tree.Get(f.Name)
|
item := tree.GetPath([]string{f.Name})
|
||||||
if item != nil {
|
if item != nil {
|
||||||
ctx.lastPosition = tree.GetPosition(f.Name)
|
ctx.lastPosition = tree.GetPositionPath([]string{f.Name})
|
||||||
f.next.call(item, ctx)
|
f.next.call(item, ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if tree, ok := node.(*toml.Tree); ok {
|
} else if tree, ok := node.(*toml.Tree); ok {
|
||||||
item := tree.Get(f.Name)
|
item := tree.GetPath([]string{f.Name})
|
||||||
if item != nil {
|
if item != nil {
|
||||||
ctx.lastPosition = tree.GetPosition(f.Name)
|
ctx.lastPosition = tree.GetPositionPath([]string{f.Name})
|
||||||
f.next.call(item, ctx)
|
f.next.call(item, ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -70,51 +72,128 @@ func newMatchIndexFn(idx int) *matchIndexFn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *matchIndexFn) call(node interface{}, ctx *queryContext) {
|
func (f *matchIndexFn) call(node interface{}, ctx *queryContext) {
|
||||||
if arr, ok := node.([]interface{}); ok {
|
v := reflect.ValueOf(node)
|
||||||
if f.Idx < len(arr) && f.Idx >= 0 {
|
if v.Kind() == reflect.Slice {
|
||||||
|
if v.Len() == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Manage negative values
|
||||||
|
idx := f.Idx
|
||||||
|
if idx < 0 {
|
||||||
|
idx += v.Len()
|
||||||
|
}
|
||||||
|
if 0 <= idx && idx < v.Len() {
|
||||||
|
callNextIndexSlice(f.next, node, ctx, v.Index(idx).Interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func callNextIndexSlice(next pathFn, node interface{}, ctx *queryContext, value interface{}) {
|
||||||
if treesArray, ok := node.([]*toml.Tree); ok {
|
if treesArray, ok := node.([]*toml.Tree); ok {
|
||||||
if len(treesArray) > 0 {
|
|
||||||
ctx.lastPosition = treesArray[0].Position()
|
ctx.lastPosition = treesArray[0].Position()
|
||||||
}
|
}
|
||||||
}
|
next.call(value, ctx)
|
||||||
f.next.call(arr[f.Idx], ctx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter by slicing
|
// filter by slicing
|
||||||
type matchSliceFn struct {
|
type matchSliceFn struct {
|
||||||
matchBase
|
matchBase
|
||||||
Start, End, Step int
|
Start, End, Step *int
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMatchSliceFn(start, end, step int) *matchSliceFn {
|
func newMatchSliceFn() *matchSliceFn {
|
||||||
return &matchSliceFn{Start: start, End: end, Step: step}
|
return &matchSliceFn{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *matchSliceFn) setStart(start int) *matchSliceFn {
|
||||||
|
f.Start = &start
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *matchSliceFn) setEnd(end int) *matchSliceFn {
|
||||||
|
f.End = &end
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *matchSliceFn) setStep(step int) *matchSliceFn {
|
||||||
|
f.Step = &step
|
||||||
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *matchSliceFn) call(node interface{}, ctx *queryContext) {
|
func (f *matchSliceFn) call(node interface{}, ctx *queryContext) {
|
||||||
if arr, ok := node.([]interface{}); ok {
|
v := reflect.ValueOf(node)
|
||||||
// adjust indexes for negative values, reverse ordering
|
if v.Kind() == reflect.Slice {
|
||||||
realStart, realEnd := f.Start, f.End
|
if v.Len() == 0 {
|
||||||
if realStart < 0 {
|
return
|
||||||
realStart = len(arr) + realStart
|
|
||||||
}
|
}
|
||||||
if realEnd < 0 {
|
|
||||||
realEnd = len(arr) + realEnd
|
var start, end, step int
|
||||||
|
|
||||||
|
// Initialize step
|
||||||
|
if f.Step != nil {
|
||||||
|
step = *f.Step
|
||||||
|
} else {
|
||||||
|
step = 1
|
||||||
}
|
}
|
||||||
if realEnd < realStart {
|
|
||||||
realEnd, realStart = realStart, realEnd // swap
|
// Initialize start
|
||||||
|
if f.Start != nil {
|
||||||
|
start = *f.Start
|
||||||
|
// Manage negative values
|
||||||
|
if start < 0 {
|
||||||
|
start += v.Len()
|
||||||
}
|
}
|
||||||
// loop and gather
|
// Manage out of range values
|
||||||
for idx := realStart; idx < realEnd; idx += f.Step {
|
start = max(start, 0)
|
||||||
if treesArray, ok := node.([]*toml.Tree); ok {
|
start = min(start, v.Len()-1)
|
||||||
if len(treesArray) > 0 {
|
} else if step > 0 {
|
||||||
ctx.lastPosition = treesArray[0].Position()
|
start = 0
|
||||||
|
} else {
|
||||||
|
start = v.Len() - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize end
|
||||||
|
if f.End != nil {
|
||||||
|
end = *f.End
|
||||||
|
// Manage negative values
|
||||||
|
if end < 0 {
|
||||||
|
end += v.Len()
|
||||||
|
}
|
||||||
|
// Manage out of range values
|
||||||
|
end = max(end, -1)
|
||||||
|
end = min(end, v.Len())
|
||||||
|
} else if step > 0 {
|
||||||
|
end = v.Len()
|
||||||
|
} else {
|
||||||
|
end = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop on values
|
||||||
|
if step > 0 {
|
||||||
|
for idx := start; idx < end; idx += step {
|
||||||
|
callNextIndexSlice(f.next, node, ctx, v.Index(idx).Interface())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for idx := start; idx > end; idx += step {
|
||||||
|
callNextIndexSlice(f.next, node, ctx, v.Index(idx).Interface())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f.next.call(arr[idx], ctx)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func min(a, b int) int {
|
||||||
|
if a < b {
|
||||||
|
return a
|
||||||
}
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func max(a, b int) int {
|
||||||
|
if a > b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// match anything
|
// match anything
|
||||||
@@ -129,8 +208,8 @@ func newMatchAnyFn() *matchAnyFn {
|
|||||||
func (f *matchAnyFn) call(node interface{}, ctx *queryContext) {
|
func (f *matchAnyFn) call(node interface{}, ctx *queryContext) {
|
||||||
if tree, ok := node.(*toml.Tree); ok {
|
if tree, ok := node.(*toml.Tree); ok {
|
||||||
for _, k := range tree.Keys() {
|
for _, k := range tree.Keys() {
|
||||||
v := tree.Get(k)
|
v := tree.GetPath([]string{k})
|
||||||
ctx.lastPosition = tree.GetPosition(k)
|
ctx.lastPosition = tree.GetPositionPath([]string{k})
|
||||||
f.next.call(v, ctx)
|
f.next.call(v, ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -168,8 +247,8 @@ func (f *matchRecursiveFn) call(node interface{}, ctx *queryContext) {
|
|||||||
var visit func(tree *toml.Tree)
|
var visit func(tree *toml.Tree)
|
||||||
visit = func(tree *toml.Tree) {
|
visit = func(tree *toml.Tree) {
|
||||||
for _, k := range tree.Keys() {
|
for _, k := range tree.Keys() {
|
||||||
v := tree.Get(k)
|
v := tree.GetPath([]string{k})
|
||||||
ctx.lastPosition = tree.GetPosition(k)
|
ctx.lastPosition = tree.GetPositionPath([]string{k})
|
||||||
f.next.call(v, ctx)
|
f.next.call(v, ctx)
|
||||||
switch node := v.(type) {
|
switch node := v.(type) {
|
||||||
case *toml.Tree:
|
case *toml.Tree:
|
||||||
@@ -207,9 +286,9 @@ func (f *matchFilterFn) call(node interface{}, ctx *queryContext) {
|
|||||||
switch castNode := node.(type) {
|
switch castNode := node.(type) {
|
||||||
case *toml.Tree:
|
case *toml.Tree:
|
||||||
for _, k := range castNode.Keys() {
|
for _, k := range castNode.Keys() {
|
||||||
v := castNode.Get(k)
|
v := castNode.GetPath([]string{k})
|
||||||
if fn(v) {
|
if fn(v) {
|
||||||
ctx.lastPosition = castNode.GetPosition(k)
|
ctx.lastPosition = castNode.GetPositionPath([]string{k})
|
||||||
f.next.call(v, ctx)
|
f.next.call(v, ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-10
@@ -2,8 +2,10 @@ package query
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pelletier/go-toml"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/pelletier/go-toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
// dump path tree to a string
|
// dump path tree to a string
|
||||||
@@ -19,8 +21,17 @@ func pathString(root pathFn) string {
|
|||||||
result += fmt.Sprintf("{%d}", fn.Idx)
|
result += fmt.Sprintf("{%d}", fn.Idx)
|
||||||
result += pathString(fn.next)
|
result += pathString(fn.next)
|
||||||
case *matchSliceFn:
|
case *matchSliceFn:
|
||||||
result += fmt.Sprintf("{%d:%d:%d}",
|
startString, endString, stepString := "nil", "nil", "nil"
|
||||||
fn.Start, fn.End, fn.Step)
|
if fn.Start != nil {
|
||||||
|
startString = strconv.Itoa(*fn.Start)
|
||||||
|
}
|
||||||
|
if fn.End != nil {
|
||||||
|
endString = strconv.Itoa(*fn.End)
|
||||||
|
}
|
||||||
|
if fn.Step != nil {
|
||||||
|
stepString = strconv.Itoa(*fn.Step)
|
||||||
|
}
|
||||||
|
result += fmt.Sprintf("{%s:%s:%s}", startString, endString, stepString)
|
||||||
result += pathString(fn.next)
|
result += pathString(fn.next)
|
||||||
case *matchAnyFn:
|
case *matchAnyFn:
|
||||||
result += "{}"
|
result += "{}"
|
||||||
@@ -110,7 +121,7 @@ func TestPathSliceStart(t *testing.T) {
|
|||||||
assertPath(t,
|
assertPath(t,
|
||||||
"$[123:]",
|
"$[123:]",
|
||||||
buildPath(
|
buildPath(
|
||||||
newMatchSliceFn(123, maxInt, 1),
|
newMatchSliceFn().setStart(123),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,7 +129,7 @@ func TestPathSliceStartEnd(t *testing.T) {
|
|||||||
assertPath(t,
|
assertPath(t,
|
||||||
"$[123:456]",
|
"$[123:456]",
|
||||||
buildPath(
|
buildPath(
|
||||||
newMatchSliceFn(123, 456, 1),
|
newMatchSliceFn().setStart(123).setEnd(456),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +137,7 @@ func TestPathSliceStartEndColon(t *testing.T) {
|
|||||||
assertPath(t,
|
assertPath(t,
|
||||||
"$[123:456:]",
|
"$[123:456:]",
|
||||||
buildPath(
|
buildPath(
|
||||||
newMatchSliceFn(123, 456, 1),
|
newMatchSliceFn().setStart(123).setEnd(456),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +145,7 @@ func TestPathSliceStartStep(t *testing.T) {
|
|||||||
assertPath(t,
|
assertPath(t,
|
||||||
"$[123::7]",
|
"$[123::7]",
|
||||||
buildPath(
|
buildPath(
|
||||||
newMatchSliceFn(123, maxInt, 7),
|
newMatchSliceFn().setStart(123).setStep(7),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +153,7 @@ func TestPathSliceEndStep(t *testing.T) {
|
|||||||
assertPath(t,
|
assertPath(t,
|
||||||
"$[:456:7]",
|
"$[:456:7]",
|
||||||
buildPath(
|
buildPath(
|
||||||
newMatchSliceFn(0, 456, 7),
|
newMatchSliceFn().setEnd(456).setStep(7),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,7 +161,7 @@ func TestPathSliceStep(t *testing.T) {
|
|||||||
assertPath(t,
|
assertPath(t,
|
||||||
"$[::7]",
|
"$[::7]",
|
||||||
buildPath(
|
buildPath(
|
||||||
newMatchSliceFn(0, maxInt, 7),
|
newMatchSliceFn().setStep(7),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,7 +169,7 @@ func TestPathSliceAll(t *testing.T) {
|
|||||||
assertPath(t,
|
assertPath(t,
|
||||||
"$[123:456:7]",
|
"$[123:456:7]",
|
||||||
buildPath(
|
buildPath(
|
||||||
newMatchSliceFn(123, 456, 7),
|
newMatchSliceFn().setStart(123).setEnd(456).setStep(7),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-8
@@ -203,12 +203,13 @@ loop: // labeled loop for easy breaking
|
|||||||
|
|
||||||
func (p *queryParser) parseSliceExpr() queryParserStateFn {
|
func (p *queryParser) parseSliceExpr() queryParserStateFn {
|
||||||
// init slice to grab all elements
|
// init slice to grab all elements
|
||||||
start, end, step := 0, maxInt, 1
|
var start, end, step *int = nil, nil, nil
|
||||||
|
|
||||||
// parse optional start
|
// parse optional start
|
||||||
tok := p.getToken()
|
tok := p.getToken()
|
||||||
if tok.typ == tokenInteger {
|
if tok.typ == tokenInteger {
|
||||||
start = tok.Int()
|
v := tok.Int()
|
||||||
|
start = &v
|
||||||
tok = p.getToken()
|
tok = p.getToken()
|
||||||
}
|
}
|
||||||
if tok.typ != tokenColon {
|
if tok.typ != tokenColon {
|
||||||
@@ -218,11 +219,12 @@ func (p *queryParser) parseSliceExpr() queryParserStateFn {
|
|||||||
// parse optional end
|
// parse optional end
|
||||||
tok = p.getToken()
|
tok = p.getToken()
|
||||||
if tok.typ == tokenInteger {
|
if tok.typ == tokenInteger {
|
||||||
end = tok.Int()
|
v := tok.Int()
|
||||||
|
end = &v
|
||||||
tok = p.getToken()
|
tok = p.getToken()
|
||||||
}
|
}
|
||||||
if tok.typ == tokenRightBracket {
|
if tok.typ == tokenRightBracket {
|
||||||
p.query.appendPath(newMatchSliceFn(start, end, step))
|
p.query.appendPath(&matchSliceFn{Start: start, End: end, Step: step})
|
||||||
return p.parseMatchExpr
|
return p.parseMatchExpr
|
||||||
}
|
}
|
||||||
if tok.typ != tokenColon {
|
if tok.typ != tokenColon {
|
||||||
@@ -232,17 +234,18 @@ func (p *queryParser) parseSliceExpr() queryParserStateFn {
|
|||||||
// parse optional step
|
// parse optional step
|
||||||
tok = p.getToken()
|
tok = p.getToken()
|
||||||
if tok.typ == tokenInteger {
|
if tok.typ == tokenInteger {
|
||||||
step = tok.Int()
|
v := tok.Int()
|
||||||
if step < 0 {
|
if v == 0 {
|
||||||
return p.parseError(tok, "step must be a positive value")
|
return p.parseError(tok, "step cannot be zero")
|
||||||
}
|
}
|
||||||
|
step = &v
|
||||||
tok = p.getToken()
|
tok = p.getToken()
|
||||||
}
|
}
|
||||||
if tok.typ != tokenRightBracket {
|
if tok.typ != tokenRightBracket {
|
||||||
return p.parseError(tok, "expected ']'")
|
return p.parseError(tok, "expected ']'")
|
||||||
}
|
}
|
||||||
|
|
||||||
p.query.appendPath(newMatchSliceFn(start, end, step))
|
p.query.appendPath(&matchSliceFn{Start: start, End: end, Step: step})
|
||||||
return p.parseMatchExpr
|
return p.parseMatchExpr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+226
-95
@@ -78,6 +78,19 @@ func assertValue(t *testing.T, result, ref interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func assertParseError(t *testing.T, query string, errString string) {
|
||||||
|
_, err := Compile(query)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("error should be non-nil")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err.Error() != errString {
|
||||||
|
t.Errorf("error does not match")
|
||||||
|
t.Log("test:", err.Error())
|
||||||
|
t.Log("ref: ", errString)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func assertQueryPositions(t *testing.T, tomlDoc string, query string, ref []interface{}) {
|
func assertQueryPositions(t *testing.T, tomlDoc string, query string, ref []interface{}) {
|
||||||
tree, err := toml.Load(tomlDoc)
|
tree, err := toml.Load(tomlDoc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -128,54 +141,213 @@ func TestQueryKeyString(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestQueryIndex(t *testing.T) {
|
func TestQueryKeyUnicodeString(t *testing.T) {
|
||||||
assertQueryPositions(t,
|
assertQueryPositions(t,
|
||||||
"[foo]\na = [1,2,3,4,5,6,7,8,9,0]",
|
"['f𝟘.o']\na = 42",
|
||||||
"$.foo.a[5]",
|
"$['f𝟘.o']['a']",
|
||||||
[]interface{}{
|
[]interface{}{
|
||||||
queryTestNode{
|
queryTestNode{
|
||||||
int64(6), toml.Position{2, 1},
|
int64(42), toml.Position{2, 1},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestQueryIndexError1(t *testing.T) {
|
||||||
|
assertParseError(t, "$.foo.a[5", "(1, 10): expected ',' or ']', not ''")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQueryIndexError2(t *testing.T) {
|
||||||
|
assertParseError(t, "$.foo.a[]", "(1, 9): expected union sub expression, not ']', 0")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQueryIndex(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[foo]\na = [0,1,2,3,4,5,6,7,8,9]",
|
||||||
|
"$.foo.a[5]",
|
||||||
|
[]interface{}{
|
||||||
|
queryTestNode{int64(5), toml.Position{2, 1}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQueryIndexNegative(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[foo]\na = [0,1,2,3,4,5,6,7,8,9]",
|
||||||
|
"$.foo.a[-2]",
|
||||||
|
[]interface{}{
|
||||||
|
queryTestNode{int64(8), toml.Position{2, 1}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQueryIndexWrong(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[foo]\na = [0,1,2,3,4,5,6,7,8,9]",
|
||||||
|
"$.foo.a[99]",
|
||||||
|
[]interface{}{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQueryIndexEmpty(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[foo]\na = []",
|
||||||
|
"$.foo.a[5]",
|
||||||
|
[]interface{}{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQueryIndexTree(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[[foo]]\na = [0,1,2,3,4,5,6,7,8,9]\n[[foo]]\nb = 3",
|
||||||
|
"$.foo[1].b",
|
||||||
|
[]interface{}{
|
||||||
|
queryTestNode{int64(3), toml.Position{4, 1}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuerySliceError1(t *testing.T) {
|
||||||
|
assertParseError(t, "$.foo.a[3:?]", "(1, 11): expected ']' or ':'")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuerySliceError2(t *testing.T) {
|
||||||
|
assertParseError(t, "$.foo.a[:::]", "(1, 11): expected ']'")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuerySliceError3(t *testing.T) {
|
||||||
|
assertParseError(t, "$.foo.a[::0]", "(1, 11): step cannot be zero")
|
||||||
|
}
|
||||||
|
|
||||||
func TestQuerySliceRange(t *testing.T) {
|
func TestQuerySliceRange(t *testing.T) {
|
||||||
assertQueryPositions(t,
|
assertQueryPositions(t,
|
||||||
"[foo]\na = [1,2,3,4,5,6,7,8,9,0]",
|
"[foo]\na = [0,1,2,3,4,5,6,7,8,9]",
|
||||||
"$.foo.a[0:5]",
|
"$.foo.a[:5]",
|
||||||
[]interface{}{
|
[]interface{}{
|
||||||
queryTestNode{
|
queryTestNode{int64(0), toml.Position{2, 1}},
|
||||||
int64(1), toml.Position{2, 1},
|
queryTestNode{int64(1), toml.Position{2, 1}},
|
||||||
},
|
queryTestNode{int64(2), toml.Position{2, 1}},
|
||||||
queryTestNode{
|
queryTestNode{int64(3), toml.Position{2, 1}},
|
||||||
int64(2), toml.Position{2, 1},
|
queryTestNode{int64(4), toml.Position{2, 1}},
|
||||||
},
|
|
||||||
queryTestNode{
|
|
||||||
int64(3), toml.Position{2, 1},
|
|
||||||
},
|
|
||||||
queryTestNode{
|
|
||||||
int64(4), toml.Position{2, 1},
|
|
||||||
},
|
|
||||||
queryTestNode{
|
|
||||||
int64(5), toml.Position{2, 1},
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestQuerySliceStep(t *testing.T) {
|
func TestQuerySliceStep(t *testing.T) {
|
||||||
assertQueryPositions(t,
|
assertQueryPositions(t,
|
||||||
"[foo]\na = [1,2,3,4,5,6,7,8,9,0]",
|
"[foo]\na = [0,1,2,3,4,5,6,7,8,9]",
|
||||||
"$.foo.a[0:5:2]",
|
"$.foo.a[0:5:2]",
|
||||||
|
[]interface{}{
|
||||||
|
queryTestNode{int64(0), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(2), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(4), toml.Position{2, 1}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuerySliceStartNegative(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[foo]\na = [0,1,2,3,4,5,6,7,8,9]",
|
||||||
|
"$.foo.a[-3:]",
|
||||||
|
[]interface{}{
|
||||||
|
queryTestNode{int64(7), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(8), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(9), toml.Position{2, 1}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuerySliceEndNegative(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[foo]\na = [0,1,2,3,4,5,6,7,8,9]",
|
||||||
|
"$.foo.a[:-6]",
|
||||||
|
[]interface{}{
|
||||||
|
queryTestNode{int64(0), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(1), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(2), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(3), toml.Position{2, 1}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuerySliceStepNegative(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[foo]\na = [0,1,2,3,4,5,6,7,8,9]",
|
||||||
|
"$.foo.a[::-2]",
|
||||||
|
[]interface{}{
|
||||||
|
queryTestNode{int64(9), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(7), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(5), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(3), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(1), toml.Position{2, 1}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuerySliceStartOverRange(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[foo]\na = [0,1,2,3,4,5,6,7,8,9]",
|
||||||
|
"$.foo.a[-99:3]",
|
||||||
|
[]interface{}{
|
||||||
|
queryTestNode{int64(0), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(1), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(2), toml.Position{2, 1}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuerySliceStartOverRangeNegative(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[foo]\na = [0,1,2,3,4,5,6,7,8,9]",
|
||||||
|
"$.foo.a[99:7:-1]",
|
||||||
|
[]interface{}{
|
||||||
|
queryTestNode{int64(9), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(8), toml.Position{2, 1}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuerySliceEndOverRange(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[foo]\na = [0,1,2,3,4,5,6,7,8,9]",
|
||||||
|
"$.foo.a[7:99]",
|
||||||
|
[]interface{}{
|
||||||
|
queryTestNode{int64(7), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(8), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(9), toml.Position{2, 1}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuerySliceEndOverRangeNegative(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[foo]\na = [0,1,2,3,4,5,6,7,8,9]",
|
||||||
|
"$.foo.a[2:-99:-1]",
|
||||||
|
[]interface{}{
|
||||||
|
queryTestNode{int64(2), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(1), toml.Position{2, 1}},
|
||||||
|
queryTestNode{int64(0), toml.Position{2, 1}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuerySliceWrongRange(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[foo]\na = [0,1,2,3,4,5,6,7,8,9]",
|
||||||
|
"$.foo.a[5:3]",
|
||||||
|
[]interface{}{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuerySliceWrongRangeNegative(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[foo]\na = [0,1,2,3,4,5,6,7,8,9]",
|
||||||
|
"$.foo.a[3:5:-1]",
|
||||||
|
[]interface{}{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuerySliceEmpty(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[foo]\na = []",
|
||||||
|
"$.foo.a[5:]",
|
||||||
|
[]interface{}{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuerySliceTree(t *testing.T) {
|
||||||
|
assertQueryPositions(t,
|
||||||
|
"[[foo]]\na='nok'\n[[foo]]\na = [0,1,2,3,4,5,6,7,8,9]\n[[foo]]\na='ok'\nb = 3",
|
||||||
|
"$.foo[1:].a",
|
||||||
[]interface{}{
|
[]interface{}{
|
||||||
queryTestNode{
|
queryTestNode{
|
||||||
int64(1), toml.Position{2, 1},
|
[]interface{}{
|
||||||
},
|
int64(0), int64(1), int64(2), int64(3), int64(4),
|
||||||
queryTestNode{
|
int64(5), int64(6), int64(7), int64(8), int64(9)},
|
||||||
int64(3), toml.Position{2, 1},
|
toml.Position{4, 1}},
|
||||||
},
|
queryTestNode{"ok", toml.Position{6, 1}},
|
||||||
queryTestNode{
|
|
||||||
int64(5), toml.Position{2, 1},
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -265,12 +437,8 @@ func TestQueryRecursionAll(t *testing.T) {
|
|||||||
"b": int64(2),
|
"b": int64(2),
|
||||||
}, toml.Position{1, 1},
|
}, toml.Position{1, 1},
|
||||||
},
|
},
|
||||||
queryTestNode{
|
queryTestNode{int64(1), toml.Position{2, 1}},
|
||||||
int64(1), toml.Position{2, 1},
|
queryTestNode{int64(2), toml.Position{3, 1}},
|
||||||
},
|
|
||||||
queryTestNode{
|
|
||||||
int64(2), toml.Position{3, 1},
|
|
||||||
},
|
|
||||||
queryTestNode{
|
queryTestNode{
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"foo": map[string]interface{}{
|
"foo": map[string]interface{}{
|
||||||
@@ -285,12 +453,8 @@ func TestQueryRecursionAll(t *testing.T) {
|
|||||||
"b": int64(4),
|
"b": int64(4),
|
||||||
}, toml.Position{4, 1},
|
}, toml.Position{4, 1},
|
||||||
},
|
},
|
||||||
queryTestNode{
|
queryTestNode{int64(3), toml.Position{5, 1}},
|
||||||
int64(3), toml.Position{5, 1},
|
queryTestNode{int64(4), toml.Position{6, 1}},
|
||||||
},
|
|
||||||
queryTestNode{
|
|
||||||
int64(4), toml.Position{6, 1},
|
|
||||||
},
|
|
||||||
queryTestNode{
|
queryTestNode{
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"foo": map[string]interface{}{
|
"foo": map[string]interface{}{
|
||||||
@@ -305,12 +469,8 @@ func TestQueryRecursionAll(t *testing.T) {
|
|||||||
"b": int64(6),
|
"b": int64(6),
|
||||||
}, toml.Position{7, 1},
|
}, toml.Position{7, 1},
|
||||||
},
|
},
|
||||||
queryTestNode{
|
queryTestNode{int64(5), toml.Position{8, 1}},
|
||||||
int64(5), toml.Position{8, 1},
|
queryTestNode{int64(6), toml.Position{9, 1}},
|
||||||
},
|
|
||||||
queryTestNode{
|
|
||||||
int64(6), toml.Position{9, 1},
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -358,56 +518,30 @@ func TestQueryFilterFn(t *testing.T) {
|
|||||||
assertQueryPositions(t, string(buff),
|
assertQueryPositions(t, string(buff),
|
||||||
"$..[?(int)]",
|
"$..[?(int)]",
|
||||||
[]interface{}{
|
[]interface{}{
|
||||||
queryTestNode{
|
queryTestNode{int64(8001), toml.Position{13, 1}},
|
||||||
int64(8001), toml.Position{13, 1},
|
queryTestNode{int64(8001), toml.Position{13, 1}},
|
||||||
},
|
queryTestNode{int64(8002), toml.Position{13, 1}},
|
||||||
queryTestNode{
|
queryTestNode{int64(5000), toml.Position{14, 1}},
|
||||||
int64(8001), toml.Position{13, 1},
|
|
||||||
},
|
|
||||||
queryTestNode{
|
|
||||||
int64(8002), toml.Position{13, 1},
|
|
||||||
},
|
|
||||||
queryTestNode{
|
|
||||||
int64(5000), toml.Position{14, 1},
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
assertQueryPositions(t, string(buff),
|
assertQueryPositions(t, string(buff),
|
||||||
"$..[?(string)]",
|
"$..[?(string)]",
|
||||||
[]interface{}{
|
[]interface{}{
|
||||||
queryTestNode{
|
queryTestNode{"TOML Example", toml.Position{3, 1}},
|
||||||
"TOML Example", toml.Position{3, 1},
|
queryTestNode{"Tom Preston-Werner", toml.Position{6, 1}},
|
||||||
},
|
queryTestNode{"GitHub", toml.Position{7, 1}},
|
||||||
queryTestNode{
|
queryTestNode{"GitHub Cofounder & CEO\nLikes tater tots and beer.", toml.Position{8, 1}},
|
||||||
"Tom Preston-Werner", toml.Position{6, 1},
|
queryTestNode{"192.168.1.1", toml.Position{12, 1}},
|
||||||
},
|
queryTestNode{"10.0.0.1", toml.Position{21, 3}},
|
||||||
queryTestNode{
|
queryTestNode{"eqdc10", toml.Position{22, 3}},
|
||||||
"GitHub", toml.Position{7, 1},
|
queryTestNode{"10.0.0.2", toml.Position{25, 3}},
|
||||||
},
|
queryTestNode{"eqdc10", toml.Position{26, 3}},
|
||||||
queryTestNode{
|
|
||||||
"GitHub Cofounder & CEO\nLikes tater tots and beer.",
|
|
||||||
toml.Position{8, 1},
|
|
||||||
},
|
|
||||||
queryTestNode{
|
|
||||||
"192.168.1.1", toml.Position{12, 1},
|
|
||||||
},
|
|
||||||
queryTestNode{
|
|
||||||
"10.0.0.1", toml.Position{21, 3},
|
|
||||||
},
|
|
||||||
queryTestNode{
|
|
||||||
"eqdc10", toml.Position{22, 3},
|
|
||||||
},
|
|
||||||
queryTestNode{
|
|
||||||
"10.0.0.2", toml.Position{25, 3},
|
|
||||||
},
|
|
||||||
queryTestNode{
|
|
||||||
"eqdc10", toml.Position{26, 3},
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
assertQueryPositions(t, string(buff),
|
assertQueryPositions(t, string(buff),
|
||||||
"$..[?(float)]",
|
"$..[?(float)]",
|
||||||
[]interface{}{ // no float values in document
|
[]interface{}{
|
||||||
|
queryTestNode{4e-08, toml.Position{30, 1}},
|
||||||
})
|
})
|
||||||
|
|
||||||
tv, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
|
tv, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
|
||||||
@@ -460,6 +594,7 @@ func TestQueryFilterFn(t *testing.T) {
|
|||||||
[]interface{}{"gamma", "delta"},
|
[]interface{}{"gamma", "delta"},
|
||||||
[]interface{}{int64(1), int64(2)},
|
[]interface{}{int64(1), int64(2)},
|
||||||
},
|
},
|
||||||
|
"score": 4e-08,
|
||||||
}, toml.Position{28, 1},
|
}, toml.Position{28, 1},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -467,16 +602,12 @@ func TestQueryFilterFn(t *testing.T) {
|
|||||||
assertQueryPositions(t, string(buff),
|
assertQueryPositions(t, string(buff),
|
||||||
"$..[?(time)]",
|
"$..[?(time)]",
|
||||||
[]interface{}{
|
[]interface{}{
|
||||||
queryTestNode{
|
queryTestNode{tv, toml.Position{9, 1}},
|
||||||
tv, toml.Position{9, 1},
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
assertQueryPositions(t, string(buff),
|
assertQueryPositions(t, string(buff),
|
||||||
"$..[?(bool)]",
|
"$..[?(bool)]",
|
||||||
[]interface{}{
|
[]interface{}{
|
||||||
queryTestNode{
|
queryTestNode{true, toml.Position{15, 1}},
|
||||||
true, toml.Position{15, 1},
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-29
@@ -7,23 +7,24 @@ import (
|
|||||||
"github.com/pelletier/go-toml"
|
"github.com/pelletier/go-toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
func assertArrayContainsInAnyOrder(t *testing.T, array []interface{}, objects ...interface{}) {
|
func assertArrayContainsInOrder(t *testing.T, array []interface{}, objects ...interface{}) {
|
||||||
if len(array) != len(objects) {
|
if len(array) != len(objects) {
|
||||||
t.Fatalf("array contains %d objects but %d are expected", len(array), len(objects))
|
t.Fatalf("array contains %d objects but %d are expected", len(array), len(objects))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, o := range objects {
|
for i := 0; i < len(array); i++ {
|
||||||
found := false
|
if array[i] != objects[i] {
|
||||||
for _, a := range array {
|
t.Fatalf("wanted '%s', have '%s'", objects[i], array[i])
|
||||||
if a == o {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found {
|
}
|
||||||
t.Fatal(o, "not found in array", array)
|
|
||||||
}
|
func checkQuery(t *testing.T, tree *toml.Tree, query string, objects ...interface{}) {
|
||||||
|
results, err := CompileAndExecute(query, tree)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("unexpected error:", err)
|
||||||
}
|
}
|
||||||
|
assertArrayContainsInOrder(t, results.Values(), objects...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestQueryExample(t *testing.T) {
|
func TestQueryExample(t *testing.T) {
|
||||||
@@ -38,15 +39,17 @@ func TestQueryExample(t *testing.T) {
|
|||||||
title = "Neuromancer"
|
title = "Neuromancer"
|
||||||
author = "William Gibson"
|
author = "William Gibson"
|
||||||
`)
|
`)
|
||||||
authors, err := CompileAndExecute("$.book.author", config)
|
|
||||||
if err != nil {
|
checkQuery(t, config, "$.book.author", "Stephen King", "Ernest Hemmingway", "William Gibson")
|
||||||
t.Fatal("unexpected error:", err)
|
|
||||||
}
|
checkQuery(t, config, "$.book[0].author", "Stephen King")
|
||||||
names := authors.Values()
|
checkQuery(t, config, "$.book[-1].author", "William Gibson")
|
||||||
if len(names) != 3 {
|
checkQuery(t, config, "$.book[1:].author", "Ernest Hemmingway", "William Gibson")
|
||||||
t.Fatalf("query should return 3 names but returned %d", len(names))
|
checkQuery(t, config, "$.book[-1:].author", "William Gibson")
|
||||||
}
|
checkQuery(t, config, "$.book[::2].author", "Stephen King", "William Gibson")
|
||||||
assertArrayContainsInAnyOrder(t, names, "Stephen King", "Ernest Hemmingway", "William Gibson")
|
checkQuery(t, config, "$.book[::-1].author", "William Gibson", "Ernest Hemmingway", "Stephen King")
|
||||||
|
checkQuery(t, config, "$.book[:].author", "Stephen King", "Ernest Hemmingway", "William Gibson")
|
||||||
|
checkQuery(t, config, "$.book[::].author", "Stephen King", "Ernest Hemmingway", "William Gibson")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestQueryReadmeExample(t *testing.T) {
|
func TestQueryReadmeExample(t *testing.T) {
|
||||||
@@ -56,16 +59,7 @@ user = "pelletier"
|
|||||||
password = "mypassword"
|
password = "mypassword"
|
||||||
`)
|
`)
|
||||||
|
|
||||||
query, err := Compile("$..[user,password]")
|
checkQuery(t, config, "$..[user,password]", "pelletier", "mypassword")
|
||||||
if err != nil {
|
|
||||||
t.Fatal("unexpected error:", err)
|
|
||||||
}
|
|
||||||
results := query.Execute(config)
|
|
||||||
values := results.Values()
|
|
||||||
if len(values) != 2 {
|
|
||||||
t.Fatalf("query should return 2 values but returned %d", len(values))
|
|
||||||
}
|
|
||||||
assertArrayContainsInAnyOrder(t, values, "pelletier", "mypassword")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestQueryPathNotPresent(t *testing.T) {
|
func TestQueryPathNotPresent(t *testing.T) {
|
||||||
|
|||||||
+4
-4
@@ -2,9 +2,9 @@ package query
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pelletier/go-toml"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"unicode"
|
|
||||||
|
"github.com/pelletier/go-toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Define tokens
|
// Define tokens
|
||||||
@@ -92,11 +92,11 @@ func isSpace(r rune) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isAlphanumeric(r rune) bool {
|
func isAlphanumeric(r rune) bool {
|
||||||
return unicode.IsLetter(r) || r == '_'
|
return 'a' <= r && r <= 'z' || 'A' <= r && r <= 'Z' || r == '_'
|
||||||
}
|
}
|
||||||
|
|
||||||
func isDigit(r rune) bool {
|
func isDigit(r rune) bool {
|
||||||
return unicode.IsNumber(r)
|
return '0' <= r && r <= '9'
|
||||||
}
|
}
|
||||||
|
|
||||||
func isHexDigit(r rune) bool {
|
func isHexDigit(r rune) bool {
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
package toml
|
package toml
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"unicode"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Define tokens
|
// Define tokens
|
||||||
type tokenType int
|
type tokenType int
|
||||||
@@ -35,6 +31,7 @@ const (
|
|||||||
tokenDoubleLeftBracket
|
tokenDoubleLeftBracket
|
||||||
tokenDoubleRightBracket
|
tokenDoubleRightBracket
|
||||||
tokenDate
|
tokenDate
|
||||||
|
tokenLocalDate
|
||||||
tokenKeyGroup
|
tokenKeyGroup
|
||||||
tokenKeyGroupArray
|
tokenKeyGroupArray
|
||||||
tokenComma
|
tokenComma
|
||||||
@@ -68,7 +65,8 @@ var tokenTypeNames = []string{
|
|||||||
")",
|
")",
|
||||||
"]]",
|
"]]",
|
||||||
"[[",
|
"[[",
|
||||||
"Date",
|
"LocalDate",
|
||||||
|
"LocalDate",
|
||||||
"KeyGroup",
|
"KeyGroup",
|
||||||
"KeyGroupArray",
|
"KeyGroupArray",
|
||||||
",",
|
",",
|
||||||
@@ -95,14 +93,6 @@ func (tt tokenType) String() string {
|
|||||||
return "Unknown"
|
return "Unknown"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t token) Int() int {
|
|
||||||
if result, err := strconv.Atoi(t.val); err != nil {
|
|
||||||
panic(err)
|
|
||||||
} else {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t token) String() string {
|
func (t token) String() string {
|
||||||
switch t.typ {
|
switch t.typ {
|
||||||
case tokenEOF:
|
case tokenEOF:
|
||||||
@@ -119,7 +109,7 @@ func isSpace(r rune) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isAlphanumeric(r rune) bool {
|
func isAlphanumeric(r rune) bool {
|
||||||
return unicode.IsLetter(r) || r == '_'
|
return 'a' <= r && r <= 'z' || 'A' <= r && r <= 'Z' || r == '_'
|
||||||
}
|
}
|
||||||
|
|
||||||
func isKeyChar(r rune) bool {
|
func isKeyChar(r rune) bool {
|
||||||
@@ -134,7 +124,7 @@ func isKeyStartChar(r rune) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isDigit(r rune) bool {
|
func isDigit(r rune) bool {
|
||||||
return unicode.IsNumber(r)
|
return '0' <= r && r <= '9'
|
||||||
}
|
}
|
||||||
|
|
||||||
func isHexDigit(r rune) bool {
|
func isHexDigit(r rune) bool {
|
||||||
|
|||||||
+2
-1
@@ -25,7 +25,8 @@ func TestTokenStringer(t *testing.T) {
|
|||||||
{tokenRightParen, ")"},
|
{tokenRightParen, ")"},
|
||||||
{tokenDoubleLeftBracket, "]]"},
|
{tokenDoubleLeftBracket, "]]"},
|
||||||
{tokenDoubleRightBracket, "[["},
|
{tokenDoubleRightBracket, "[["},
|
||||||
{tokenDate, "Date"},
|
{tokenDate, "LocalDate"},
|
||||||
|
{tokenLocalDate, "LocalDate"},
|
||||||
{tokenKeyGroup, "KeyGroup"},
|
{tokenKeyGroup, "KeyGroup"},
|
||||||
{tokenKeyGroupArray, "KeyGroupArray"},
|
{tokenKeyGroupArray, "KeyGroupArray"},
|
||||||
{tokenComma, ","},
|
{tokenComma, ","},
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ type Tree struct {
|
|||||||
values map[string]interface{} // string -> *tomlValue, *Tree, []*Tree
|
values map[string]interface{} // string -> *tomlValue, *Tree, []*Tree
|
||||||
comment string
|
comment string
|
||||||
commented bool
|
commented bool
|
||||||
|
inline bool
|
||||||
position Position
|
position Position
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,6 +122,89 @@ func (t *Tree) GetPath(keys []string) interface{} {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetArray returns the value at key in the Tree.
|
||||||
|
// It returns []string, []int64, etc type if key has homogeneous lists
|
||||||
|
// Key is a dot-separated path (e.g. a.b.c) without single/double quoted strings.
|
||||||
|
// Returns nil if the path does not exist in the tree.
|
||||||
|
// If keys is of length zero, the current tree is returned.
|
||||||
|
func (t *Tree) GetArray(key string) interface{} {
|
||||||
|
if key == "" {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
return t.GetArrayPath(strings.Split(key, "."))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetArrayPath returns the element in the tree indicated by 'keys'.
|
||||||
|
// If keys is of length zero, the current tree is returned.
|
||||||
|
func (t *Tree) GetArrayPath(keys []string) interface{} {
|
||||||
|
if len(keys) == 0 {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
subtree := t
|
||||||
|
for _, intermediateKey := range keys[:len(keys)-1] {
|
||||||
|
value, exists := subtree.values[intermediateKey]
|
||||||
|
if !exists {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
switch node := value.(type) {
|
||||||
|
case *Tree:
|
||||||
|
subtree = node
|
||||||
|
case []*Tree:
|
||||||
|
// go to most recent element
|
||||||
|
if len(node) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
subtree = node[len(node)-1]
|
||||||
|
default:
|
||||||
|
return nil // cannot navigate through other node types
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// branch based on final node type
|
||||||
|
switch node := subtree.values[keys[len(keys)-1]].(type) {
|
||||||
|
case *tomlValue:
|
||||||
|
switch n := node.value.(type) {
|
||||||
|
case []interface{}:
|
||||||
|
return getArray(n)
|
||||||
|
default:
|
||||||
|
return node.value
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if homogeneous array, then return slice type object over []interface{}
|
||||||
|
func getArray(n []interface{}) interface{} {
|
||||||
|
var s []string
|
||||||
|
var i64 []int64
|
||||||
|
var f64 []float64
|
||||||
|
var bl []bool
|
||||||
|
for _, value := range n {
|
||||||
|
switch v := value.(type) {
|
||||||
|
case string:
|
||||||
|
s = append(s, v)
|
||||||
|
case int64:
|
||||||
|
i64 = append(i64, v)
|
||||||
|
case float64:
|
||||||
|
f64 = append(f64, v)
|
||||||
|
case bool:
|
||||||
|
bl = append(bl, v)
|
||||||
|
default:
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(s) == len(n) {
|
||||||
|
return s
|
||||||
|
} else if len(i64) == len(n) {
|
||||||
|
return i64
|
||||||
|
} else if len(f64) == len(n) {
|
||||||
|
return f64
|
||||||
|
} else if len(bl) == len(n) {
|
||||||
|
return bl
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
// GetPosition returns the position of the given key.
|
// GetPosition returns the position of the given key.
|
||||||
func (t *Tree) GetPosition(key string) Position {
|
func (t *Tree) GetPosition(key string) Position {
|
||||||
if key == "" {
|
if key == "" {
|
||||||
@@ -129,6 +213,50 @@ func (t *Tree) GetPosition(key string) Position {
|
|||||||
return t.GetPositionPath(strings.Split(key, "."))
|
return t.GetPositionPath(strings.Split(key, "."))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetPositionPath sets the position of element in the tree indicated by 'keys'.
|
||||||
|
// If keys is of length zero, the current tree position is set.
|
||||||
|
func (t *Tree) SetPositionPath(keys []string, pos Position) {
|
||||||
|
if len(keys) == 0 {
|
||||||
|
t.position = pos
|
||||||
|
return
|
||||||
|
}
|
||||||
|
subtree := t
|
||||||
|
for _, intermediateKey := range keys[:len(keys)-1] {
|
||||||
|
value, exists := subtree.values[intermediateKey]
|
||||||
|
if !exists {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch node := value.(type) {
|
||||||
|
case *Tree:
|
||||||
|
subtree = node
|
||||||
|
case []*Tree:
|
||||||
|
// go to most recent element
|
||||||
|
if len(node) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
subtree = node[len(node)-1]
|
||||||
|
default:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// branch based on final node type
|
||||||
|
switch node := subtree.values[keys[len(keys)-1]].(type) {
|
||||||
|
case *tomlValue:
|
||||||
|
node.position = pos
|
||||||
|
return
|
||||||
|
case *Tree:
|
||||||
|
node.position = pos
|
||||||
|
return
|
||||||
|
case []*Tree:
|
||||||
|
// go to most recent element
|
||||||
|
if len(node) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
node[len(node)-1].position = pos
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// GetPositionPath returns the element in the tree indicated by 'keys'.
|
// GetPositionPath returns the element in the tree indicated by 'keys'.
|
||||||
// If keys is of length zero, the current tree is returned.
|
// If keys is of length zero, the current tree is returned.
|
||||||
func (t *Tree) GetPositionPath(keys []string) Position {
|
func (t *Tree) GetPositionPath(keys []string) Position {
|
||||||
@@ -211,7 +339,8 @@ func (t *Tree) SetPathWithOptions(keys []string, opts SetOptions, value interfac
|
|||||||
// go to most recent element
|
// go to most recent element
|
||||||
if len(node) == 0 {
|
if len(node) == 0 {
|
||||||
// create element if it does not exist
|
// create element if it does not exist
|
||||||
subtree.values[intermediateKey] = append(node, newTreeWithPosition(Position{Line: t.position.Line + i, Col: t.position.Col}))
|
node = append(node, newTreeWithPosition(Position{Line: t.position.Line + i, Col: t.position.Col}))
|
||||||
|
subtree.values[intermediateKey] = node
|
||||||
}
|
}
|
||||||
subtree = node[len(node)-1]
|
subtree = node[len(node)-1]
|
||||||
}
|
}
|
||||||
@@ -222,11 +351,17 @@ func (t *Tree) SetPathWithOptions(keys []string, opts SetOptions, value interfac
|
|||||||
switch v := value.(type) {
|
switch v := value.(type) {
|
||||||
case *Tree:
|
case *Tree:
|
||||||
v.comment = opts.Comment
|
v.comment = opts.Comment
|
||||||
|
v.commented = opts.Commented
|
||||||
toInsert = value
|
toInsert = value
|
||||||
case []*Tree:
|
case []*Tree:
|
||||||
|
for i := range v {
|
||||||
|
v[i].commented = opts.Commented
|
||||||
|
}
|
||||||
toInsert = value
|
toInsert = value
|
||||||
case *tomlValue:
|
case *tomlValue:
|
||||||
v.comment = opts.Comment
|
v.comment = opts.Comment
|
||||||
|
v.commented = opts.Commented
|
||||||
|
v.multiline = opts.Multiline
|
||||||
toInsert = v
|
toInsert = v
|
||||||
default:
|
default:
|
||||||
toInsert = &tomlValue{value: value,
|
toInsert = &tomlValue{value: value,
|
||||||
@@ -307,6 +442,7 @@ func (t *Tree) createSubTree(keys []string, pos Position) error {
|
|||||||
if !exists {
|
if !exists {
|
||||||
tree := newTreeWithPosition(Position{Line: t.position.Line + i, Col: t.position.Col})
|
tree := newTreeWithPosition(Position{Line: t.position.Line + i, Col: t.position.Col})
|
||||||
tree.position = pos
|
tree.position = pos
|
||||||
|
tree.inline = subtree.inline
|
||||||
subtree.values[intermediateKey] = tree
|
subtree.values[intermediateKey] = tree
|
||||||
nextTree = tree
|
nextTree = tree
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
package toml
|
package toml
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,6 +40,41 @@ func TestTomlGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTomlGetArray(t *testing.T) {
|
||||||
|
tree, _ := Load(`
|
||||||
|
[test]
|
||||||
|
key = ["one", "two"]
|
||||||
|
key2 = [true, false, false]
|
||||||
|
key3 = [1.5,2.5]
|
||||||
|
`)
|
||||||
|
|
||||||
|
if tree.GetArray("") != tree {
|
||||||
|
t.Errorf("GetArray should return the tree itself when given an empty path")
|
||||||
|
}
|
||||||
|
|
||||||
|
expect := []string{"one", "two"}
|
||||||
|
actual := tree.GetArray("test.key").([]string)
|
||||||
|
if !reflect.DeepEqual(actual, expect) {
|
||||||
|
t.Errorf("GetArray should return the []string value")
|
||||||
|
}
|
||||||
|
|
||||||
|
expect2 := []bool{true, false, false}
|
||||||
|
actual2 := tree.GetArray("test.key2").([]bool)
|
||||||
|
if !reflect.DeepEqual(actual2, expect2) {
|
||||||
|
t.Errorf("GetArray should return the []bool value")
|
||||||
|
}
|
||||||
|
|
||||||
|
expect3 := []float64{1.5, 2.5}
|
||||||
|
actual3 := tree.GetArray("test.key3").([]float64)
|
||||||
|
if !reflect.DeepEqual(actual3, expect3) {
|
||||||
|
t.Errorf("GetArray should return the []float64 value")
|
||||||
|
}
|
||||||
|
|
||||||
|
if tree.GetArray(`\`) != nil {
|
||||||
|
t.Errorf("should return nil when the key is malformed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTomlGetDefault(t *testing.T) {
|
func TestTomlGetDefault(t *testing.T) {
|
||||||
tree, _ := Load(`
|
tree, _ := Load(`
|
||||||
[test]
|
[test]
|
||||||
@@ -148,6 +184,51 @@ func TestTomlGetPath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTomlGetArrayPath(t *testing.T) {
|
||||||
|
for idx, item := range []struct {
|
||||||
|
Name string
|
||||||
|
Path []string
|
||||||
|
Make func() (tree *Tree, expected interface{})
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Name: "empty",
|
||||||
|
Path: []string{},
|
||||||
|
Make: func() (tree *Tree, expected interface{}) {
|
||||||
|
tree = newTree()
|
||||||
|
expected = tree
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "int64",
|
||||||
|
Path: []string{"a"},
|
||||||
|
Make: func() (tree *Tree, expected interface{}) {
|
||||||
|
var err error
|
||||||
|
tree, err = Load(`a = [1,2,3]`)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
expected = []int64{1, 2, 3}
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(item.Name, func(t *testing.T) {
|
||||||
|
tree, expected := item.Make()
|
||||||
|
result := tree.GetArrayPath(item.Path)
|
||||||
|
if !reflect.DeepEqual(result, expected) {
|
||||||
|
t.Errorf("GetArrayPath[%d] %v - expected %#v, got %#v instead.", idx, item.Path, expected, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
tree, _ := Load("[foo.bar]\na=1\nb=2\n[baz.foo]\na=3\nb=4\n[gorf.foo]\na=5\nb=6")
|
||||||
|
if tree.GetArrayPath([]string{"whatever"}) != nil {
|
||||||
|
t.Error("GetArrayPath should return nil when the key does not exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestTomlFromMap(t *testing.T) {
|
func TestTomlFromMap(t *testing.T) {
|
||||||
simpleMap := map[string]interface{}{"hello": 42}
|
simpleMap := map[string]interface{}{"hello": 42}
|
||||||
tree, err := TreeFromMap(simpleMap)
|
tree, err := TreeFromMap(simpleMap)
|
||||||
|
|||||||
@@ -5,21 +5,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInvalidArrayMixedTypesArraysAndInts(t *testing.T) {
|
|
||||||
input := `arrays-and-ints = [1, ["Arrays are not integers."]]`
|
|
||||||
testgenInvalid(t, input)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInvalidArrayMixedTypesIntsAndFloats(t *testing.T) {
|
|
||||||
input := `ints-and-floats = [1, 1.1]`
|
|
||||||
testgenInvalid(t, input)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInvalidArrayMixedTypesStringsAndInts(t *testing.T) {
|
|
||||||
input := `strings-and-ints = ["hi", 42]`
|
|
||||||
testgenInvalid(t, input)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInvalidDatetimeMalformedNoLeads(t *testing.T) {
|
func TestInvalidDatetimeMalformedNoLeads(t *testing.T) {
|
||||||
input := `no-leads = 1987-7-05T17:45:00Z`
|
input := `no-leads = 1987-7-05T17:45:00Z`
|
||||||
testgenInvalid(t, input)
|
testgenInvalid(t, input)
|
||||||
|
|||||||
@@ -57,6 +57,19 @@ func simpleValueCoercion(object interface{}) (interface{}, error) {
|
|||||||
return float64(original), nil
|
return float64(original), nil
|
||||||
case fmt.Stringer:
|
case fmt.Stringer:
|
||||||
return original.String(), nil
|
return original.String(), nil
|
||||||
|
case []interface{}:
|
||||||
|
value := reflect.ValueOf(original)
|
||||||
|
length := value.Len()
|
||||||
|
arrayValue := reflect.MakeSlice(value.Type(), 0, length)
|
||||||
|
for i := 0; i < length; i++ {
|
||||||
|
val := value.Index(i).Interface()
|
||||||
|
simpleValue, err := simpleValueCoercion(val)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
arrayValue = reflect.Append(arrayValue, reflect.ValueOf(simpleValue))
|
||||||
|
}
|
||||||
|
return arrayValue.Interface(), nil
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("cannot convert type %T to Tree", object)
|
return nil, fmt.Errorf("cannot convert type %T to Tree", object)
|
||||||
}
|
}
|
||||||
|
|||||||
+118
-1
@@ -1,6 +1,7 @@
|
|||||||
package toml
|
package toml
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -105,7 +106,7 @@ func TestTreeCreateToTreeInvalidTableGroupType(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRoundTripArrayOfTables(t *testing.T) {
|
func TestRoundTripArrayOfTables(t *testing.T) {
|
||||||
orig := "\n[[stuff]]\n name = \"foo\"\n things = [\"a\",\"b\"]\n"
|
orig := "\n[[stuff]]\n name = \"foo\"\n things = [\"a\", \"b\"]\n"
|
||||||
tree, err := Load(orig)
|
tree, err := Load(orig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %s", err)
|
t.Fatalf("unexpected error: %s", err)
|
||||||
@@ -124,3 +125,119 @@ func TestRoundTripArrayOfTables(t *testing.T) {
|
|||||||
t.Errorf("want:\n%s\ngot:\n%s", want, got)
|
t.Errorf("want:\n%s\ngot:\n%s", want, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTomlSliceOfSlice(t *testing.T) {
|
||||||
|
tree, err := Load(` hosts=[["10.1.0.107:9092","10.1.0.107:9093", "192.168.0.40:9094"] ] `)
|
||||||
|
m := tree.ToMap()
|
||||||
|
tree, err = TreeFromMap(m)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("should not error", err)
|
||||||
|
}
|
||||||
|
type Struct struct {
|
||||||
|
Hosts [][]string
|
||||||
|
}
|
||||||
|
var actual Struct
|
||||||
|
tree.Unmarshal(&actual)
|
||||||
|
|
||||||
|
expected := Struct{Hosts: [][]string{[]string{"10.1.0.107:9092", "10.1.0.107:9093", "192.168.0.40:9094"}}}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Errorf("Bad unmarshal: expected %+v, got %+v", expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTomlSliceOfSliceOfSlice(t *testing.T) {
|
||||||
|
tree, err := Load(` hosts=[[["10.1.0.107:9092","10.1.0.107:9093", "192.168.0.40:9094"] ]] `)
|
||||||
|
m := tree.ToMap()
|
||||||
|
tree, err = TreeFromMap(m)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("should not error", err)
|
||||||
|
}
|
||||||
|
type Struct struct {
|
||||||
|
Hosts [][][]string
|
||||||
|
}
|
||||||
|
var actual Struct
|
||||||
|
tree.Unmarshal(&actual)
|
||||||
|
|
||||||
|
expected := Struct{Hosts: [][][]string{[][]string{[]string{"10.1.0.107:9092", "10.1.0.107:9093", "192.168.0.40:9094"}}}}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Errorf("Bad unmarshal: expected %+v, got %+v", expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTomlSliceOfSliceInt(t *testing.T) {
|
||||||
|
tree, err := Load(` hosts=[[1,2,3],[4,5,6] ] `)
|
||||||
|
m := tree.ToMap()
|
||||||
|
tree, err = TreeFromMap(m)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("should not error", err)
|
||||||
|
}
|
||||||
|
type Struct struct {
|
||||||
|
Hosts [][]int
|
||||||
|
}
|
||||||
|
var actual Struct
|
||||||
|
err = tree.Unmarshal(&actual)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("should not error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := Struct{Hosts: [][]int{[]int{1, 2, 3}, []int{4, 5, 6}}}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Errorf("Bad unmarshal: expected %+v, got %+v", expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
func TestTomlSliceOfSliceInt64(t *testing.T) {
|
||||||
|
tree, err := Load(` hosts=[[1,2,3],[4,5,6] ] `)
|
||||||
|
m := tree.ToMap()
|
||||||
|
tree, err = TreeFromMap(m)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("should not error", err)
|
||||||
|
}
|
||||||
|
type Struct struct {
|
||||||
|
Hosts [][]int64
|
||||||
|
}
|
||||||
|
var actual Struct
|
||||||
|
err = tree.Unmarshal(&actual)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("should not error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := Struct{Hosts: [][]int64{[]int64{1, 2, 3}, []int64{4, 5, 6}}}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Errorf("Bad unmarshal: expected %+v, got %+v", expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTomlSliceOfSliceInt64FromMap(t *testing.T) {
|
||||||
|
tree, err := TreeFromMap(map[string]interface{}{"hosts": [][]interface{}{[]interface{}{int32(1), int8(2), 3}}})
|
||||||
|
if err != nil {
|
||||||
|
t.Error("should not error", err)
|
||||||
|
}
|
||||||
|
type Struct struct {
|
||||||
|
Hosts [][]int64
|
||||||
|
}
|
||||||
|
var actual Struct
|
||||||
|
err = tree.Unmarshal(&actual)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("should not error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := Struct{Hosts: [][]int64{[]int64{1, 2, 3}}}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Errorf("Bad unmarshal: expected %+v, got %+v", expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
func TestTomlSliceOfSliceError(t *testing.T) { // make Codecov happy
|
||||||
|
_, err := TreeFromMap(map[string]interface{}{"hosts": [][]interface{}{[]interface{}{1, 2, []struct{}{}}}})
|
||||||
|
expected := "cannot convert type []struct {} to Tree"
|
||||||
|
if err.Error() != expected {
|
||||||
|
t.Fatalf("unexpected error: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+116
-33
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
"math/big"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -27,23 +28,35 @@ type sortNode struct {
|
|||||||
// Encodes a string to a TOML-compliant multi-line string value
|
// Encodes a string to a TOML-compliant multi-line string value
|
||||||
// This function is a clone of the existing encodeTomlString function, except that whitespace characters
|
// This function is a clone of the existing encodeTomlString function, except that whitespace characters
|
||||||
// are preserved. Quotation marks and backslashes are also not escaped.
|
// are preserved. Quotation marks and backslashes are also not escaped.
|
||||||
func encodeMultilineTomlString(value string) string {
|
func encodeMultilineTomlString(value string, commented string) string {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
|
adjacentQuoteCount := 0
|
||||||
|
|
||||||
for _, rr := range value {
|
b.WriteString(commented)
|
||||||
|
for i, rr := range value {
|
||||||
|
if rr != '"' {
|
||||||
|
adjacentQuoteCount = 0
|
||||||
|
} else {
|
||||||
|
adjacentQuoteCount++
|
||||||
|
}
|
||||||
switch rr {
|
switch rr {
|
||||||
case '\b':
|
case '\b':
|
||||||
b.WriteString(`\b`)
|
b.WriteString(`\b`)
|
||||||
case '\t':
|
case '\t':
|
||||||
b.WriteString("\t")
|
b.WriteString("\t")
|
||||||
case '\n':
|
case '\n':
|
||||||
b.WriteString("\n")
|
b.WriteString("\n" + commented)
|
||||||
case '\f':
|
case '\f':
|
||||||
b.WriteString(`\f`)
|
b.WriteString(`\f`)
|
||||||
case '\r':
|
case '\r':
|
||||||
b.WriteString("\r")
|
b.WriteString("\r")
|
||||||
case '"':
|
case '"':
|
||||||
|
if adjacentQuoteCount >= 3 || i == len(value)-1 {
|
||||||
|
adjacentQuoteCount = 0
|
||||||
|
b.WriteString(`\"`)
|
||||||
|
} else {
|
||||||
b.WriteString(`"`)
|
b.WriteString(`"`)
|
||||||
|
}
|
||||||
case '\\':
|
case '\\':
|
||||||
b.WriteString(`\`)
|
b.WriteString(`\`)
|
||||||
default:
|
default:
|
||||||
@@ -90,7 +103,30 @@ func encodeTomlString(value string) string {
|
|||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func tomlValueStringRepresentation(v interface{}, indent string, arraysOneElementPerLine bool) (string, error) {
|
func tomlTreeStringRepresentation(t *Tree, ord marshalOrder) (string, error) {
|
||||||
|
var orderedVals []sortNode
|
||||||
|
switch ord {
|
||||||
|
case OrderPreserve:
|
||||||
|
orderedVals = sortByLines(t)
|
||||||
|
default:
|
||||||
|
orderedVals = sortAlphabetical(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
var values []string
|
||||||
|
for _, node := range orderedVals {
|
||||||
|
k := node.key
|
||||||
|
v := t.values[k]
|
||||||
|
|
||||||
|
repr, err := tomlValueStringRepresentation(v, "", "", ord, false)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
values = append(values, quoteKeyIfNeeded(k)+" = "+repr)
|
||||||
|
}
|
||||||
|
return "{ " + strings.Join(values, ", ") + " }", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func tomlValueStringRepresentation(v interface{}, commented string, indent string, ord marshalOrder, arraysOneElementPerLine bool) (string, error) {
|
||||||
// this interface check is added to dereference the change made in the writeTo function.
|
// this interface check is added to dereference the change made in the writeTo function.
|
||||||
// That change was made to allow this function to see formatting options.
|
// That change was made to allow this function to see formatting options.
|
||||||
tv, ok := v.(*tomlValue)
|
tv, ok := v.(*tomlValue)
|
||||||
@@ -106,20 +142,28 @@ func tomlValueStringRepresentation(v interface{}, indent string, arraysOneElemen
|
|||||||
case int64:
|
case int64:
|
||||||
return strconv.FormatInt(value, 10), nil
|
return strconv.FormatInt(value, 10), nil
|
||||||
case float64:
|
case float64:
|
||||||
// Ensure a round float does contain a decimal point. Otherwise feeding
|
// Default bit length is full 64
|
||||||
// the output back to the parser would convert to an integer.
|
bits := 64
|
||||||
if math.Trunc(value) == value {
|
// Float panics if nan is used
|
||||||
return strings.ToLower(strconv.FormatFloat(value, 'f', 1, 32)), nil
|
if !math.IsNaN(value) {
|
||||||
|
// if 32 bit accuracy is enough to exactly show, use 32
|
||||||
|
_, acc := big.NewFloat(value).Float32()
|
||||||
|
if acc == big.Exact {
|
||||||
|
bits = 32
|
||||||
}
|
}
|
||||||
return strings.ToLower(strconv.FormatFloat(value, 'f', -1, 32)), nil
|
}
|
||||||
|
if math.Trunc(value) == value {
|
||||||
|
return strings.ToLower(strconv.FormatFloat(value, 'f', 1, bits)), nil
|
||||||
|
}
|
||||||
|
return strings.ToLower(strconv.FormatFloat(value, 'f', -1, bits)), nil
|
||||||
case string:
|
case string:
|
||||||
if tv.multiline {
|
if tv.multiline {
|
||||||
return "\"\"\"\n" + encodeMultilineTomlString(value) + "\"\"\"", nil
|
return "\"\"\"\n" + encodeMultilineTomlString(value, commented) + "\"\"\"", nil
|
||||||
}
|
}
|
||||||
return "\"" + encodeTomlString(value) + "\"", nil
|
return "\"" + encodeTomlString(value) + "\"", nil
|
||||||
case []byte:
|
case []byte:
|
||||||
b, _ := v.([]byte)
|
b, _ := v.([]byte)
|
||||||
return tomlValueStringRepresentation(string(b), indent, arraysOneElementPerLine)
|
return string(b), nil
|
||||||
case bool:
|
case bool:
|
||||||
if value {
|
if value {
|
||||||
return "true", nil
|
return "true", nil
|
||||||
@@ -127,6 +171,14 @@ func tomlValueStringRepresentation(v interface{}, indent string, arraysOneElemen
|
|||||||
return "false", nil
|
return "false", nil
|
||||||
case time.Time:
|
case time.Time:
|
||||||
return value.Format(time.RFC3339), nil
|
return value.Format(time.RFC3339), nil
|
||||||
|
case LocalDate:
|
||||||
|
return value.String(), nil
|
||||||
|
case LocalDateTime:
|
||||||
|
return value.String(), nil
|
||||||
|
case LocalTime:
|
||||||
|
return value.String(), nil
|
||||||
|
case *Tree:
|
||||||
|
return tomlTreeStringRepresentation(value, ord)
|
||||||
case nil:
|
case nil:
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
@@ -137,7 +189,7 @@ func tomlValueStringRepresentation(v interface{}, indent string, arraysOneElemen
|
|||||||
var values []string
|
var values []string
|
||||||
for i := 0; i < rv.Len(); i++ {
|
for i := 0; i < rv.Len(); i++ {
|
||||||
item := rv.Index(i).Interface()
|
item := rv.Index(i).Interface()
|
||||||
itemRepr, err := tomlValueStringRepresentation(item, indent, arraysOneElementPerLine)
|
itemRepr, err := tomlValueStringRepresentation(item, commented, indent, ord, arraysOneElementPerLine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -151,16 +203,16 @@ func tomlValueStringRepresentation(v interface{}, indent string, arraysOneElemen
|
|||||||
|
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
stringBuffer.WriteString(valueIndent)
|
stringBuffer.WriteString(valueIndent)
|
||||||
stringBuffer.WriteString(value)
|
stringBuffer.WriteString(commented + value)
|
||||||
stringBuffer.WriteString(`,`)
|
stringBuffer.WriteString(`,`)
|
||||||
stringBuffer.WriteString("\n")
|
stringBuffer.WriteString("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
stringBuffer.WriteString(indent + "]")
|
stringBuffer.WriteString(indent + commented + "]")
|
||||||
|
|
||||||
return stringBuffer.String(), nil
|
return stringBuffer.String(), nil
|
||||||
}
|
}
|
||||||
return "[" + strings.Join(values, ",") + "]", nil
|
return "[" + strings.Join(values, ", ") + "]", nil
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("unsupported value type %T: %v", v, v)
|
return "", fmt.Errorf("unsupported value type %T: %v", v, v)
|
||||||
}
|
}
|
||||||
@@ -255,10 +307,10 @@ func sortAlphabetical(t *Tree) (vals []sortNode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tree) writeTo(w io.Writer, indent, keyspace string, bytesCount int64, arraysOneElementPerLine bool) (int64, error) {
|
func (t *Tree) writeTo(w io.Writer, indent, keyspace string, bytesCount int64, arraysOneElementPerLine bool) (int64, error) {
|
||||||
return t.writeToOrdered(w, indent, keyspace, bytesCount, arraysOneElementPerLine, OrderAlphabetical)
|
return t.writeToOrdered(w, indent, keyspace, bytesCount, arraysOneElementPerLine, OrderAlphabetical, " ", false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount int64, arraysOneElementPerLine bool, ord marshalOrder) (int64, error) {
|
func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount int64, arraysOneElementPerLine bool, ord marshalOrder, indentString string, parentCommented bool) (int64, error) {
|
||||||
var orderedVals []sortNode
|
var orderedVals []sortNode
|
||||||
|
|
||||||
switch ord {
|
switch ord {
|
||||||
@@ -274,14 +326,10 @@ func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount i
|
|||||||
k := node.key
|
k := node.key
|
||||||
v := t.values[k]
|
v := t.values[k]
|
||||||
|
|
||||||
combinedKey := k
|
combinedKey := quoteKeyIfNeeded(k)
|
||||||
if keyspace != "" {
|
if keyspace != "" {
|
||||||
combinedKey = keyspace + "." + combinedKey
|
combinedKey = keyspace + "." + combinedKey
|
||||||
}
|
}
|
||||||
var commented string
|
|
||||||
if t.commented {
|
|
||||||
commented = "# "
|
|
||||||
}
|
|
||||||
|
|
||||||
switch node := v.(type) {
|
switch node := v.(type) {
|
||||||
// node has to be of those two types given how keys are sorted above
|
// node has to be of those two types given how keys are sorted above
|
||||||
@@ -302,24 +350,33 @@ func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount i
|
|||||||
return bytesCount, errc
|
return bytesCount, errc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var commented string
|
||||||
|
if parentCommented || t.commented || tv.commented {
|
||||||
|
commented = "# "
|
||||||
|
}
|
||||||
writtenBytesCount, err := writeStrings(w, "\n", indent, commented, "[", combinedKey, "]\n")
|
writtenBytesCount, err := writeStrings(w, "\n", indent, commented, "[", combinedKey, "]\n")
|
||||||
bytesCount += int64(writtenBytesCount)
|
bytesCount += int64(writtenBytesCount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bytesCount, err
|
return bytesCount, err
|
||||||
}
|
}
|
||||||
bytesCount, err = node.writeToOrdered(w, indent+" ", combinedKey, bytesCount, arraysOneElementPerLine, ord)
|
bytesCount, err = node.writeToOrdered(w, indent+indentString, combinedKey, bytesCount, arraysOneElementPerLine, ord, indentString, parentCommented || t.commented || tv.commented)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bytesCount, err
|
return bytesCount, err
|
||||||
}
|
}
|
||||||
case []*Tree:
|
case []*Tree:
|
||||||
for _, subTree := range node {
|
for _, subTree := range node {
|
||||||
|
var commented string
|
||||||
|
if parentCommented || t.commented || subTree.commented {
|
||||||
|
commented = "# "
|
||||||
|
}
|
||||||
writtenBytesCount, err := writeStrings(w, "\n", indent, commented, "[[", combinedKey, "]]\n")
|
writtenBytesCount, err := writeStrings(w, "\n", indent, commented, "[[", combinedKey, "]]\n")
|
||||||
bytesCount += int64(writtenBytesCount)
|
bytesCount += int64(writtenBytesCount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bytesCount, err
|
return bytesCount, err
|
||||||
}
|
}
|
||||||
|
|
||||||
bytesCount, err = subTree.writeToOrdered(w, indent+" ", combinedKey, bytesCount, arraysOneElementPerLine, ord)
|
bytesCount, err = subTree.writeToOrdered(w, indent+indentString, combinedKey, bytesCount, arraysOneElementPerLine, ord, indentString, parentCommented || t.commented || subTree.commented)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bytesCount, err
|
return bytesCount, err
|
||||||
}
|
}
|
||||||
@@ -332,7 +389,11 @@ func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount i
|
|||||||
return bytesCount, fmt.Errorf("invalid value type at %s: %T", k, t.values[k])
|
return bytesCount, fmt.Errorf("invalid value type at %s: %T", k, t.values[k])
|
||||||
}
|
}
|
||||||
|
|
||||||
repr, err := tomlValueStringRepresentation(v, indent, arraysOneElementPerLine)
|
var commented string
|
||||||
|
if parentCommented || t.commented || v.commented {
|
||||||
|
commented = "# "
|
||||||
|
}
|
||||||
|
repr, err := tomlValueStringRepresentation(v, commented, indent, ord, arraysOneElementPerLine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bytesCount, err
|
return bytesCount, err
|
||||||
}
|
}
|
||||||
@@ -350,11 +411,8 @@ func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount i
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var commented string
|
quotedKey := quoteKeyIfNeeded(k)
|
||||||
if v.commented {
|
writtenBytesCount, err := writeStrings(w, indent, commented, quotedKey, " = ", repr, "\n")
|
||||||
commented = "# "
|
|
||||||
}
|
|
||||||
writtenBytesCount, err := writeStrings(w, indent, commented, k, " = ", repr, "\n")
|
|
||||||
bytesCount += int64(writtenBytesCount)
|
bytesCount += int64(writtenBytesCount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bytesCount, err
|
return bytesCount, err
|
||||||
@@ -365,6 +423,32 @@ func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount i
|
|||||||
return bytesCount, nil
|
return bytesCount, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// quote a key if it does not fit the bare key format (A-Za-z0-9_-)
|
||||||
|
// quoted keys use the same rules as strings
|
||||||
|
func quoteKeyIfNeeded(k string) string {
|
||||||
|
// when encoding a map with the 'quoteMapKeys' option enabled, the tree will contain
|
||||||
|
// keys that have already been quoted.
|
||||||
|
// not an ideal situation, but good enough of a stop gap.
|
||||||
|
if len(k) >= 2 && k[0] == '"' && k[len(k)-1] == '"' {
|
||||||
|
return k
|
||||||
|
}
|
||||||
|
isBare := true
|
||||||
|
for _, r := range k {
|
||||||
|
if !isValidBareChar(r) {
|
||||||
|
isBare = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if isBare {
|
||||||
|
return k
|
||||||
|
}
|
||||||
|
return quoteKey(k)
|
||||||
|
}
|
||||||
|
|
||||||
|
func quoteKey(k string) string {
|
||||||
|
return "\"" + encodeTomlString(k) + "\""
|
||||||
|
}
|
||||||
|
|
||||||
func writeStrings(w io.Writer, s ...string) (int, error) {
|
func writeStrings(w io.Writer, s ...string) (int, error) {
|
||||||
var n int
|
var n int
|
||||||
for i := range s {
|
for i := range s {
|
||||||
@@ -387,12 +471,11 @@ func (t *Tree) WriteTo(w io.Writer) (int64, error) {
|
|||||||
// Output spans multiple lines, and is suitable for ingest by a TOML parser.
|
// Output spans multiple lines, and is suitable for ingest by a TOML parser.
|
||||||
// If the conversion cannot be performed, ToString returns a non-nil error.
|
// If the conversion cannot be performed, ToString returns a non-nil error.
|
||||||
func (t *Tree) ToTomlString() (string, error) {
|
func (t *Tree) ToTomlString() (string, error) {
|
||||||
var buf bytes.Buffer
|
b, err := t.Marshal()
|
||||||
_, err := t.WriteTo(&buf)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return buf.String(), nil
|
return string(b), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// String generates a human-readable representation of the current tree.
|
// String generates a human-readable representation of the current tree.
|
||||||
|
|||||||
@@ -236,6 +236,7 @@ func TestTreeWriteToMapExampleFile(t *testing.T) {
|
|||||||
[]interface{}{"gamma", "delta"},
|
[]interface{}{"gamma", "delta"},
|
||||||
[]interface{}{int64(1), int64(2)},
|
[]interface{}{int64(1), int64(2)},
|
||||||
},
|
},
|
||||||
|
"score": 4e-08,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
testMaps(t, tree.ToMap(), expected)
|
testMaps(t, tree.ToMap(), expected)
|
||||||
@@ -327,6 +328,30 @@ c = nan`
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIssue290(t *testing.T) {
|
||||||
|
tomlString :=
|
||||||
|
`[table]
|
||||||
|
"127.0.0.1" = "value"
|
||||||
|
"127.0.0.1:8028" = "value"
|
||||||
|
"character encoding" = "value"
|
||||||
|
"ʎǝʞ" = "value"`
|
||||||
|
|
||||||
|
t1, err := Load(tomlString)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("load err:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err := t1.ToTomlString()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("ToTomlString err:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = Load(s)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("reload err:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkTreeToTomlString(b *testing.B) {
|
func BenchmarkTreeToTomlString(b *testing.B) {
|
||||||
toml, err := Load(sampleHard)
|
toml, err := Load(sampleHard)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user