Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b8ba995eaa | |||
| 8e44986c28 | |||
| 837c1d09ee | |||
| 8410c965c2 | |||
| d083470585 | |||
| c893dbf25c | |||
| 2a1df71375 | |||
| a2f5197638 | |||
| bb65137dc4 | |||
| 99782c87cf | |||
| ce6fbd7bc0 | |||
| b59c12a70d | |||
| 6a307ac0d0 | |||
| a2e5256180 | |||
| 5163266f16 | |||
| b4f0a950bf | |||
| ef48fb2be1 | |||
| c9a09d8695 | |||
| 3430b0f086 | |||
| a713a3eccc | |||
| 652b9f8232 | |||
| ba1b12be14 | |||
| 2e01f733df | |||
| 1bd9461acb | |||
| 5b4e7e5dcc | |||
| b4905040a8 | |||
| 5c66c78bc5 | |||
| f9ba08244d | |||
| e6908614ee | |||
| a7448fe8de |
@@ -1,9 +1,18 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Create a report to help us improve
|
||||
|
||||
---
|
||||
|
||||
<!--
|
||||
‼️ Main development focus is on the upcoming go-toml v2 ⚠️
|
||||
|
||||
As a result, v1.x bugs will likely not see a fix on a v1.x version.
|
||||
However, reporting the bug is the best way to ensure that it will be fixed in v2.
|
||||
|
||||
See https://github.com/pelletier/go-toml/discussions/506.
|
||||
-->
|
||||
|
||||
|
||||
**Describe the bug**
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
@@ -14,7 +23,7 @@ Steps to reproduce the behavior. Including TOML files.
|
||||
A clear and concise description of what you expected to happen, if other than "should work".
|
||||
|
||||
**Versions**
|
||||
- go-toml: version (git sha)
|
||||
- go-toml: version (or git sha)
|
||||
- go: version
|
||||
- operating system: e.g. macOS, Windows, Linux
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
blank_issues_enabled: false
|
||||
contact_links:
|
||||
- name: Questions and discussions
|
||||
url: https://github.com/pelletier/go-toml/discussions
|
||||
about: Please ask and answer questions here.
|
||||
@@ -1,17 +0,0 @@
|
||||
---
|
||||
name: Feature request
|
||||
about: Suggest an idea for this project
|
||||
|
||||
---
|
||||
|
||||
**Is your feature request related to a problem? Please describe.**
|
||||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||
|
||||
**Describe the solution you'd like**
|
||||
A clear and concise description of what you want to happen.
|
||||
|
||||
**Describe alternatives you've considered**
|
||||
A clear and concise description of any alternative solutions or features you've considered.
|
||||
|
||||
**Additional context**
|
||||
Add any other context or screenshots about the feature request here.
|
||||
@@ -0,0 +1,8 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: gomod
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: daily
|
||||
time: "13:00"
|
||||
open-pull-requests-limit: 10
|
||||
@@ -0,0 +1,67 @@
|
||||
# For most projects, this workflow file will not need changing; you simply need
|
||||
# to commit it to your repository.
|
||||
#
|
||||
# You may wish to alter this file to override the set of languages analyzed,
|
||||
# or to provide custom queries or build logic.
|
||||
#
|
||||
# ******** NOTE ********
|
||||
# We have attempted to detect the languages in your repository. Please check
|
||||
# the `language` matrix defined below to confirm you have the correct set of
|
||||
# supported CodeQL languages.
|
||||
#
|
||||
name: "CodeQL"
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
# The branches below must be a subset of the branches above
|
||||
branches: [ master ]
|
||||
schedule:
|
||||
- cron: '26 19 * * 0'
|
||||
|
||||
jobs:
|
||||
analyze:
|
||||
name: Analyze
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
language: [ 'go' ]
|
||||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
|
||||
# Learn more:
|
||||
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v2
|
||||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v1
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
# By default, queries listed here will override any specified in a config file.
|
||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
||||
|
||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
- name: Autobuild
|
||||
uses: github/codeql-action/autobuild@v1
|
||||
|
||||
# ℹ️ Command-line programs to run using the OS shell.
|
||||
# 📚 https://git.io/JvXDl
|
||||
|
||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
||||
# and modify them (or add more) to build your code if your project
|
||||
# uses a compiled language
|
||||
|
||||
#- run: |
|
||||
# make bootstrap
|
||||
# make release
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v1
|
||||
+2
-2
@@ -28,7 +28,7 @@ improve the documentation. Fix a typo, clarify an interface, add an
|
||||
example, anything goes!
|
||||
|
||||
The documentation is present in the [README][readme] and thorough the
|
||||
source code. On release, it gets updated on [GoDoc][godoc]. To make a
|
||||
source code. On release, it gets updated on [pkg.go.dev][pkg.go.dev]. To make a
|
||||
change to the documentation, create a pull request with your proposed
|
||||
changes. For simple changes like that, the easiest way to go is probably
|
||||
the "Fork this project and edit the file" button on Github, displayed at
|
||||
@@ -123,7 +123,7 @@ Checklist:
|
||||
|
||||
[issues-tracker]: https://github.com/pelletier/go-toml/issues
|
||||
[bug-report]: https://github.com/pelletier/go-toml/issues/new?template=bug_report.md
|
||||
[godoc]: https://godoc.org/github.com/pelletier/go-toml
|
||||
[pkg.go.dev]: https://pkg.go.dev/github.com/pelletier/go-toml
|
||||
[readme]: ./README.md
|
||||
[fork]: https://help.github.com/articles/fork-a-repo
|
||||
[pull-request]: https://help.github.com/en/articles/creating-a-pull-request
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
The bulk of github.com/pelletier/go-toml is distributed under the MIT license
|
||||
(see below), with the exception of localtime.go and localtime.test.go.
|
||||
Those two files have been copied over from Google's civil library at revision
|
||||
ed46f5086358513cf8c25f8e3f022cb838a49d66, and are distributed under the Apache
|
||||
2.0 license (see below).
|
||||
|
||||
|
||||
github.com/pelletier/go-toml:
|
||||
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 - 2017 Thomas Pelletier, Eric Anderton
|
||||
Copyright (c) 2013 - 2021 Thomas Pelletier, Eric Anderton
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -19,3 +29,219 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
|
||||
localtime.go, localtime_test.go:
|
||||
|
||||
Originals:
|
||||
https://raw.githubusercontent.com/googleapis/google-cloud-go/ed46f5086358513cf8c25f8e3f022cb838a49d66/civil/civil.go
|
||||
https://raw.githubusercontent.com/googleapis/google-cloud-go/ed46f5086358513cf8c25f8e3f022cb838a49d66/civil/civil_test.go
|
||||
Changes:
|
||||
* Renamed files from civil* to localtime*.
|
||||
* Package changed from civil to toml.
|
||||
* 'Local' prefix added to all structs.
|
||||
License:
|
||||
https://raw.githubusercontent.com/googleapis/google-cloud-go/ed46f5086358513cf8c25f8e3f022cb838a49d66/LICENSE
|
||||
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
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.
|
||||
|
||||
@@ -1,17 +1,42 @@
|
||||
# go-toml
|
||||
|
||||
Go library for the [TOML](https://github.com/mojombo/toml) format.
|
||||
Go library for the [TOML](https://toml.io/) format.
|
||||
|
||||
This library supports TOML version
|
||||
[v1.0.0-rc.1](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v1.0.0-rc.1.md)
|
||||
[v1.0.0-rc.3](https://toml.io/en/v1.0.0-rc.3)
|
||||
|
||||
[](http://godoc.org/github.com/pelletier/go-toml)
|
||||
[](https://pkg.go.dev/github.com/pelletier/go-toml)
|
||||
[](https://github.com/pelletier/go-toml/blob/master/LICENSE)
|
||||
[](https://dev.azure.com/pelletierthomas/go-toml-ci/_build/latest?definitionId=1&branchName=master)
|
||||
[](https://codecov.io/gh/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)
|
||||
|
||||
|
||||
## Development status
|
||||
|
||||
**ℹ️ Consider go-toml v2!**
|
||||
|
||||
The next version of go-toml is in [active development][v2-dev], and
|
||||
[nearing completion][v2-map].
|
||||
|
||||
Though technically in beta, v2 is already more tested, [fixes bugs][v1-bugs],
|
||||
and [much faster][v2-bench]. If you only need reading and writing TOML documents
|
||||
(majority of cases), those features are implemented and the API unlikely to
|
||||
change.
|
||||
|
||||
The remaining features (Document structure editing and tooling) will be added
|
||||
shortly. While pull-requests are welcome on v1, no active development is
|
||||
expected on it. When v2.0.0 is released, v1 will be deprecated.
|
||||
|
||||
👉 [go-toml v2][v2]
|
||||
|
||||
[v2]: https://github.com/pelletier/go-toml/tree/v2
|
||||
[v2-map]: https://github.com/pelletier/go-toml/discussions/506
|
||||
[v2-dev]: https://github.com/pelletier/go-toml/tree/v2
|
||||
[v1-bugs]: https://github.com/pelletier/go-toml/issues?q=is%3Aissue+is%3Aopen+label%3Av2-fixed
|
||||
[v2-bench]: https://github.com/pelletier/go-toml/tree/v2#benchmarks
|
||||
|
||||
## Features
|
||||
|
||||
Go-toml provides the following features for using data parsed from TOML documents:
|
||||
@@ -81,11 +106,11 @@ for ii, item := range results.Values() {
|
||||
## Documentation
|
||||
|
||||
The documentation and additional examples are available at
|
||||
[godoc.org](http://godoc.org/github.com/pelletier/go-toml).
|
||||
[pkg.go.dev](https://pkg.go.dev/github.com/pelletier/go-toml).
|
||||
|
||||
## Tools
|
||||
|
||||
Go-toml provides two handy command line tools:
|
||||
Go-toml provides three handy command line tools:
|
||||
|
||||
* `tomll`: Reads TOML files and lints them.
|
||||
|
||||
@@ -109,7 +134,7 @@ Go-toml provides two handy command line tools:
|
||||
|
||||
### Docker image
|
||||
|
||||
Those tools are also availble as a Docker image from
|
||||
Those tools are also available as a Docker image from
|
||||
[dockerhub](https://hub.docker.com/r/pelletier/go-toml). For example, to
|
||||
use `tomljson`:
|
||||
|
||||
@@ -148,4 +173,4 @@ this document. The last two major versions of Go are supported
|
||||
|
||||
## License
|
||||
|
||||
The MIT License (MIT). Read [LICENSE](LICENSE).
|
||||
The MIT License (MIT) + Apache 2.0. Read [LICENSE](LICENSE).
|
||||
|
||||
+16
-58
@@ -2,30 +2,6 @@ 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: []
|
||||
@@ -36,9 +12,9 @@ stages:
|
||||
vmImage: ubuntu-latest
|
||||
steps:
|
||||
- task: GoTool@0
|
||||
displayName: "Install Go 1.15"
|
||||
displayName: "Install Go 1.16"
|
||||
inputs:
|
||||
version: "1.15"
|
||||
version: "1.16"
|
||||
- task: Go@0
|
||||
displayName: "go fmt ./..."
|
||||
inputs:
|
||||
@@ -51,9 +27,9 @@ stages:
|
||||
vmImage: ubuntu-latest
|
||||
steps:
|
||||
- task: GoTool@0
|
||||
displayName: "Install Go 1.15"
|
||||
displayName: "Install Go 1.16"
|
||||
inputs:
|
||||
version: "1.15"
|
||||
version: "1.16"
|
||||
- task: Go@0
|
||||
displayName: "Generate coverage"
|
||||
inputs:
|
||||
@@ -71,37 +47,28 @@ stages:
|
||||
vmImage: ubuntu-latest
|
||||
steps:
|
||||
- task: GoTool@0
|
||||
displayName: "Install Go 1.15"
|
||||
displayName: "Install Go 1.16"
|
||||
inputs:
|
||||
version: "1.15"
|
||||
version: "1.16"
|
||||
- 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.16:
|
||||
goVersion: '1.16'
|
||||
imageName: 'ubuntu-latest'
|
||||
mac 1.16:
|
||||
goVersion: '1.16'
|
||||
imageName: 'macOS-latest'
|
||||
windows 1.16:
|
||||
goVersion: '1.16'
|
||||
imageName: 'windows-latest'
|
||||
linux 1.15:
|
||||
goVersion: '1.15'
|
||||
imageName: 'ubuntu-latest'
|
||||
@@ -111,15 +78,6 @@ stages:
|
||||
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:
|
||||
@@ -155,7 +113,7 @@ stages:
|
||||
- task: GoTool@0
|
||||
displayName: "Install Go"
|
||||
inputs:
|
||||
version: 1.15
|
||||
version: 1.16
|
||||
- task: Bash@3
|
||||
inputs:
|
||||
targetType: inline
|
||||
|
||||
@@ -137,6 +137,7 @@ func BenchmarkUnmarshalToml(b *testing.B) {
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
target := benchmarkDoc{}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
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=
|
||||
|
||||
+24
-6
@@ -6,6 +6,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -16,6 +17,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
multiLineArray := flag.Bool("multiLineArray", false, "sets up the linter to encode arrays with more than one element on multiple lines instead of one.")
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintln(os.Stderr, "tomll can be used in two ways:")
|
||||
fmt.Fprintln(os.Stderr, "Writing to STDIN and reading from STDOUT:")
|
||||
@@ -25,11 +27,16 @@ func main() {
|
||||
fmt.Fprintln(os.Stderr, " tomll a.toml b.toml c.toml")
|
||||
fmt.Fprintln(os.Stderr, "")
|
||||
fmt.Fprintln(os.Stderr, "When given a list of files, tomll will modify all files in place without asking.")
|
||||
fmt.Fprintln(os.Stderr, "When given a list of files, tomll will modify all files in place without asking.")
|
||||
fmt.Fprintln(os.Stderr, "")
|
||||
fmt.Fprintln(os.Stderr, "Flags:")
|
||||
fmt.Fprintln(os.Stderr, "-multiLineArray sets up the linter to encode arrays with more than one element on multiple lines instead of one.")
|
||||
}
|
||||
flag.Parse()
|
||||
|
||||
// read from stdin and print to stdout
|
||||
if flag.NArg() == 0 {
|
||||
s, err := lintReader(os.Stdin)
|
||||
s, err := lintReader(os.Stdin, *multiLineArray)
|
||||
if err != nil {
|
||||
io.WriteString(os.Stderr, err.Error())
|
||||
os.Exit(-1)
|
||||
@@ -38,7 +45,7 @@ func main() {
|
||||
} else {
|
||||
// otherwise modify a list of files
|
||||
for _, filename := range flag.Args() {
|
||||
s, err := lintFile(filename)
|
||||
s, err := lintFile(filename, *multiLineArray)
|
||||
if err != nil {
|
||||
io.WriteString(os.Stderr, err.Error())
|
||||
os.Exit(-1)
|
||||
@@ -48,18 +55,29 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func lintFile(filename string) (string, error) {
|
||||
func lintFile(filename string, multiLineArray bool) (string, error) {
|
||||
tree, err := toml.LoadFile(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return tree.String(), nil
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
if err := toml.NewEncoder(buf).ArraysWithOneElementPerLine(multiLineArray).Encode(tree); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
func lintReader(r io.Reader) (string, error) {
|
||||
func lintReader(r io.Reader, multiLineArray bool) (string, error) {
|
||||
tree, err := toml.LoadReader(r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return tree.String(), nil
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
if err := toml.NewEncoder(buf).ArraysWithOneElementPerLine(multiLineArray).Encode(tree); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
#!/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}
|
||||
@@ -1,5 +1,3 @@
|
||||
module github.com/pelletier/go-toml
|
||||
|
||||
go 1.12
|
||||
|
||||
require github.com/davecgh/go-spew v1.1.1
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
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/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
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.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=
|
||||
@@ -9,13 +9,10 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var dateRegexp *regexp.Regexp
|
||||
|
||||
// Define state functions
|
||||
type tomlLexStateFn func() tomlLexStateFn
|
||||
|
||||
@@ -216,18 +213,12 @@ func (l *tomlLexer) lexRvalue() tomlLexStateFn {
|
||||
break
|
||||
}
|
||||
|
||||
possibleDate := l.peekString(35)
|
||||
dateSubmatches := dateRegexp.FindStringSubmatch(possibleDate)
|
||||
if dateSubmatches != nil && dateSubmatches[0] != "" {
|
||||
l.fastForward(len(dateSubmatches[0]))
|
||||
if dateSubmatches[2] == "" { // no timezone information => local date
|
||||
return l.lexLocalDate
|
||||
}
|
||||
return l.lexDate
|
||||
if next == '+' || next == '-' {
|
||||
return l.lexNumber
|
||||
}
|
||||
|
||||
if next == '+' || next == '-' || isDigit(next) {
|
||||
return l.lexNumber
|
||||
if isDigit(next) {
|
||||
return l.lexDateTimeOrNumber
|
||||
}
|
||||
|
||||
return l.errorf("no value can start with %c", next)
|
||||
@@ -237,6 +228,32 @@ func (l *tomlLexer) lexRvalue() tomlLexStateFn {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *tomlLexer) lexDateTimeOrNumber() tomlLexStateFn {
|
||||
// Could be either a date/time, or a digit.
|
||||
// The options for date/times are:
|
||||
// YYYY-... => date or date-time
|
||||
// HH:... => time
|
||||
// Anything else should be a number.
|
||||
|
||||
lookAhead := l.peekString(5)
|
||||
if len(lookAhead) < 3 {
|
||||
return l.lexNumber()
|
||||
}
|
||||
|
||||
for idx, r := range lookAhead {
|
||||
if !isDigit(r) {
|
||||
if idx == 2 && r == ':' {
|
||||
return l.lexDateTimeOrTime()
|
||||
}
|
||||
if idx == 4 && r == '-' {
|
||||
return l.lexDateTimeOrTime()
|
||||
}
|
||||
return l.lexNumber()
|
||||
}
|
||||
}
|
||||
return l.lexNumber()
|
||||
}
|
||||
|
||||
func (l *tomlLexer) lexLeftCurlyBrace() tomlLexStateFn {
|
||||
l.next()
|
||||
l.emit(tokenLeftCurlyBrace)
|
||||
@@ -254,14 +271,245 @@ func (l *tomlLexer) lexRightCurlyBrace() tomlLexStateFn {
|
||||
return l.lexRvalue
|
||||
}
|
||||
|
||||
func (l *tomlLexer) lexDate() tomlLexStateFn {
|
||||
l.emit(tokenDate)
|
||||
func (l *tomlLexer) lexDateTimeOrTime() tomlLexStateFn {
|
||||
// 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
|
||||
|
||||
// we already know those two are digits
|
||||
l.next()
|
||||
l.next()
|
||||
|
||||
// Got 2 digits. At that point it could be either a time or a date(-time).
|
||||
|
||||
r := l.next()
|
||||
if r == ':' {
|
||||
return l.lexTime()
|
||||
}
|
||||
|
||||
return l.lexDateTime()
|
||||
}
|
||||
|
||||
func (l *tomlLexer) lexDateTime() tomlLexStateFn {
|
||||
// This state accepts an offset date-time, a local date-time, or a local date.
|
||||
//
|
||||
// v--- cursor
|
||||
// 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
|
||||
|
||||
// date
|
||||
|
||||
// already checked by lexRvalue
|
||||
l.next() // digit
|
||||
l.next() // -
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
r := l.next()
|
||||
if !isDigit(r) {
|
||||
return l.errorf("invalid month digit in date: %c", r)
|
||||
}
|
||||
}
|
||||
|
||||
r := l.next()
|
||||
if r != '-' {
|
||||
return l.errorf("expected - to separate month of a date, not %c", r)
|
||||
}
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
r := l.next()
|
||||
if !isDigit(r) {
|
||||
return l.errorf("invalid day digit in date: %c", r)
|
||||
}
|
||||
}
|
||||
|
||||
l.emit(tokenLocalDate)
|
||||
|
||||
r = l.peek()
|
||||
|
||||
if r == eof {
|
||||
|
||||
return l.lexRvalue
|
||||
}
|
||||
|
||||
if r != ' ' && r != 'T' {
|
||||
return l.errorf("incorrect date/time separation character: %c", r)
|
||||
}
|
||||
|
||||
if r == ' ' {
|
||||
lookAhead := l.peekString(3)[1:]
|
||||
if len(lookAhead) < 2 {
|
||||
return l.lexRvalue
|
||||
}
|
||||
for _, r := range lookAhead {
|
||||
if !isDigit(r) {
|
||||
return l.lexRvalue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
l.skip() // skip the T or ' '
|
||||
|
||||
// time
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
r := l.next()
|
||||
if !isDigit(r) {
|
||||
return l.errorf("invalid hour digit in time: %c", r)
|
||||
}
|
||||
}
|
||||
|
||||
r = l.next()
|
||||
if r != ':' {
|
||||
return l.errorf("time hour/minute separator should be :, not %c", r)
|
||||
}
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
r := l.next()
|
||||
if !isDigit(r) {
|
||||
return l.errorf("invalid minute digit in time: %c", r)
|
||||
}
|
||||
}
|
||||
|
||||
r = l.next()
|
||||
if r != ':' {
|
||||
return l.errorf("time minute/second separator should be :, not %c", r)
|
||||
}
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
r := l.next()
|
||||
if !isDigit(r) {
|
||||
return l.errorf("invalid second digit in time: %c", r)
|
||||
}
|
||||
}
|
||||
|
||||
r = l.peek()
|
||||
if r == '.' {
|
||||
l.next()
|
||||
r := l.next()
|
||||
if !isDigit(r) {
|
||||
return l.errorf("expected at least one digit in time's fraction, not %c", r)
|
||||
}
|
||||
|
||||
for {
|
||||
r := l.peek()
|
||||
if !isDigit(r) {
|
||||
break
|
||||
}
|
||||
l.next()
|
||||
}
|
||||
}
|
||||
|
||||
l.emit(tokenLocalTime)
|
||||
|
||||
return l.lexTimeOffset
|
||||
|
||||
}
|
||||
|
||||
func (l *tomlLexer) lexTimeOffset() tomlLexStateFn {
|
||||
// potential offset
|
||||
|
||||
// Z
|
||||
// -07:00
|
||||
// +07:00
|
||||
// nothing
|
||||
|
||||
r := l.peek()
|
||||
|
||||
if r == 'Z' {
|
||||
l.next()
|
||||
l.emit(tokenTimeOffset)
|
||||
} else if r == '+' || r == '-' {
|
||||
l.next()
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
r := l.next()
|
||||
if !isDigit(r) {
|
||||
return l.errorf("invalid hour digit in time offset: %c", r)
|
||||
}
|
||||
}
|
||||
|
||||
r = l.next()
|
||||
if r != ':' {
|
||||
return l.errorf("time offset hour/minute separator should be :, not %c", r)
|
||||
}
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
r := l.next()
|
||||
if !isDigit(r) {
|
||||
return l.errorf("invalid minute digit in time offset: %c", r)
|
||||
}
|
||||
}
|
||||
|
||||
l.emit(tokenTimeOffset)
|
||||
}
|
||||
|
||||
return l.lexRvalue
|
||||
}
|
||||
|
||||
func (l *tomlLexer) lexLocalDate() tomlLexStateFn {
|
||||
l.emit(tokenLocalDate)
|
||||
func (l *tomlLexer) lexTime() tomlLexStateFn {
|
||||
// v--- cursor
|
||||
// 07:32:00
|
||||
// 00:32:00.999999
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
r := l.next()
|
||||
if !isDigit(r) {
|
||||
return l.errorf("invalid minute digit in time: %c", r)
|
||||
}
|
||||
}
|
||||
|
||||
r := l.next()
|
||||
if r != ':' {
|
||||
return l.errorf("time minute/second separator should be :, not %c", r)
|
||||
}
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
r := l.next()
|
||||
if !isDigit(r) {
|
||||
return l.errorf("invalid second digit in time: %c", r)
|
||||
}
|
||||
}
|
||||
|
||||
r = l.peek()
|
||||
if r == '.' {
|
||||
l.next()
|
||||
r := l.next()
|
||||
if !isDigit(r) {
|
||||
return l.errorf("expected at least one digit in time's fraction, not %c", r)
|
||||
}
|
||||
|
||||
for {
|
||||
r := l.peek()
|
||||
if !isDigit(r) {
|
||||
break
|
||||
}
|
||||
l.next()
|
||||
}
|
||||
}
|
||||
|
||||
l.emit(tokenLocalTime)
|
||||
return l.lexRvalue
|
||||
|
||||
}
|
||||
|
||||
func (l *tomlLexer) lexTrue() tomlLexStateFn {
|
||||
@@ -767,30 +1015,6 @@ func (l *tomlLexer) run() {
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
// 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
|
||||
func lexToml(inputBytes []byte) []token {
|
||||
runes := bytes.Runes(inputBytes)
|
||||
|
||||
+317
-48
@@ -1,17 +1,63 @@
|
||||
package toml
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"text/tabwriter"
|
||||
)
|
||||
|
||||
func testFlow(t *testing.T, input string, expectedFlow []token) {
|
||||
tokens := lexToml([]byte(input))
|
||||
|
||||
if !reflect.DeepEqual(tokens, expectedFlow) {
|
||||
t.Fatalf("Different flows.\nExpected:\n%v\nGot:\n%v", expectedFlow, tokens)
|
||||
diffFlowsColumnsFatal(t, expectedFlow, tokens)
|
||||
}
|
||||
}
|
||||
|
||||
func diffFlowsColumnsFatal(t *testing.T, expectedFlow []token, actualFlow []token) {
|
||||
max := len(expectedFlow)
|
||||
if len(actualFlow) > max {
|
||||
max = len(actualFlow)
|
||||
}
|
||||
|
||||
b := &bytes.Buffer{}
|
||||
w := tabwriter.NewWriter(b, 0, 0, 1, ' ', tabwriter.Debug)
|
||||
|
||||
fmt.Fprintln(w, "expected\tT\tP\tactual\tT\tP\tdiff")
|
||||
|
||||
for i := 0; i < max; i++ {
|
||||
expected := ""
|
||||
expectedType := ""
|
||||
expectedPos := ""
|
||||
if i < len(expectedFlow) {
|
||||
expected = fmt.Sprintf("%s", expectedFlow[i])
|
||||
expectedType = fmt.Sprintf("%s", expectedFlow[i].typ)
|
||||
expectedPos = expectedFlow[i].Position.String()
|
||||
}
|
||||
actual := ""
|
||||
actualType := ""
|
||||
actualPos := ""
|
||||
if i < len(actualFlow) {
|
||||
actual = fmt.Sprintf("%s", actualFlow[i])
|
||||
actualType = fmt.Sprintf("%s", actualFlow[i].typ)
|
||||
actualPos = actualFlow[i].Position.String()
|
||||
}
|
||||
different := ""
|
||||
if i >= len(expectedFlow) {
|
||||
different = "+"
|
||||
} else if i >= len(actualFlow) {
|
||||
different = "-"
|
||||
} else if !reflect.DeepEqual(expectedFlow[i], actualFlow[i]) {
|
||||
different = "x"
|
||||
}
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", expected, expectedType, expectedPos, actual, actualType, actualPos, different)
|
||||
}
|
||||
w.Flush()
|
||||
t.Errorf("Different flows:\n%s", b.String())
|
||||
}
|
||||
|
||||
func TestValidKeyGroup(t *testing.T) {
|
||||
testFlow(t, "[hello world]", []token{
|
||||
{Position{1, 1}, tokenLeftBracket, "["},
|
||||
@@ -298,57 +344,280 @@ func TestKeyEqualArrayBoolsWithComments(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestDateRegexp(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"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",
|
||||
}
|
||||
|
||||
for name, value := range cases {
|
||||
if dateRegexp.FindString(value) == "" {
|
||||
t.Error("failed date regexp test", name)
|
||||
}
|
||||
}
|
||||
if dateRegexp.FindString("1979-05-27 07:32:00Z") == "" {
|
||||
t.Error("space delimiter lexing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeyEqualDate(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27T07:32:00Z", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenDate, "1979-05-27T07:32:00Z"},
|
||||
{Position{1, 27}, tokenEOF, ""},
|
||||
t.Run("local date time", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27T07:32:00", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenLocalTime, "07:32:00"},
|
||||
{Position{1, 26}, tokenEOF, ""},
|
||||
})
|
||||
})
|
||||
testFlow(t, "foo = 1979-05-27T00:32:00-07:00", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenDate, "1979-05-27T00:32:00-07:00"},
|
||||
{Position{1, 32}, tokenEOF, ""},
|
||||
|
||||
t.Run("local date time space", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27 07:32:00", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenLocalTime, "07:32:00"},
|
||||
{Position{1, 26}, tokenEOF, ""},
|
||||
})
|
||||
})
|
||||
testFlow(t, "foo = 1979-05-27T00:32:00.999999-07:00", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenDate, "1979-05-27T00:32:00.999999-07:00"},
|
||||
{Position{1, 39}, tokenEOF, ""},
|
||||
|
||||
t.Run("local date time fraction", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27T00:32:00.999999", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenLocalTime, "00:32:00.999999"},
|
||||
{Position{1, 33}, 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, ""},
|
||||
|
||||
t.Run("local date time fraction space", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27 00:32:00.999999", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenLocalTime, "00:32:00.999999"},
|
||||
{Position{1, 33}, tokenEOF, ""},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("offset date-time utc", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27T07:32:00Z", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenLocalTime, "07:32:00"},
|
||||
{Position{1, 26}, tokenTimeOffset, "Z"},
|
||||
{Position{1, 27}, tokenEOF, ""},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("offset date-time -07:00", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27T00:32:00-07:00", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenLocalTime, "00:32:00"},
|
||||
{Position{1, 26}, tokenTimeOffset, "-07:00"},
|
||||
{Position{1, 32}, tokenEOF, ""},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("offset date-time fractions -07:00", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27T00:32:00.999999-07:00", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenLocalTime, "00:32:00.999999"},
|
||||
{Position{1, 33}, tokenTimeOffset, "-07:00"},
|
||||
{Position{1, 39}, tokenEOF, ""},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("offset date-time space separated utc", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27 07:32:00Z", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenLocalTime, "07:32:00"},
|
||||
{Position{1, 26}, tokenTimeOffset, "Z"},
|
||||
{Position{1, 27}, tokenEOF, ""},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("offset date-time space separated offset", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27 00:32:00-07:00", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenLocalTime, "00:32:00"},
|
||||
{Position{1, 26}, tokenTimeOffset, "-07:00"},
|
||||
{Position{1, 32}, tokenEOF, ""},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("offset date-time space separated fraction offset", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27 00:32:00.999999-07:00", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenLocalTime, "00:32:00.999999"},
|
||||
{Position{1, 33}, tokenTimeOffset, "-07:00"},
|
||||
{Position{1, 39}, tokenEOF, ""},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local date", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 17}, tokenEOF, ""},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local time", func(t *testing.T) {
|
||||
testFlow(t, "foo = 07:32:00", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalTime, "07:32:00"},
|
||||
{Position{1, 15}, tokenEOF, ""},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local time fraction", func(t *testing.T) {
|
||||
testFlow(t, "foo = 00:32:00.999999", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalTime, "00:32:00.999999"},
|
||||
{Position{1, 22}, tokenEOF, ""},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local time invalid minute digit", func(t *testing.T) {
|
||||
testFlow(t, "foo = 00:3x:00.999999", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenError, "invalid minute digit in time: x"},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local time invalid minute/second digit", func(t *testing.T) {
|
||||
testFlow(t, "foo = 00:30x00.999999", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenError, "time minute/second separator should be :, not x"},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local time invalid second digit", func(t *testing.T) {
|
||||
testFlow(t, "foo = 00:30:x0.999999", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenError, "invalid second digit in time: x"},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local time invalid second digit", func(t *testing.T) {
|
||||
testFlow(t, "foo = 00:30:00.F", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenError, "expected at least one digit in time's fraction, not F"},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local date-time invalid minute digit", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27 00:3x:00.999999", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenError, "invalid minute digit in time: x"},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local date-time invalid hour digit", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27T0x:30:00.999999", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenError, "invalid hour digit in time: x"},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local date-time invalid hour digit", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27T00x30:00.999999", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenError, "time hour/minute separator should be :, not x"},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local date-time invalid minute/second digit", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27 00:30x00.999999", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenError, "time minute/second separator should be :, not x"},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local date-time invalid second digit", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27 00:30:x0.999999", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenError, "invalid second digit in time: x"},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local date-time invalid fraction", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27 00:30:00.F", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenError, "expected at least one digit in time's fraction, not F"},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local date-time invalid month-date separator", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05X27 00:30:00.F", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenError, "expected - to separate month of a date, not X"},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local date-time extra whitespace", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27 ", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 19}, tokenEOF, ""},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("local date-time extra whitespace", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27 ", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 22}, tokenEOF, ""},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("offset date-time space separated offset", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27 00:32:00-0x:00", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenLocalTime, "00:32:00"},
|
||||
{Position{1, 26}, tokenError, "invalid hour digit in time offset: x"},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("offset date-time space separated offset", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27 00:32:00-07x00", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenLocalTime, "00:32:00"},
|
||||
{Position{1, 26}, tokenError, "time offset hour/minute separator should be :, not x"},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("offset date-time space separated offset", func(t *testing.T) {
|
||||
testFlow(t, "foo = 1979-05-27 00:32:00-07:x0", []token{
|
||||
{Position{1, 1}, tokenKey, "foo"},
|
||||
{Position{1, 5}, tokenEqual, "="},
|
||||
{Position{1, 7}, tokenLocalDate, "1979-05-27"},
|
||||
{Position{1, 18}, tokenLocalTime, "00:32:00"},
|
||||
{Position{1, 26}, tokenError, "invalid minute digit in time offset: x"},
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+8
-2
@@ -1,6 +1,12 @@
|
||||
// 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.
|
||||
//
|
||||
// Copied over from Google's civil to avoid pulling all the Google dependencies.
|
||||
// Originals:
|
||||
// https://raw.githubusercontent.com/googleapis/google-cloud-go/ed46f5086358513cf8c25f8e3f022cb838a49d66/civil/civil.go
|
||||
// Changes:
|
||||
// * Renamed files from civil* to localtime*.
|
||||
// * Package changed from civil to toml.
|
||||
// * 'Local' prefix added to all structs.
|
||||
//
|
||||
// Copyright 2016 Google LLC
|
||||
//
|
||||
|
||||
@@ -1,3 +1,13 @@
|
||||
// Implementation of TOML's local date/time.
|
||||
//
|
||||
// Copied over from Google's civil to avoid pulling all the Google dependencies.
|
||||
// Originals:
|
||||
// https://raw.githubusercontent.com/googleapis/google-cloud-go/ed46f5086358513cf8c25f8e3f022cb838a49d66/civil/civil_test.go
|
||||
// Changes:
|
||||
// * Renamed files from civil* to localtime*.
|
||||
// * Package changed from civil to toml.
|
||||
// * 'Local' prefix added to all structs.
|
||||
//
|
||||
// Copyright 2016 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
||||
+50
-11
@@ -18,6 +18,7 @@ const (
|
||||
tagFieldComment = "comment"
|
||||
tagCommented = "commented"
|
||||
tagMultiline = "multiline"
|
||||
tagLiteral = "literal"
|
||||
tagDefault = "default"
|
||||
)
|
||||
|
||||
@@ -27,6 +28,7 @@ type tomlOpts struct {
|
||||
comment string
|
||||
commented bool
|
||||
multiline bool
|
||||
literal bool
|
||||
include bool
|
||||
omitempty bool
|
||||
defaultValue string
|
||||
@@ -46,6 +48,7 @@ type annotation struct {
|
||||
comment string
|
||||
commented string
|
||||
multiline string
|
||||
literal string
|
||||
defaultValue string
|
||||
}
|
||||
|
||||
@@ -54,15 +57,16 @@ var annotationDefault = annotation{
|
||||
comment: tagFieldComment,
|
||||
commented: tagCommented,
|
||||
multiline: tagMultiline,
|
||||
literal: tagLiteral,
|
||||
defaultValue: tagDefault,
|
||||
}
|
||||
|
||||
type marshalOrder int
|
||||
type MarshalOrder int
|
||||
|
||||
// Orders the Encoder can write the fields to the output stream.
|
||||
const (
|
||||
// Sort fields alphabetically.
|
||||
OrderAlphabetical marshalOrder = iota + 1
|
||||
OrderAlphabetical MarshalOrder = iota + 1
|
||||
// Preserve the order the fields are encountered. For example, the order of fields in
|
||||
// a struct.
|
||||
OrderPreserve
|
||||
@@ -254,11 +258,12 @@ type Encoder struct {
|
||||
w io.Writer
|
||||
encOpts
|
||||
annotation
|
||||
line int
|
||||
col int
|
||||
order marshalOrder
|
||||
promoteAnon bool
|
||||
indentation string
|
||||
line int
|
||||
col int
|
||||
order MarshalOrder
|
||||
promoteAnon bool
|
||||
compactComments bool
|
||||
indentation string
|
||||
}
|
||||
|
||||
// NewEncoder returns a new encoder that writes to w.
|
||||
@@ -317,7 +322,7 @@ func (e *Encoder) ArraysWithOneElementPerLine(v bool) *Encoder {
|
||||
}
|
||||
|
||||
// Order allows to change in which order fields will be written to the output stream.
|
||||
func (e *Encoder) Order(ord marshalOrder) *Encoder {
|
||||
func (e *Encoder) Order(ord MarshalOrder) *Encoder {
|
||||
e.order = ord
|
||||
return e
|
||||
}
|
||||
@@ -365,6 +370,12 @@ func (e *Encoder) PromoteAnonymous(promote bool) *Encoder {
|
||||
return e
|
||||
}
|
||||
|
||||
// CompactComments removes the new line before each comment in the tree.
|
||||
func (e *Encoder) CompactComments(cc bool) *Encoder {
|
||||
e.compactComments = cc
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *Encoder) marshal(v interface{}) ([]byte, error) {
|
||||
// Check if indentation is valid
|
||||
for _, char := range e.indentation {
|
||||
@@ -404,7 +415,7 @@ func (e *Encoder) marshal(v interface{}) ([]byte, error) {
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
_, err = t.writeToOrdered(&buf, "", "", 0, e.arraysOneElementPerLine, e.order, e.indentation, false)
|
||||
_, err = t.writeToOrdered(&buf, "", "", 0, e.arraysOneElementPerLine, e.order, e.indentation, e.compactComments, false)
|
||||
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
@@ -442,6 +453,7 @@ func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, er
|
||||
Comment: opts.comment,
|
||||
Commented: opts.commented,
|
||||
Multiline: opts.multiline,
|
||||
Literal: opts.literal,
|
||||
}, val)
|
||||
}
|
||||
}
|
||||
@@ -586,6 +598,7 @@ func (e *Encoder) wrapTomlValue(val interface{}, parent *Tree) interface{} {
|
||||
_, isTree := val.(*Tree)
|
||||
_, isTreeS := val.([]*Tree)
|
||||
if isTree || isTreeS {
|
||||
e.line++
|
||||
return val
|
||||
}
|
||||
|
||||
@@ -830,7 +843,21 @@ func (d *Decoder) valueFromTree(mtype reflect.Type, tval *Tree, mval1 *reflect.V
|
||||
case reflect.Int32:
|
||||
val, err = strconv.ParseInt(opts.defaultValue, 10, 32)
|
||||
case reflect.Int64:
|
||||
val, err = strconv.ParseInt(opts.defaultValue, 10, 64)
|
||||
// Check if the provided number has a non-numeric extension.
|
||||
var hasExtension bool
|
||||
if len(opts.defaultValue) > 0 {
|
||||
lastChar := opts.defaultValue[len(opts.defaultValue)-1]
|
||||
if lastChar < '0' || lastChar > '9' {
|
||||
hasExtension = true
|
||||
}
|
||||
}
|
||||
// If the value is a time.Duration with extension, parse as duration.
|
||||
// If the value is an int64 or a time.Duration without extension, parse as number.
|
||||
if hasExtension && mvalf.Type().String() == "time.Duration" {
|
||||
val, err = time.ParseDuration(opts.defaultValue)
|
||||
} else {
|
||||
val, err = strconv.ParseInt(opts.defaultValue, 10, 64)
|
||||
}
|
||||
case reflect.Float32:
|
||||
val, err = strconv.ParseFloat(opts.defaultValue, 32)
|
||||
case reflect.Float64:
|
||||
@@ -1004,8 +1031,18 @@ func (d *Decoder) valueFromToml(mtype reflect.Type, tval interface{}, mval1 *ref
|
||||
return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to a slice", tval, tval)
|
||||
default:
|
||||
d.visitor.visit()
|
||||
mvalPtr := reflect.New(mtype)
|
||||
|
||||
// Check if pointer to value implements the Unmarshaler interface.
|
||||
if isCustomUnmarshaler(mvalPtr.Type()) {
|
||||
if err := callCustomUnmarshaler(mvalPtr, tval); err != nil {
|
||||
return reflect.ValueOf(nil), fmt.Errorf("unmarshal toml: %v", err)
|
||||
}
|
||||
return mvalPtr.Elem(), nil
|
||||
}
|
||||
|
||||
// Check if pointer to value implements the encoding.TextUnmarshaler.
|
||||
if mvalPtr := reflect.New(mtype); isTextUnmarshaler(mvalPtr.Type()) && !isTimeType(mtype) {
|
||||
if isTextUnmarshaler(mvalPtr.Type()) && !isTimeType(mtype) {
|
||||
if err := d.unmarshalText(tval, mvalPtr); err != nil {
|
||||
return reflect.ValueOf(nil), fmt.Errorf("unmarshal text: %v", err)
|
||||
}
|
||||
@@ -1144,6 +1181,7 @@ func tomlOptions(vf reflect.StructField, an annotation) tomlOpts {
|
||||
}
|
||||
commented, _ := strconv.ParseBool(vf.Tag.Get(an.commented))
|
||||
multiline, _ := strconv.ParseBool(vf.Tag.Get(an.multiline))
|
||||
literal, _ := strconv.ParseBool(vf.Tag.Get(an.literal))
|
||||
defaultValue := vf.Tag.Get(tagDefault)
|
||||
result := tomlOpts{
|
||||
name: vf.Name,
|
||||
@@ -1151,6 +1189,7 @@ func tomlOptions(vf reflect.StructField, an annotation) tomlOpts {
|
||||
comment: comment,
|
||||
commented: commented,
|
||||
multiline: multiline,
|
||||
literal: literal,
|
||||
include: true,
|
||||
omitempty: false,
|
||||
defaultValue: defaultValue,
|
||||
|
||||
+157
-14
@@ -3,6 +3,7 @@ package toml
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@@ -1293,6 +1294,32 @@ NonCommented = "Not commented line"
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalMultilineLiteral(t *testing.T) {
|
||||
type Doc struct {
|
||||
Value string `multiline:"true" literal:"true"`
|
||||
}
|
||||
|
||||
d := Doc{
|
||||
Value: "hello\nworld\ttest\nend",
|
||||
}
|
||||
|
||||
expected := []byte(`Value = '''
|
||||
hello
|
||||
world test
|
||||
end
|
||||
'''
|
||||
`)
|
||||
|
||||
b, err := Marshal(d)
|
||||
if err != nil {
|
||||
t.Fatal("unexpected error")
|
||||
}
|
||||
|
||||
if !bytes.Equal(b, expected) {
|
||||
t.Errorf("Bad marshal: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expected, b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalNonPrimitiveTypeCommented(t *testing.T) {
|
||||
expectedToml := []byte(`
|
||||
# [CommentedMapField]
|
||||
@@ -1449,6 +1476,47 @@ commented out"""
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompactComments(t *testing.T) {
|
||||
expected := []byte(`
|
||||
[first-section]
|
||||
# comment for first-key
|
||||
first-key = 1
|
||||
# comment for second-key
|
||||
second-key = "value"
|
||||
# comment for commented third-key
|
||||
# third-key = []
|
||||
|
||||
[second-section]
|
||||
# comment for first-key
|
||||
first-key = 2
|
||||
# comment for second-key
|
||||
second-key = "another value"
|
||||
# comment for commented third-key
|
||||
# third-key = ["value1", "value2"]
|
||||
`)
|
||||
type Settings struct {
|
||||
FirstKey int `toml:"first-key" comment:"comment for first-key"`
|
||||
SecondKey string `toml:"second-key" comment:"comment for second-key"`
|
||||
ThirdKey []string `toml:"third-key" comment:"comment for commented third-key" commented:"true"`
|
||||
}
|
||||
type Config struct {
|
||||
FirstSection Settings `toml:"first-section"`
|
||||
SecondSection Settings `toml:"second-section"`
|
||||
}
|
||||
data := Config{
|
||||
FirstSection: Settings{1, "value", []string{}},
|
||||
SecondSection: Settings{2, "another value", []string{"value1", "value2"}},
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
if err := NewEncoder(buf).CompactComments(true).Encode(data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(expected, buf.Bytes()) {
|
||||
t.Errorf("Bad marshal: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expected, buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
type mapsTestStruct struct {
|
||||
Simple map[string]string
|
||||
Paths map[string]string
|
||||
@@ -2285,20 +2353,22 @@ func TestUnmarshalDefault(t *testing.T) {
|
||||
type aliasUint uint
|
||||
|
||||
var doc struct {
|
||||
StringField string `default:"a"`
|
||||
BoolField bool `default:"true"`
|
||||
UintField uint `default:"1"`
|
||||
Uint8Field uint8 `default:"8"`
|
||||
Uint16Field uint16 `default:"16"`
|
||||
Uint32Field uint32 `default:"32"`
|
||||
Uint64Field uint64 `default:"64"`
|
||||
IntField int `default:"-1"`
|
||||
Int8Field int8 `default:"-8"`
|
||||
Int16Field int16 `default:"-16"`
|
||||
Int32Field int32 `default:"-32"`
|
||||
Int64Field int64 `default:"-64"`
|
||||
Float32Field float32 `default:"32.1"`
|
||||
Float64Field float64 `default:"64.1"`
|
||||
StringField string `default:"a"`
|
||||
BoolField bool `default:"true"`
|
||||
UintField uint `default:"1"`
|
||||
Uint8Field uint8 `default:"8"`
|
||||
Uint16Field uint16 `default:"16"`
|
||||
Uint32Field uint32 `default:"32"`
|
||||
Uint64Field uint64 `default:"64"`
|
||||
IntField int `default:"-1"`
|
||||
Int8Field int8 `default:"-8"`
|
||||
Int16Field int16 `default:"-16"`
|
||||
Int32Field int32 `default:"-32"`
|
||||
Int64Field int64 `default:"-64"`
|
||||
Float32Field float32 `default:"32.1"`
|
||||
Float64Field float64 `default:"64.1"`
|
||||
DurationField time.Duration `default:"120ms"`
|
||||
DurationField2 time.Duration `default:"120000000"`
|
||||
NonEmbeddedStruct struct {
|
||||
StringField string `default:"b"`
|
||||
}
|
||||
@@ -2352,6 +2422,12 @@ func TestUnmarshalDefault(t *testing.T) {
|
||||
if doc.Float64Field != 64.1 {
|
||||
t.Errorf("Float64Field should be 64.1, not %f", doc.Float64Field)
|
||||
}
|
||||
if doc.DurationField != 120*time.Millisecond {
|
||||
t.Errorf("DurationField should be 120ms, not %s", doc.DurationField.String())
|
||||
}
|
||||
if doc.DurationField2 != 120*time.Millisecond {
|
||||
t.Errorf("DurationField2 should be 120000000, not %d", doc.DurationField2)
|
||||
}
|
||||
if doc.NonEmbeddedStruct.StringField != "b" {
|
||||
t.Errorf("StringField should be \"b\", not %s", doc.NonEmbeddedStruct.StringField)
|
||||
}
|
||||
@@ -2407,6 +2483,17 @@ func TestUnmarshalDefaultFailureFloat64(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalDefaultFailureDuration(t *testing.T) {
|
||||
var doc struct {
|
||||
Field time.Duration `default:"blah"`
|
||||
}
|
||||
|
||||
err := Unmarshal([]byte(``), &doc)
|
||||
if err == nil {
|
||||
t.Fatal("should error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalDefaultFailureUnsupported(t *testing.T) {
|
||||
var doc struct {
|
||||
Field struct{} `default:"blah"`
|
||||
@@ -3976,3 +4063,59 @@ func TestGithubIssue431(t *testing.T) {
|
||||
t.Errorf("UnmarshalTOML should not have been called")
|
||||
}
|
||||
}
|
||||
|
||||
type durationString struct {
|
||||
time.Duration
|
||||
}
|
||||
|
||||
func (d *durationString) UnmarshalTOML(v interface{}) error {
|
||||
d.Duration = 10 * time.Second
|
||||
return nil
|
||||
}
|
||||
|
||||
type config437Error struct {
|
||||
}
|
||||
|
||||
func (e *config437Error) UnmarshalTOML(v interface{}) error {
|
||||
return errors.New("expected")
|
||||
}
|
||||
|
||||
type config437 struct {
|
||||
HTTP struct {
|
||||
PingTimeout durationString `toml:"PingTimeout"`
|
||||
ErrorField config437Error
|
||||
} `toml:"HTTP"`
|
||||
}
|
||||
|
||||
func TestGithubIssue437(t *testing.T) {
|
||||
src := `
|
||||
[HTTP]
|
||||
PingTimeout = "32m"
|
||||
`
|
||||
cfg := &config437{}
|
||||
cfg.HTTP.PingTimeout = durationString{time.Second}
|
||||
|
||||
r := strings.NewReader(src)
|
||||
err := NewDecoder(r).Decode(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected errors %s", err)
|
||||
}
|
||||
expected := durationString{10 * time.Second}
|
||||
if cfg.HTTP.PingTimeout != expected {
|
||||
t.Fatalf("expected '%s', got '%s'", expected, cfg.HTTP.PingTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLeafUnmarshalerError(t *testing.T) {
|
||||
src := `
|
||||
[HTTP]
|
||||
ErrorField = "foo"
|
||||
`
|
||||
cfg := &config437{}
|
||||
|
||||
r := strings.NewReader(src)
|
||||
err := NewDecoder(r).Decode(cfg)
|
||||
if err == nil {
|
||||
t.Fatalf("error was expected")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -231,19 +230,38 @@ func (p *tomlParser) parseAssign() tomlParserStateFn {
|
||||
return p.parseStart
|
||||
}
|
||||
|
||||
var numberUnderscoreInvalidRegexp *regexp.Regexp
|
||||
var hexNumberUnderscoreInvalidRegexp *regexp.Regexp
|
||||
var errInvalidUnderscore = errors.New("invalid use of _ in number")
|
||||
|
||||
func numberContainsInvalidUnderscore(value string) error {
|
||||
if numberUnderscoreInvalidRegexp.MatchString(value) {
|
||||
return errors.New("invalid use of _ in number")
|
||||
// For large numbers, you may use underscores between digits to enhance
|
||||
// readability. Each underscore must be surrounded by at least one digit on
|
||||
// each side.
|
||||
|
||||
hasBefore := false
|
||||
for idx, r := range value {
|
||||
if r == '_' {
|
||||
if !hasBefore || idx+1 >= len(value) {
|
||||
// can't end with an underscore
|
||||
return errInvalidUnderscore
|
||||
}
|
||||
}
|
||||
hasBefore = isDigit(r)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var errInvalidUnderscoreHex = errors.New("invalid use of _ in hex number")
|
||||
|
||||
func hexNumberContainsInvalidUnderscore(value string) error {
|
||||
if hexNumberUnderscoreInvalidRegexp.MatchString(value) {
|
||||
return errors.New("invalid use of _ in hex number")
|
||||
hasBefore := false
|
||||
for idx, r := range value {
|
||||
if r == '_' {
|
||||
if !hasBefore || idx+1 >= len(value) {
|
||||
// can't end with an underscore
|
||||
return errInvalidUnderscoreHex
|
||||
}
|
||||
}
|
||||
hasBefore = isHexDigit(r)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -322,42 +340,44 @@ func (p *tomlParser) parseRvalue() interface{} {
|
||||
p.raiseError(tok, "%s", err)
|
||||
}
|
||||
return val
|
||||
case tokenDate:
|
||||
layout := time.RFC3339Nano
|
||||
if !strings.Contains(tok.val, "T") {
|
||||
layout = strings.Replace(layout, "T", " ", 1)
|
||||
}
|
||||
val, err := time.ParseInLocation(layout, tok.val, time.UTC)
|
||||
case tokenLocalTime:
|
||||
val, err := ParseLocalTime(tok.val)
|
||||
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
|
||||
// a local date may be followed by:
|
||||
// * nothing: this is a local date
|
||||
// * a local time: this is a local date-time
|
||||
|
||||
next := p.peek()
|
||||
if next == nil || next.typ != tokenLocalTime {
|
||||
val, err := ParseLocalDate(tok.val)
|
||||
if err != nil {
|
||||
p.raiseError(tok, "%s", err)
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
var val interface{}
|
||||
var err error
|
||||
localDate := tok
|
||||
localTime := p.getToken()
|
||||
|
||||
if isDateTime {
|
||||
val, err = ParseLocalDateTime(v)
|
||||
} else if isTime {
|
||||
val, err = ParseLocalTime(v)
|
||||
} else {
|
||||
val, err = ParseLocalDate(v)
|
||||
next = p.peek()
|
||||
if next == nil || next.typ != tokenTimeOffset {
|
||||
v := localDate.val + "T" + localTime.val
|
||||
val, err := ParseLocalDateTime(v)
|
||||
if err != nil {
|
||||
p.raiseError(tok, "%s", err)
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
offset := p.getToken()
|
||||
|
||||
layout := time.RFC3339Nano
|
||||
v := localDate.val + "T" + localTime.val + offset.val
|
||||
val, err := time.ParseInLocation(layout, v, time.UTC)
|
||||
if err != nil {
|
||||
p.raiseError(tok, "%s", err)
|
||||
}
|
||||
@@ -370,10 +390,10 @@ func (p *tomlParser) parseRvalue() interface{} {
|
||||
p.raiseError(tok, "cannot have multiple equals for the same key")
|
||||
case tokenError:
|
||||
p.raiseError(tok, "%s", tok)
|
||||
default:
|
||||
panic(fmt.Errorf("unhandled token: %v", tok))
|
||||
}
|
||||
|
||||
p.raiseError(tok, "never reached")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -486,8 +506,3 @@ func parseToml(flow []token) *Tree {
|
||||
parser.run()
|
||||
return result
|
||||
}
|
||||
|
||||
func init() {
|
||||
numberUnderscoreInvalidRegexp = regexp.MustCompile(`([^\d]_|_[^\d])|_$|^_`)
|
||||
hexNumberUnderscoreInvalidRegexp = regexp.MustCompile(`(^0x_)|([^\da-f]_|_[^\da-f])|_$|^_`)
|
||||
}
|
||||
|
||||
+29
-3
@@ -6,8 +6,6 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
func assertSubTree(t *testing.T, path []string, tree *Tree, err error, ref map[string]interface{}) {
|
||||
@@ -39,7 +37,7 @@ func assertSubTree(t *testing.T, path []string, tree *Tree, err error, ref map[s
|
||||
}
|
||||
|
||||
func assertTree(t *testing.T, tree *Tree, err error, ref map[string]interface{}) {
|
||||
t.Log("Asserting tree:\n", spew.Sdump(tree))
|
||||
t.Logf("Asserting tree:\n (%T)(%p)(%+v)", tree, tree, tree)
|
||||
assertSubTree(t, []string{}, tree, err, ref)
|
||||
t.Log("Finished tree assertion.")
|
||||
}
|
||||
@@ -274,6 +272,34 @@ func TestLocalDate(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestLocalDateError(t *testing.T) {
|
||||
_, err := Load("a = 2020-09-31")
|
||||
if err == nil {
|
||||
t.Fatalf("should error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalTimeError(t *testing.T) {
|
||||
_, err := Load("a = 07:99:00")
|
||||
if err == nil {
|
||||
t.Fatalf("should error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalDateTimeError(t *testing.T) {
|
||||
_, err := Load("a = 2020-09-31T07:99:00")
|
||||
if err == nil {
|
||||
t.Fatalf("should error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDateTimeOffsetError(t *testing.T) {
|
||||
_, err := Load("a = 2020-09-31T07:99:00Z")
|
||||
if err == nil {
|
||||
t.Fatalf("should error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalTime(t *testing.T) {
|
||||
tree, err := Load("a = 07:32:00")
|
||||
assertTree(t, tree, err, map[string]interface{}{
|
||||
|
||||
@@ -30,8 +30,9 @@ const (
|
||||
tokenRightParen
|
||||
tokenDoubleLeftBracket
|
||||
tokenDoubleRightBracket
|
||||
tokenDate
|
||||
tokenLocalDate
|
||||
tokenLocalTime
|
||||
tokenTimeOffset
|
||||
tokenKeyGroup
|
||||
tokenKeyGroupArray
|
||||
tokenComma
|
||||
@@ -66,7 +67,8 @@ var tokenTypeNames = []string{
|
||||
"]]",
|
||||
"[[",
|
||||
"LocalDate",
|
||||
"LocalDate",
|
||||
"LocalTime",
|
||||
"TimeOffset",
|
||||
"KeyGroup",
|
||||
"KeyGroupArray",
|
||||
",",
|
||||
|
||||
+2
-1
@@ -25,8 +25,9 @@ func TestTokenStringer(t *testing.T) {
|
||||
{tokenRightParen, ")"},
|
||||
{tokenDoubleLeftBracket, "]]"},
|
||||
{tokenDoubleRightBracket, "[["},
|
||||
{tokenDate, "LocalDate"},
|
||||
{tokenLocalDate, "LocalDate"},
|
||||
{tokenLocalTime, "LocalTime"},
|
||||
{tokenTimeOffset, "TimeOffset"},
|
||||
{tokenKeyGroup, "KeyGroup"},
|
||||
{tokenKeyGroupArray, "KeyGroupArray"},
|
||||
{tokenComma, ","},
|
||||
|
||||
@@ -15,6 +15,7 @@ type tomlValue struct {
|
||||
comment string
|
||||
commented bool
|
||||
multiline bool
|
||||
literal bool
|
||||
position Position
|
||||
}
|
||||
|
||||
@@ -314,6 +315,7 @@ type SetOptions struct {
|
||||
Comment string
|
||||
Commented bool
|
||||
Multiline bool
|
||||
Literal bool
|
||||
}
|
||||
|
||||
// SetWithOptions is the same as Set, but allows you to provide formatting
|
||||
@@ -362,12 +364,14 @@ func (t *Tree) SetPathWithOptions(keys []string, opts SetOptions, value interfac
|
||||
v.comment = opts.Comment
|
||||
v.commented = opts.Commented
|
||||
v.multiline = opts.Multiline
|
||||
v.literal = opts.Literal
|
||||
toInsert = v
|
||||
default:
|
||||
toInsert = &tomlValue{value: value,
|
||||
comment: opts.Comment,
|
||||
commented: opts.Commented,
|
||||
multiline: opts.Multiline,
|
||||
literal: opts.Literal,
|
||||
position: Position{Line: subtree.position.Line + len(subtree.values) + 1, Col: subtree.position.Col}}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,6 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
func testgenInvalid(t *testing.T, input string) {
|
||||
@@ -56,7 +54,7 @@ func testgenValid(t *testing.T, input string, jsonRef string) {
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(jsonExpected, jsonTest) {
|
||||
t.Logf("Diff:\n%s", spew.Sdump(jsonExpected, jsonTest))
|
||||
t.Logf("Diff:\n%#+v\n%#+v", jsonExpected, jsonTest)
|
||||
t.Fatal("parsed TOML tree is different than expected structure")
|
||||
}
|
||||
}
|
||||
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package toml
|
||||
|
||||
// PubTOMLValue wrapping tomlValue in order to access all properties from outside.
|
||||
type PubTOMLValue = tomlValue
|
||||
|
||||
func (ptv *PubTOMLValue) Value() interface{} {
|
||||
return ptv.value
|
||||
}
|
||||
func (ptv *PubTOMLValue) Comment() string {
|
||||
return ptv.comment
|
||||
}
|
||||
func (ptv *PubTOMLValue) Commented() bool {
|
||||
return ptv.commented
|
||||
}
|
||||
func (ptv *PubTOMLValue) Multiline() bool {
|
||||
return ptv.multiline
|
||||
}
|
||||
func (ptv *PubTOMLValue) Position() Position {
|
||||
return ptv.position
|
||||
}
|
||||
|
||||
func (ptv *PubTOMLValue) SetValue(v interface{}) {
|
||||
ptv.value = v
|
||||
}
|
||||
func (ptv *PubTOMLValue) SetComment(s string) {
|
||||
ptv.comment = s
|
||||
}
|
||||
func (ptv *PubTOMLValue) SetCommented(c bool) {
|
||||
ptv.commented = c
|
||||
}
|
||||
func (ptv *PubTOMLValue) SetMultiline(m bool) {
|
||||
ptv.multiline = m
|
||||
}
|
||||
func (ptv *PubTOMLValue) SetPosition(p Position) {
|
||||
ptv.position = p
|
||||
}
|
||||
|
||||
// PubTree wrapping Tree in order to access all properties from outside.
|
||||
type PubTree = Tree
|
||||
|
||||
func (pt *PubTree) Values() map[string]interface{} {
|
||||
return pt.values
|
||||
}
|
||||
|
||||
func (pt *PubTree) Comment() string {
|
||||
return pt.comment
|
||||
}
|
||||
|
||||
func (pt *PubTree) Commented() bool {
|
||||
return pt.commented
|
||||
}
|
||||
|
||||
func (pt *PubTree) Inline() bool {
|
||||
return pt.inline
|
||||
}
|
||||
|
||||
func (pt *PubTree) SetValues(v map[string]interface{}) {
|
||||
pt.values = v
|
||||
}
|
||||
|
||||
func (pt *PubTree) SetComment(c string) {
|
||||
pt.comment = c
|
||||
}
|
||||
|
||||
func (pt *PubTree) SetCommented(c bool) {
|
||||
pt.commented = c
|
||||
}
|
||||
|
||||
func (pt *PubTree) SetInline(i bool) {
|
||||
pt.inline = i
|
||||
}
|
||||
+45
-10
@@ -103,7 +103,7 @@ func encodeTomlString(value string) string {
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func tomlTreeStringRepresentation(t *Tree, ord marshalOrder) (string, error) {
|
||||
func tomlTreeStringRepresentation(t *Tree, ord MarshalOrder) (string, error) {
|
||||
var orderedVals []sortNode
|
||||
switch ord {
|
||||
case OrderPreserve:
|
||||
@@ -126,7 +126,7 @@ func tomlTreeStringRepresentation(t *Tree, ord marshalOrder) (string, error) {
|
||||
return "{ " + strings.Join(values, ", ") + " }", nil
|
||||
}
|
||||
|
||||
func tomlValueStringRepresentation(v interface{}, commented string, indent string, ord marshalOrder, arraysOneElementPerLine bool) (string, error) {
|
||||
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.
|
||||
// That change was made to allow this function to see formatting options.
|
||||
tv, ok := v.(*tomlValue)
|
||||
@@ -158,7 +158,15 @@ func tomlValueStringRepresentation(v interface{}, commented string, indent strin
|
||||
return strings.ToLower(strconv.FormatFloat(value, 'f', -1, bits)), nil
|
||||
case string:
|
||||
if tv.multiline {
|
||||
return "\"\"\"\n" + encodeMultilineTomlString(value, commented) + "\"\"\"", nil
|
||||
if tv.literal {
|
||||
b := strings.Builder{}
|
||||
b.WriteString("'''\n")
|
||||
b.Write([]byte(value))
|
||||
b.WriteString("\n'''")
|
||||
return b.String(), nil
|
||||
} else {
|
||||
return "\"\"\"\n" + encodeMultilineTomlString(value, commented) + "\"\"\"", nil
|
||||
}
|
||||
}
|
||||
return "\"" + encodeTomlString(value) + "\"", nil
|
||||
case []byte:
|
||||
@@ -218,7 +226,9 @@ func tomlValueStringRepresentation(v interface{}, commented string, indent strin
|
||||
}
|
||||
|
||||
func getTreeArrayLine(trees []*Tree) (line int) {
|
||||
// get lowest line number that is not 0
|
||||
// Prevent returning 0 for empty trees
|
||||
line = int(^uint(0) >> 1)
|
||||
// get lowest line number >= 0
|
||||
for _, tv := range trees {
|
||||
if tv.position.Line < line || line == 0 {
|
||||
line = tv.position.Line
|
||||
@@ -307,10 +317,10 @@ func sortAlphabetical(t *Tree) (vals []sortNode) {
|
||||
}
|
||||
|
||||
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, " ", false)
|
||||
return t.writeToOrdered(w, indent, keyspace, bytesCount, arraysOneElementPerLine, OrderAlphabetical, " ", false, false)
|
||||
}
|
||||
|
||||
func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount int64, arraysOneElementPerLine bool, ord marshalOrder, indentString string, parentCommented bool) (int64, error) {
|
||||
func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount int64, arraysOneElementPerLine bool, ord MarshalOrder, indentString string, compactComments, parentCommented bool) (int64, error) {
|
||||
var orderedVals []sortNode
|
||||
|
||||
switch ord {
|
||||
@@ -360,7 +370,7 @@ func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount i
|
||||
if err != nil {
|
||||
return bytesCount, err
|
||||
}
|
||||
bytesCount, err = node.writeToOrdered(w, indent+indentString, combinedKey, bytesCount, arraysOneElementPerLine, ord, indentString, parentCommented || t.commented || tv.commented)
|
||||
bytesCount, err = node.writeToOrdered(w, indent+indentString, combinedKey, bytesCount, arraysOneElementPerLine, ord, indentString, compactComments, parentCommented || t.commented || tv.commented)
|
||||
if err != nil {
|
||||
return bytesCount, err
|
||||
}
|
||||
@@ -376,7 +386,7 @@ func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount i
|
||||
return bytesCount, err
|
||||
}
|
||||
|
||||
bytesCount, err = subTree.writeToOrdered(w, indent+indentString, combinedKey, bytesCount, arraysOneElementPerLine, ord, indentString, parentCommented || t.commented || subTree.commented)
|
||||
bytesCount, err = subTree.writeToOrdered(w, indent+indentString, combinedKey, bytesCount, arraysOneElementPerLine, ord, indentString, compactComments, parentCommented || t.commented || subTree.commented)
|
||||
if err != nil {
|
||||
return bytesCount, err
|
||||
}
|
||||
@@ -404,7 +414,14 @@ func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount i
|
||||
if strings.HasPrefix(comment, "#") {
|
||||
start = ""
|
||||
}
|
||||
writtenBytesCountComment, errc := writeStrings(w, "\n", indent, start, comment, "\n")
|
||||
if !compactComments {
|
||||
writtenBytesCountComment, errc := writeStrings(w, "\n")
|
||||
bytesCount += int64(writtenBytesCountComment)
|
||||
if errc != nil {
|
||||
return bytesCount, errc
|
||||
}
|
||||
}
|
||||
writtenBytesCountComment, errc := writeStrings(w, indent, start, comment, "\n")
|
||||
bytesCount += int64(writtenBytesCountComment)
|
||||
if errc != nil {
|
||||
return bytesCount, errc
|
||||
@@ -510,8 +527,26 @@ func (t *Tree) ToMap() map[string]interface{} {
|
||||
case *Tree:
|
||||
result[k] = node.ToMap()
|
||||
case *tomlValue:
|
||||
result[k] = node.value
|
||||
result[k] = tomlValueToGo(node.value)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func tomlValueToGo(v interface{}) interface{} {
|
||||
if tree, ok := v.(*Tree); ok {
|
||||
return tree.ToMap()
|
||||
}
|
||||
|
||||
rv := reflect.ValueOf(v)
|
||||
|
||||
if rv.Kind() != reflect.Slice {
|
||||
return v
|
||||
}
|
||||
values := make([]interface{}, rv.Len())
|
||||
for i := 0; i < rv.Len(); i++ {
|
||||
item := rv.Index(i).Interface()
|
||||
values[i] = tomlValueToGo(item)
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
@@ -295,6 +295,42 @@ func TestTreeWriteToMapWithArrayOfInlineTables(t *testing.T) {
|
||||
testMaps(t, treeMap, expected)
|
||||
}
|
||||
|
||||
func TestTreeWriteToMapWithTableInMixedArray(t *testing.T) {
|
||||
tree, _ := Load(`a = [
|
||||
"foo",
|
||||
[
|
||||
"bar",
|
||||
{baz = "quux"},
|
||||
],
|
||||
[
|
||||
{a = "b"},
|
||||
{c = "d"},
|
||||
],
|
||||
]`)
|
||||
expected := map[string]interface{}{
|
||||
"a": []interface{}{
|
||||
"foo",
|
||||
[]interface{}{
|
||||
"bar",
|
||||
map[string]interface{}{
|
||||
"baz": "quux",
|
||||
},
|
||||
},
|
||||
[]interface{}{
|
||||
map[string]interface{}{
|
||||
"a": "b",
|
||||
},
|
||||
map[string]interface{}{
|
||||
"c": "d",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
treeMap := tree.ToMap()
|
||||
|
||||
testMaps(t, treeMap, expected)
|
||||
}
|
||||
|
||||
func TestTreeWriteToFloat(t *testing.T) {
|
||||
tree, err := Load(`a = 3.0`)
|
||||
if err != nil {
|
||||
@@ -328,6 +364,55 @@ c = nan`
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderedEmptyTrees(t *testing.T) {
|
||||
type val struct {
|
||||
Key string `toml:"key"`
|
||||
}
|
||||
type structure struct {
|
||||
First val `toml:"first"`
|
||||
Empty []val `toml:"empty"`
|
||||
}
|
||||
input := structure{First: val{Key: "value"}}
|
||||
buf := new(bytes.Buffer)
|
||||
err := NewEncoder(buf).Order(OrderPreserve).Encode(input)
|
||||
if err != nil {
|
||||
t.Fatal("failed to encode input")
|
||||
}
|
||||
expected := `
|
||||
[first]
|
||||
key = "value"
|
||||
`
|
||||
if expected != buf.String() {
|
||||
t.Fatal("expected and encoded body aren't equal: ", expected, buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderedNonIncreasedLine(t *testing.T) {
|
||||
type NiceMap map[string]string
|
||||
type Manifest struct {
|
||||
NiceMap `toml:"dependencies"`
|
||||
Build struct {
|
||||
BuildCommand string `toml:"build-command"`
|
||||
} `toml:"build"`
|
||||
}
|
||||
|
||||
test := &Manifest{}
|
||||
test.Build.BuildCommand = "test"
|
||||
buf := new(bytes.Buffer)
|
||||
if err := NewEncoder(buf).Order(OrderPreserve).Encode(test); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
expected := `
|
||||
[dependencies]
|
||||
|
||||
[build]
|
||||
build-command = "test"
|
||||
`
|
||||
if expected != buf.String() {
|
||||
t.Fatal("expected and encoded body aren't equal: ", expected, buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestIssue290(t *testing.T) {
|
||||
tomlString :=
|
||||
`[table]
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package toml
|
||||
|
||||
// ValueStringRepresentation transforms an interface{} value into its toml string representation.
|
||||
func ValueStringRepresentation(v interface{}, commented string, indent string, ord MarshalOrder, arraysOneElementPerLine bool) (string, error) {
|
||||
return tomlValueStringRepresentation(v, commented, indent, ord, arraysOneElementPerLine)
|
||||
}
|
||||
Reference in New Issue
Block a user