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

Commit 878ae28

Browse files
committed
Add metadata to manifest & warn on unknown fields
- Use manifest buffer to create a map of TomlTree and find all the unknown fields in the tree. Raise warnings for unknown fields. - For "metadata" field, ensure it's of map type, else raise warning.
1 parent 553fbf0 commit 878ae28

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

manifest.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@ package dep
77
import (
88
"bytes"
99
"io"
10+
"reflect"
1011
"sort"
1112

1213
"github.com/golang/dep/gps"
14+
"github.com/golang/dep/internal"
1315
"github.com/pelletier/go-toml"
1416
"github.com/pkg/errors"
1517
)
1618

1719
const ManifestName = "Gopkg.toml"
1820

21+
var ManifestFields = []string{
22+
"dependencies", "overrides", "ignored", "required", "metadata",
23+
}
24+
1925
type Manifest struct {
2026
Dependencies gps.ProjectConstraints
2127
Ovr gps.ProjectConstraints
@@ -38,12 +44,59 @@ type rawProject struct {
3844
Source string `toml:"source,omitempty"`
3945
}
4046

47+
func validateManifest(b *bytes.Buffer) error {
48+
// Load the TomlTree from reader
49+
tree, err := toml.Load(b.String())
50+
if err != nil {
51+
return errors.Wrap(err, "Unable to load TomlTree from string")
52+
}
53+
// Convert tree to a map
54+
manifest := tree.ToMap()
55+
56+
// Look for unknown fields and raise warnings
57+
for prop, val := range manifest {
58+
isValidField := false
59+
for _, v := range ManifestFields {
60+
if prop == v {
61+
isValidField = true
62+
63+
// For metadata, check if it is a map type
64+
if prop == "metadata" {
65+
isValidMetadata := false
66+
if metadata, ok := val.([]interface{}); ok {
67+
for _, v := range metadata {
68+
if reflect.TypeOf(v).Kind() == reflect.Map {
69+
isValidMetadata = true
70+
} else {
71+
isValidMetadata = false
72+
break
73+
}
74+
}
75+
}
76+
if !isValidMetadata {
77+
internal.Logf("WARNING: metadata should consist of key-value pairs")
78+
}
79+
}
80+
}
81+
}
82+
if !isValidField {
83+
internal.Logf("WARNING: Unknown field in manifest: %v", prop)
84+
}
85+
}
86+
87+
return nil
88+
}
89+
4190
func readManifest(r io.Reader) (*Manifest, error) {
4291
buf := &bytes.Buffer{}
4392
_, err := buf.ReadFrom(r)
4493
if err != nil {
4594
return nil, errors.Wrap(err, "Unable to read byte stream")
4695
}
96+
err = validateManifest(buf)
97+
if err != nil {
98+
return nil, errors.Wrap(err, "Manifest validation failed")
99+
}
47100

48101
raw := rawManifest{}
49102
err = toml.Unmarshal(buf.Bytes(), &raw)

0 commit comments

Comments
 (0)