Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c893dbf25c | |||
| 2a1df71375 | |||
| a2f5197638 | |||
| bb65137dc4 | |||
| 99782c87cf | |||
| ce6fbd7bc0 | |||
| b59c12a70d | |||
| 6a307ac0d0 | |||
| a2e5256180 | |||
| 5163266f16 | |||
| b4f0a950bf | |||
| ef48fb2be1 |
@@ -1,9 +1,18 @@
|
|||||||
---
|
---
|
||||||
name: Bug report
|
name: Bug report
|
||||||
about: Create a report to help us improve
|
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**
|
**Describe the bug**
|
||||||
A clear and concise description of what the bug is.
|
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".
|
A clear and concise description of what you expected to happen, if other than "should work".
|
||||||
|
|
||||||
**Versions**
|
**Versions**
|
||||||
- go-toml: version (git sha)
|
- go-toml: version (or git sha)
|
||||||
- go: version
|
- go: version
|
||||||
- operating system: e.g. macOS, Windows, Linux
|
- operating system: e.g. macOS, Windows, Linux
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -109,7 +109,7 @@ Go-toml provides three handy command line tools:
|
|||||||
|
|
||||||
### Docker image
|
### 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
|
[dockerhub](https://hub.docker.com/r/pelletier/go-toml). For example, to
|
||||||
use `tomljson`:
|
use `tomljson`:
|
||||||
|
|
||||||
|
|||||||
+16
-17
@@ -12,9 +12,9 @@ stages:
|
|||||||
vmImage: ubuntu-latest
|
vmImage: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- task: GoTool@0
|
- task: GoTool@0
|
||||||
displayName: "Install Go 1.15"
|
displayName: "Install Go 1.16"
|
||||||
inputs:
|
inputs:
|
||||||
version: "1.15"
|
version: "1.16"
|
||||||
- task: Go@0
|
- task: Go@0
|
||||||
displayName: "go fmt ./..."
|
displayName: "go fmt ./..."
|
||||||
inputs:
|
inputs:
|
||||||
@@ -23,14 +23,13 @@ stages:
|
|||||||
arguments: './...'
|
arguments: './...'
|
||||||
- job: coverage
|
- job: coverage
|
||||||
displayName: "coverage"
|
displayName: "coverage"
|
||||||
condition: ne(variables['Build.SourceBranchName'], 'master')
|
|
||||||
pool:
|
pool:
|
||||||
vmImage: ubuntu-latest
|
vmImage: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- task: GoTool@0
|
- task: GoTool@0
|
||||||
displayName: "Install Go 1.15"
|
displayName: "Install Go 1.16"
|
||||||
inputs:
|
inputs:
|
||||||
version: "1.15"
|
version: "1.16"
|
||||||
- task: Go@0
|
- task: Go@0
|
||||||
displayName: "Generate coverage"
|
displayName: "Generate coverage"
|
||||||
inputs:
|
inputs:
|
||||||
@@ -48,9 +47,9 @@ stages:
|
|||||||
vmImage: ubuntu-latest
|
vmImage: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- task: GoTool@0
|
- task: GoTool@0
|
||||||
displayName: "Install Go 1.15"
|
displayName: "Install Go 1.16"
|
||||||
inputs:
|
inputs:
|
||||||
version: "1.15"
|
version: "1.16"
|
||||||
- script: echo "##vso[task.setvariable variable=PATH]${PATH}:/home/vsts/go/bin/"
|
- script: echo "##vso[task.setvariable variable=PATH]${PATH}:/home/vsts/go/bin/"
|
||||||
- task: Bash@3
|
- task: Bash@3
|
||||||
inputs:
|
inputs:
|
||||||
@@ -61,6 +60,15 @@ stages:
|
|||||||
displayName: "unit tests"
|
displayName: "unit tests"
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
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:
|
linux 1.15:
|
||||||
goVersion: '1.15'
|
goVersion: '1.15'
|
||||||
imageName: 'ubuntu-latest'
|
imageName: 'ubuntu-latest'
|
||||||
@@ -70,15 +78,6 @@ stages:
|
|||||||
windows 1.15:
|
windows 1.15:
|
||||||
goVersion: '1.15'
|
goVersion: '1.15'
|
||||||
imageName: 'windows-latest'
|
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:
|
pool:
|
||||||
vmImage: $(imageName)
|
vmImage: $(imageName)
|
||||||
steps:
|
steps:
|
||||||
@@ -114,7 +113,7 @@ stages:
|
|||||||
- task: GoTool@0
|
- task: GoTool@0
|
||||||
displayName: "Install Go"
|
displayName: "Install Go"
|
||||||
inputs:
|
inputs:
|
||||||
version: 1.15
|
version: 1.16
|
||||||
- task: Bash@3
|
- task: Bash@3
|
||||||
inputs:
|
inputs:
|
||||||
targetType: inline
|
targetType: inline
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
module github.com/pelletier/go-toml
|
module github.com/pelletier/go-toml
|
||||||
|
|
||||||
go 1.12
|
go 1.12
|
||||||
|
|
||||||
require github.com/davecgh/go-spew v1.1.1
|
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
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=
|
|
||||||
+12
-4
@@ -18,6 +18,7 @@ const (
|
|||||||
tagFieldComment = "comment"
|
tagFieldComment = "comment"
|
||||||
tagCommented = "commented"
|
tagCommented = "commented"
|
||||||
tagMultiline = "multiline"
|
tagMultiline = "multiline"
|
||||||
|
tagLiteral = "literal"
|
||||||
tagDefault = "default"
|
tagDefault = "default"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,6 +28,7 @@ type tomlOpts struct {
|
|||||||
comment string
|
comment string
|
||||||
commented bool
|
commented bool
|
||||||
multiline bool
|
multiline bool
|
||||||
|
literal bool
|
||||||
include bool
|
include bool
|
||||||
omitempty bool
|
omitempty bool
|
||||||
defaultValue string
|
defaultValue string
|
||||||
@@ -46,6 +48,7 @@ type annotation struct {
|
|||||||
comment string
|
comment string
|
||||||
commented string
|
commented string
|
||||||
multiline string
|
multiline string
|
||||||
|
literal string
|
||||||
defaultValue string
|
defaultValue string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,15 +57,16 @@ var annotationDefault = annotation{
|
|||||||
comment: tagFieldComment,
|
comment: tagFieldComment,
|
||||||
commented: tagCommented,
|
commented: tagCommented,
|
||||||
multiline: tagMultiline,
|
multiline: tagMultiline,
|
||||||
|
literal: tagLiteral,
|
||||||
defaultValue: tagDefault,
|
defaultValue: tagDefault,
|
||||||
}
|
}
|
||||||
|
|
||||||
type marshalOrder int
|
type MarshalOrder int
|
||||||
|
|
||||||
// Orders the Encoder can write the fields to the output stream.
|
// Orders the Encoder can write the fields to the output stream.
|
||||||
const (
|
const (
|
||||||
// Sort fields alphabetically.
|
// 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
|
// Preserve the order the fields are encountered. For example, the order of fields in
|
||||||
// a struct.
|
// a struct.
|
||||||
OrderPreserve
|
OrderPreserve
|
||||||
@@ -256,7 +260,7 @@ type Encoder struct {
|
|||||||
annotation
|
annotation
|
||||||
line int
|
line int
|
||||||
col int
|
col int
|
||||||
order marshalOrder
|
order MarshalOrder
|
||||||
promoteAnon bool
|
promoteAnon bool
|
||||||
indentation string
|
indentation string
|
||||||
}
|
}
|
||||||
@@ -317,7 +321,7 @@ func (e *Encoder) ArraysWithOneElementPerLine(v bool) *Encoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Order allows to change in which order fields will be written to the output stream.
|
// 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
|
e.order = ord
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
@@ -442,6 +446,7 @@ func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, er
|
|||||||
Comment: opts.comment,
|
Comment: opts.comment,
|
||||||
Commented: opts.commented,
|
Commented: opts.commented,
|
||||||
Multiline: opts.multiline,
|
Multiline: opts.multiline,
|
||||||
|
Literal: opts.literal,
|
||||||
}, val)
|
}, val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -586,6 +591,7 @@ func (e *Encoder) wrapTomlValue(val interface{}, parent *Tree) interface{} {
|
|||||||
_, isTree := val.(*Tree)
|
_, isTree := val.(*Tree)
|
||||||
_, isTreeS := val.([]*Tree)
|
_, isTreeS := val.([]*Tree)
|
||||||
if isTree || isTreeS {
|
if isTree || isTreeS {
|
||||||
|
e.line++
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1168,6 +1174,7 @@ func tomlOptions(vf reflect.StructField, an annotation) tomlOpts {
|
|||||||
}
|
}
|
||||||
commented, _ := strconv.ParseBool(vf.Tag.Get(an.commented))
|
commented, _ := strconv.ParseBool(vf.Tag.Get(an.commented))
|
||||||
multiline, _ := strconv.ParseBool(vf.Tag.Get(an.multiline))
|
multiline, _ := strconv.ParseBool(vf.Tag.Get(an.multiline))
|
||||||
|
literal, _ := strconv.ParseBool(vf.Tag.Get(an.literal))
|
||||||
defaultValue := vf.Tag.Get(tagDefault)
|
defaultValue := vf.Tag.Get(tagDefault)
|
||||||
result := tomlOpts{
|
result := tomlOpts{
|
||||||
name: vf.Name,
|
name: vf.Name,
|
||||||
@@ -1175,6 +1182,7 @@ func tomlOptions(vf reflect.StructField, an annotation) tomlOpts {
|
|||||||
comment: comment,
|
comment: comment,
|
||||||
commented: commented,
|
commented: commented,
|
||||||
multiline: multiline,
|
multiline: multiline,
|
||||||
|
literal: literal,
|
||||||
include: true,
|
include: true,
|
||||||
omitempty: false,
|
omitempty: false,
|
||||||
defaultValue: defaultValue,
|
defaultValue: defaultValue,
|
||||||
|
|||||||
@@ -1294,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) {
|
func TestMarshalNonPrimitiveTypeCommented(t *testing.T) {
|
||||||
expectedToml := []byte(`
|
expectedToml := []byte(`
|
||||||
# [CommentedMapField]
|
# [CommentedMapField]
|
||||||
|
|||||||
+1
-3
@@ -6,8 +6,6 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func assertSubTree(t *testing.T, path []string, tree *Tree, err error, ref map[string]interface{}) {
|
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{}) {
|
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)
|
assertSubTree(t, []string{}, tree, err, ref)
|
||||||
t.Log("Finished tree assertion.")
|
t.Log("Finished tree assertion.")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ type tomlValue struct {
|
|||||||
comment string
|
comment string
|
||||||
commented bool
|
commented bool
|
||||||
multiline bool
|
multiline bool
|
||||||
|
literal bool
|
||||||
position Position
|
position Position
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,6 +315,7 @@ type SetOptions struct {
|
|||||||
Comment string
|
Comment string
|
||||||
Commented bool
|
Commented bool
|
||||||
Multiline bool
|
Multiline bool
|
||||||
|
Literal bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetWithOptions is the same as Set, but allows you to provide formatting
|
// 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.comment = opts.Comment
|
||||||
v.commented = opts.Commented
|
v.commented = opts.Commented
|
||||||
v.multiline = opts.Multiline
|
v.multiline = opts.Multiline
|
||||||
|
v.literal = opts.Literal
|
||||||
toInsert = v
|
toInsert = v
|
||||||
default:
|
default:
|
||||||
toInsert = &tomlValue{value: value,
|
toInsert = &tomlValue{value: value,
|
||||||
comment: opts.Comment,
|
comment: opts.Comment,
|
||||||
commented: opts.Commented,
|
commented: opts.Commented,
|
||||||
multiline: opts.Multiline,
|
multiline: opts.Multiline,
|
||||||
|
literal: opts.Literal,
|
||||||
position: Position{Line: subtree.position.Line + len(subtree.values) + 1, Col: subtree.position.Col}}
|
position: Position{Line: subtree.position.Line + len(subtree.values) + 1, Col: subtree.position.Col}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func testgenInvalid(t *testing.T, input string) {
|
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) {
|
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")
|
t.Fatal("parsed TOML tree is different than expected structure")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-5
@@ -103,7 +103,7 @@ func encodeTomlString(value string) string {
|
|||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func tomlTreeStringRepresentation(t *Tree, ord marshalOrder) (string, error) {
|
func tomlTreeStringRepresentation(t *Tree, ord MarshalOrder) (string, error) {
|
||||||
var orderedVals []sortNode
|
var orderedVals []sortNode
|
||||||
switch ord {
|
switch ord {
|
||||||
case OrderPreserve:
|
case OrderPreserve:
|
||||||
@@ -126,7 +126,7 @@ func tomlTreeStringRepresentation(t *Tree, ord marshalOrder) (string, error) {
|
|||||||
return "{ " + strings.Join(values, ", ") + " }", nil
|
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.
|
// this interface check is added to dereference the change made in the writeTo function.
|
||||||
// That change was made to allow this function to see formatting options.
|
// That change was made to allow this function to see formatting options.
|
||||||
tv, ok := v.(*tomlValue)
|
tv, ok := v.(*tomlValue)
|
||||||
@@ -158,7 +158,15 @@ func tomlValueStringRepresentation(v interface{}, commented string, indent strin
|
|||||||
return strings.ToLower(strconv.FormatFloat(value, 'f', -1, bits)), nil
|
return strings.ToLower(strconv.FormatFloat(value, 'f', -1, bits)), nil
|
||||||
case string:
|
case string:
|
||||||
if tv.multiline {
|
if tv.multiline {
|
||||||
return "\"\"\"\n" + encodeMultilineTomlString(value, 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
|
return "\"" + encodeTomlString(value) + "\"", nil
|
||||||
case []byte:
|
case []byte:
|
||||||
@@ -218,7 +226,9 @@ func tomlValueStringRepresentation(v interface{}, commented string, indent strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getTreeArrayLine(trees []*Tree) (line int) {
|
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 {
|
for _, tv := range trees {
|
||||||
if tv.position.Line < line || line == 0 {
|
if tv.position.Line < line || line == 0 {
|
||||||
line = tv.position.Line
|
line = tv.position.Line
|
||||||
@@ -310,7 +320,7 @@ func (t *Tree) writeTo(w io.Writer, indent, keyspace string, bytesCount int64, a
|
|||||||
return t.writeToOrdered(w, indent, keyspace, bytesCount, arraysOneElementPerLine, OrderAlphabetical, " ", false)
|
return t.writeToOrdered(w, indent, keyspace, bytesCount, arraysOneElementPerLine, OrderAlphabetical, " ", false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount int64, arraysOneElementPerLine bool, ord marshalOrder, indentString string, parentCommented bool) (int64, error) {
|
func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount int64, arraysOneElementPerLine bool, ord MarshalOrder, indentString string, parentCommented bool) (int64, error) {
|
||||||
var orderedVals []sortNode
|
var orderedVals []sortNode
|
||||||
|
|
||||||
switch ord {
|
switch ord {
|
||||||
|
|||||||
@@ -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) {
|
func TestIssue290(t *testing.T) {
|
||||||
tomlString :=
|
tomlString :=
|
||||||
`[table]
|
`[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