Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit afddae1

Browse files
committed
Update go-toml for nested custom Marshaler support
1 parent b30a25c commit afddae1

File tree

6 files changed

+93
-21
lines changed

6 files changed

+93
-21
lines changed

fs.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,9 @@ import (
1313
"syscall"
1414

1515
"github.com/pkg/errors"
16+
"github.com/pelletier/go-toml"
1617
)
1718

18-
// tomlMarshaler is the interface implemented by types that
19-
// can marshal themselves into valid TOML.
20-
// TODO(carolynvs) Add this (and an unmarshaler) to go-toml, implemented using the same patterns in encoding/json
21-
type tomlMarshaler interface {
22-
MarshalTOML() ([]byte, error)
23-
}
24-
2519
func IsRegular(name string) (bool, error) {
2620
// TODO: lstat?
2721
fi, err := os.Stat(name)
@@ -65,7 +59,7 @@ func IsNonEmptyDir(name string) (bool, error) {
6559
return len(files) != 0, nil
6660
}
6761

68-
func writeFile(path string, in tomlMarshaler) error {
62+
func writeFile(path string, in toml.Marshaler) error {
6963
f, err := os.Create(path)
7064
if err != nil {
7165
return err

lock.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ memo = "31a7162c06758e4619ed89b91e1f48bf94ad14f394bbee79299ed0bb5150e409"
2727
[[projects]]
2828
branch = "master"
2929
name = "github.com/pelletier/go-toml"
30-
revision = "e32a2e04744250647a72bf17da1b09befc03b6b1"
30+
revision = "fe206efb84b2bc8e8cfafe6b4c1826622be969e3"
3131
packages = ["."]
3232

3333
[[projects]]

txn_writer.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ type LockDiff struct {
5959
Modify []LockedProjectDiff
6060
}
6161

62+
type rawLockedProjectDiffs struct {
63+
Projects []LockedProjectDiff `toml:"projects"`
64+
}
65+
6266
func (diff *LockDiff) Format() (string, error) {
6367
if diff == nil {
6468
return "", nil
@@ -71,13 +75,12 @@ func (diff *LockDiff) Format() (string, error) {
7175
}
7276

7377
writeDiffs := func(diffs []LockedProjectDiff) error {
74-
for i := 0; i < len(diffs); i++ {
75-
chunk, err := toml.Marshal(diffs[i])
76-
if err != nil {
77-
return err
78-
}
79-
buf.Write(chunk)
78+
raw := rawLockedProjectDiffs{diffs}
79+
chunk, err := toml.Marshal(raw)
80+
if err != nil {
81+
return err
8082
}
83+
buf.Write(chunk)
8184
buf.WriteString("\n")
8285
return nil
8386
}
@@ -114,11 +117,11 @@ func (diff *LockDiff) Format() (string, error) {
114117
// TODO(carolynvs) this should be moved to gps
115118
type LockedProjectDiff struct {
116119
Name gps.ProjectRoot `toml:"name"`
117-
Source *StringDiff `toml:"source"`
118-
Version *StringDiff `toml:"version"`
119-
Branch *StringDiff `toml:"branch"`
120-
Revision *StringDiff `toml:"revision"`
121-
Packages []StringDiff `toml:"packages"`
120+
Source *StringDiff `toml:"source,omitempty"`
121+
Version *StringDiff `toml:"version,omitempty"`
122+
Branch *StringDiff `toml:"branch,omitempty"`
123+
Revision *StringDiff `toml:"revision,omitempty"`
124+
Packages []StringDiff `toml:"packages,omitempty"`
122125
}
123126

124127
type StringDiff struct {
@@ -142,6 +145,10 @@ func (diff StringDiff) String() string {
142145
return diff.Current
143146
}
144147

148+
func (diff StringDiff) MarshalTOML() ([]byte, error) {
149+
return []byte(diff.String()), nil
150+
}
151+
145152
// VendorBehavior defines when the vendor directory should be written.
146153
type VendorBehavior int
147154

vendor/github.com/pelletier/go-toml/marshal.go

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/pelletier/go-toml/marshal_test.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/pelletier/go-toml/tomltree_write.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)