Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5dc006fb52 | |||
| fed1464066 | |||
| 1baee4630f | |||
| 352072d51a | |||
| c42c3365f3 | |||
| b8ba995eaa | |||
| 8e44986c28 | |||
| 837c1d09ee | |||
| 8410c965c2 | |||
| d083470585 | |||
| c893dbf25c | |||
| 2a1df71375 | |||
| a2f5197638 | |||
| bb65137dc4 | |||
| 99782c87cf |
@@ -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.
|
||||
@@ -0,0 +1,8 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: gomod
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: daily
|
||||
time: "13:00"
|
||||
open-pull-requests-limit: 10
|
||||
@@ -1,3 +1,13 @@
|
||||
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 - 2021 Thomas Pelletier, Eric Anderton
|
||||
@@ -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.
|
||||
|
||||
@@ -2,6 +2,19 @@
|
||||
|
||||
Go library for the [TOML](https://toml.io/) format.
|
||||
|
||||
|
||||
⚠️ This readme is for go-toml v1. As for 2022-04-27,
|
||||
[go-toml v2](https://github.com/pelletier/go-toml/tree/v2) has been released.
|
||||
|
||||
The new version contains tons of bug fixes, is much faster, and more
|
||||
importantly maintained. You are strongly encouraged to use it instead of v1!
|
||||
|
||||
[👉 go-toml v2](https://github.com/pelletier/go-toml/tree/v2)
|
||||
|
||||
v1 will not receive any updates.
|
||||
|
||||
---
|
||||
|
||||
This library supports TOML version
|
||||
[v1.0.0-rc.3](https://toml.io/en/v1.0.0-rc.3)
|
||||
|
||||
@@ -12,6 +25,31 @@ This library supports TOML version
|
||||
[](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 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:
|
||||
@@ -109,7 +147,7 @@ Go-toml provides three 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 +186,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).
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
# Security Policy
|
||||
|
||||
## Supported Versions
|
||||
|
||||
Use this section to tell people about which versions of your project are
|
||||
currently being supported with security updates.
|
||||
|
||||
| Version | Supported |
|
||||
| ---------- | ------------------ |
|
||||
| Latest 2.x | :white_check_mark: |
|
||||
| All 1.x | :x: |
|
||||
| All 0.x | :x: |
|
||||
|
||||
## Reporting a Vulnerability
|
||||
|
||||
Email a vulnerability report to `security@pelletier.codes`. Make sure to include
|
||||
as many details as possible to reproduce the vulnerability. This is a
|
||||
side-project: I will try to get back to you as quickly as possible, time
|
||||
permitting in my personal life. Providing a working patch helps very much!
|
||||
+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
|
||||
}
|
||||
|
||||
+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");
|
||||
|
||||
+15
-7
@@ -258,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.
|
||||
@@ -369,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 {
|
||||
@@ -408,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
|
||||
}
|
||||
@@ -591,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
|
||||
}
|
||||
|
||||
@@ -1105,7 +1113,7 @@ func (d *Decoder) valueFromToml(mtype reflect.Type, tval interface{}, mval1 *ref
|
||||
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.Type().Kind() != reflect.Uint64 && 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())
|
||||
}
|
||||
if reflect.Indirect(reflect.New(mtype)).OverflowUint(val.Convert(reflect.TypeOf(uint64(0))).Uint()) {
|
||||
|
||||
@@ -1476,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
|
||||
@@ -4078,3 +4119,13 @@ ErrorField = "foo"
|
||||
t.Fatalf("error was expected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGithubIssue732(t *testing.T) {
|
||||
var v interface{}
|
||||
data := []byte("a=\nb=0")
|
||||
|
||||
err := Unmarshal(data, &v)
|
||||
if err == nil {
|
||||
t.Fatalf("error was expected")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -293,42 +293,41 @@ func (p *tomlParser) parseRvalue() interface{} {
|
||||
return math.NaN()
|
||||
case tokenInteger:
|
||||
cleanedVal := cleanupNumberToken(tok.val)
|
||||
var err error
|
||||
var val int64
|
||||
base := 10
|
||||
s := cleanedVal
|
||||
checkInvalidUnderscore := numberContainsInvalidUnderscore
|
||||
if len(cleanedVal) >= 3 && cleanedVal[0] == '0' {
|
||||
switch cleanedVal[1] {
|
||||
case 'x':
|
||||
err = hexNumberContainsInvalidUnderscore(tok.val)
|
||||
if err != nil {
|
||||
p.raiseError(tok, "%s", err)
|
||||
}
|
||||
val, err = strconv.ParseInt(cleanedVal[2:], 16, 64)
|
||||
checkInvalidUnderscore = hexNumberContainsInvalidUnderscore
|
||||
base = 16
|
||||
case 'o':
|
||||
err = numberContainsInvalidUnderscore(tok.val)
|
||||
if err != nil {
|
||||
p.raiseError(tok, "%s", err)
|
||||
}
|
||||
val, err = strconv.ParseInt(cleanedVal[2:], 8, 64)
|
||||
base = 8
|
||||
case 'b':
|
||||
err = numberContainsInvalidUnderscore(tok.val)
|
||||
if err != nil {
|
||||
p.raiseError(tok, "%s", err)
|
||||
}
|
||||
val, err = strconv.ParseInt(cleanedVal[2:], 2, 64)
|
||||
base = 2
|
||||
default:
|
||||
panic("invalid base") // the lexer should catch this first
|
||||
}
|
||||
} else {
|
||||
err = numberContainsInvalidUnderscore(tok.val)
|
||||
if err != nil {
|
||||
p.raiseError(tok, "%s", err)
|
||||
}
|
||||
val, err = strconv.ParseInt(cleanedVal, 10, 64)
|
||||
s = cleanedVal[2:]
|
||||
}
|
||||
|
||||
err := checkInvalidUnderscore(tok.val)
|
||||
if err != nil {
|
||||
p.raiseError(tok, "%s", err)
|
||||
}
|
||||
return val
|
||||
|
||||
var val interface{}
|
||||
val, err = strconv.ParseInt(s, base, 64)
|
||||
if err == nil {
|
||||
return val
|
||||
}
|
||||
|
||||
if s[0] != '-' {
|
||||
if val, err = strconv.ParseUint(s, base, 64); err == nil {
|
||||
return val
|
||||
}
|
||||
}
|
||||
p.raiseError(tok, "%s", err)
|
||||
case tokenFloat:
|
||||
err := numberContainsInvalidUnderscore(tok.val)
|
||||
if err != nil {
|
||||
|
||||
@@ -1162,3 +1162,10 @@ str3 = """\
|
||||
t.Errorf("expected '%s', got '%s'", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint(t *testing.T) {
|
||||
tree, err := Load("hello = 18446744073709551615")
|
||||
assertTree(t, tree, err, map[string]interface{}{
|
||||
"hello": uint64(math.MaxUint64),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -471,7 +471,7 @@ func LoadBytes(b []byte) (tree *Tree, err error) {
|
||||
if _, ok := r.(runtime.Error); ok {
|
||||
panic(r)
|
||||
}
|
||||
err = errors.New(r.(string))
|
||||
err = fmt.Errorf("%s", r)
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
+15
-6
@@ -226,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
|
||||
@@ -315,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 {
|
||||
@@ -368,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
|
||||
}
|
||||
@@ -384,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
|
||||
}
|
||||
@@ -412,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
|
||||
|
||||
@@ -364,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]
|
||||
|
||||
Reference in New Issue
Block a user