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

Commit

Permalink
Map manifest and lock TOML to structs
Browse files Browse the repository at this point in the history
This is a bit of a poor man's TOML mapping while
we wait for go-toml to get reflection-based mapping
implemented.
  • Loading branch information
carolynvs committed Mar 29, 2017
1 parent fc4f862 commit 38e79aa
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 109 deletions.
60 changes: 35 additions & 25 deletions lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
package dep

import (
"bytes"
"encoding/hex"
"fmt"
"io"
"sort"

Expand All @@ -24,48 +22,62 @@ type Lock struct {
}

type rawLock struct {
Memo string
P []lockedDep
Memo string
Projects []rawLockedProject
}

type lockedDep struct {
type rawLockedProject struct {
Name string
Version string
Branch string
Revision string
Version string
Source string
Packages []string
}

func readLock(r io.Reader) (*Lock, error) {
rl := rawLock{}
err := json.NewDecoder(r).Decode(&rl)
tree, err := toml.LoadReader(r)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "Unable to parse the lock as TOML")
}

b, err := hex.DecodeString(rl.Memo)
if err != nil {
return nil, fmt.Errorf("invalid hash digest in lock's memo field")
mapper := &tomlMapper{Tree: tree}

raw := rawLock{
Memo: readKeyAsString(mapper, "memo"),
Projects: readTableAsLockedProjects(mapper, "projects"),
}

if mapper.Error != nil {
return nil, errors.Wrap(mapper.Error, "Invalid lock structure")
}
return fromRawLock(raw)
}

func fromRawLock(raw rawLock) (*Lock, error) {
var err error
l := &Lock{
Memo: b,
P: make([]gps.LockedProject, len(rl.P)),
P: make([]gps.LockedProject, len(raw.Projects)),
}

l.Memo, err = hex.DecodeString(raw.Memo)
if err != nil {
return nil, errors.Errorf("invalid hash digest in lock's memo field")
}

for i, ld := range rl.P {
for i, ld := range raw.Projects {
r := gps.Revision(ld.Revision)

var v gps.Version = r
if ld.Version != "" {
if ld.Branch != "" {
return nil, fmt.Errorf("lock file specified both a branch (%s) and version (%s) for %s", ld.Branch, ld.Version, ld.Name)
return nil, errors.Errorf("lock file specified both a branch (%s) and version (%s) for %s", ld.Branch, ld.Version, ld.Name)
}
v = gps.NewVersion(ld.Version).Is(r)
} else if ld.Branch != "" {
v = gps.NewBranch(ld.Branch).Is(r)
} else if r == "" {
return nil, fmt.Errorf("lock file has entry for %s, but specifies no branch or version", ld.Name)
return nil, errors.Errorf("lock file has entry for %s, but specifies no branch or version", ld.Name)
}

id := gps.ProjectIdentifier{
Expand All @@ -74,7 +86,6 @@ func readLock(r io.Reader) (*Lock, error) {
}
l.P[i] = gps.NewLockedProject(id, v, ld.Packages)
}

return l, nil
}

Expand All @@ -89,15 +100,15 @@ func (l *Lock) Projects() []gps.LockedProject {
// toRaw converts the manifest into a representation suitable to write to the lock file
func (l *Lock) toRaw() rawLock {
raw := rawLock{
Memo: hex.EncodeToString(l.Memo),
P: make([]lockedDep, len(l.P)),
Memo: hex.EncodeToString(l.Memo),
Projects: make([]rawLockedProject, len(l.P)),
}

sort.Sort(SortedLockedProjects(l.P))

for k, lp := range l.P {
id := lp.Ident()
ld := lockedDep{
ld := rawLockedProject{
Name: string(id.ProjectRoot),
Source: id.Source,
Packages: lp.Packages(),
Expand All @@ -106,7 +117,7 @@ func (l *Lock) toRaw() rawLock {
v := lp.Version()
ld.Revision, ld.Branch, ld.Version = getVersionInfo(v)

raw.P[k] = ld
raw.Projects[k] = ld
}

// TODO sort output - #15
Expand All @@ -117,12 +128,11 @@ func (l *Lock) toRaw() rawLock {
func (l *Lock) MarshalTOML() (string, error) {
raw := l.toRaw()

// TODO(carolynvs) Consider adding reflection-based marshal functionality to go-toml
m := make(map[string]interface{})
m["memo"] = raw.Memo
p := make([]map[string]interface{}, len(raw.P))
p := make([]map[string]interface{}, len(raw.Projects))
for i := 0; i < len(p); i++ {
srcPrj := raw.P[i]
srcPrj := raw.Projects[i]
prj := make(map[string]interface{})
prj["name"] = srcPrj.Name
prj["revision"] = srcPrj.Revision
Expand Down
Loading

0 comments on commit 38e79aa

Please sign in to comment.