Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5dc006fb52 | |||
| fed1464066 | |||
| 1baee4630f | |||
| 352072d51a | |||
| c42c3365f3 | |||
| b8ba995eaa |
@@ -2,6 +2,19 @@
|
|||||||
|
|
||||||
Go library for the [TOML](https://toml.io/) format.
|
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
|
This library supports TOML version
|
||||||
[v1.0.0-rc.3](https://toml.io/en/v1.0.0-rc.3)
|
[v1.0.0-rc.3](https://toml.io/en/v1.0.0-rc.3)
|
||||||
|
|
||||||
@@ -25,9 +38,9 @@ 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
|
(majority of cases), those features are implemented and the API unlikely to
|
||||||
change.
|
change.
|
||||||
|
|
||||||
The remaining features (Document structure editing and tooling) will be added
|
The remaining features will be added shortly. While pull-requests are welcome on
|
||||||
shortly. While pull-requests are welcome on v1, no active development is
|
v1, no active development is expected on it. When v2.0.0 is released, v1 will be
|
||||||
expected on it. When v2.0.0 is released, v1 will be deprecated.
|
deprecated.
|
||||||
|
|
||||||
👉 [go-toml v2][v2]
|
👉 [go-toml v2][v2]
|
||||||
|
|
||||||
|
|||||||
+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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -16,6 +17,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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() {
|
flag.Usage = func() {
|
||||||
fmt.Fprintln(os.Stderr, "tomll can be used in two ways:")
|
fmt.Fprintln(os.Stderr, "tomll can be used in two ways:")
|
||||||
fmt.Fprintln(os.Stderr, "Writing to STDIN and reading from STDOUT:")
|
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, " tomll a.toml b.toml c.toml")
|
||||||
fmt.Fprintln(os.Stderr, "")
|
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, "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()
|
flag.Parse()
|
||||||
|
|
||||||
// read from stdin and print to stdout
|
// read from stdin and print to stdout
|
||||||
if flag.NArg() == 0 {
|
if flag.NArg() == 0 {
|
||||||
s, err := lintReader(os.Stdin)
|
s, err := lintReader(os.Stdin, *multiLineArray)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
io.WriteString(os.Stderr, err.Error())
|
io.WriteString(os.Stderr, err.Error())
|
||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
@@ -38,7 +45,7 @@ func main() {
|
|||||||
} else {
|
} else {
|
||||||
// otherwise modify a list of files
|
// otherwise modify a list of files
|
||||||
for _, filename := range flag.Args() {
|
for _, filename := range flag.Args() {
|
||||||
s, err := lintFile(filename)
|
s, err := lintFile(filename, *multiLineArray)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
io.WriteString(os.Stderr, err.Error())
|
io.WriteString(os.Stderr, err.Error())
|
||||||
os.Exit(-1)
|
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)
|
tree, err := toml.LoadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return tree.String(), nil
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
if err := toml.NewEncoder(buf).ArraysWithOneElementPerLine(multiLineArray).Encode(tree); err != nil {
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func lintReader(r io.Reader) (string, error) {
|
return buf.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func lintReader(r io.Reader, multiLineArray bool) (string, error) {
|
||||||
tree, err := toml.LoadReader(r)
|
tree, err := toml.LoadReader(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
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
-1
@@ -1113,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())
|
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())
|
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()) {
|
if reflect.Indirect(reflect.New(mtype)).OverflowUint(val.Convert(reflect.TypeOf(uint64(0))).Uint()) {
|
||||||
|
|||||||
@@ -4119,3 +4119,13 @@ ErrorField = "foo"
|
|||||||
t.Fatalf("error was expected")
|
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()
|
return math.NaN()
|
||||||
case tokenInteger:
|
case tokenInteger:
|
||||||
cleanedVal := cleanupNumberToken(tok.val)
|
cleanedVal := cleanupNumberToken(tok.val)
|
||||||
var err error
|
base := 10
|
||||||
var val int64
|
s := cleanedVal
|
||||||
|
checkInvalidUnderscore := numberContainsInvalidUnderscore
|
||||||
if len(cleanedVal) >= 3 && cleanedVal[0] == '0' {
|
if len(cleanedVal) >= 3 && cleanedVal[0] == '0' {
|
||||||
switch cleanedVal[1] {
|
switch cleanedVal[1] {
|
||||||
case 'x':
|
case 'x':
|
||||||
err = hexNumberContainsInvalidUnderscore(tok.val)
|
checkInvalidUnderscore = hexNumberContainsInvalidUnderscore
|
||||||
if err != nil {
|
base = 16
|
||||||
p.raiseError(tok, "%s", err)
|
|
||||||
}
|
|
||||||
val, err = strconv.ParseInt(cleanedVal[2:], 16, 64)
|
|
||||||
case 'o':
|
case 'o':
|
||||||
err = numberContainsInvalidUnderscore(tok.val)
|
base = 8
|
||||||
if err != nil {
|
|
||||||
p.raiseError(tok, "%s", err)
|
|
||||||
}
|
|
||||||
val, err = strconv.ParseInt(cleanedVal[2:], 8, 64)
|
|
||||||
case 'b':
|
case 'b':
|
||||||
err = numberContainsInvalidUnderscore(tok.val)
|
base = 2
|
||||||
if err != nil {
|
|
||||||
p.raiseError(tok, "%s", err)
|
|
||||||
}
|
|
||||||
val, err = strconv.ParseInt(cleanedVal[2:], 2, 64)
|
|
||||||
default:
|
default:
|
||||||
panic("invalid base") // the lexer should catch this first
|
panic("invalid base") // the lexer should catch this first
|
||||||
}
|
}
|
||||||
} else {
|
s = cleanedVal[2:]
|
||||||
err = numberContainsInvalidUnderscore(tok.val)
|
}
|
||||||
if err != nil {
|
|
||||||
p.raiseError(tok, "%s", err)
|
err := checkInvalidUnderscore(tok.val)
|
||||||
}
|
|
||||||
val, err = strconv.ParseInt(cleanedVal, 10, 64)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.raiseError(tok, "%s", err)
|
p.raiseError(tok, "%s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var val interface{}
|
||||||
|
val, err = strconv.ParseInt(s, base, 64)
|
||||||
|
if err == nil {
|
||||||
return val
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
if s[0] != '-' {
|
||||||
|
if val, err = strconv.ParseUint(s, base, 64); err == nil {
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p.raiseError(tok, "%s", err)
|
||||||
case tokenFloat:
|
case tokenFloat:
|
||||||
err := numberContainsInvalidUnderscore(tok.val)
|
err := numberContainsInvalidUnderscore(tok.val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1162,3 +1162,10 @@ str3 = """\
|
|||||||
t.Errorf("expected '%s', got '%s'", expected, got)
|
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),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user