Skip to content

Make Resolve(nil) return map of top-level objects, fixes #20 #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,11 @@ type PersonInfo struct {
}

func (pi *PersonInfo) MarshalJSON() ([]byte, error) {
date, err := pi.date()
obj, _, err := pi.resolve(nil)
if err != nil {
return nil, err
}
return json.Marshal(map[string]interface{}{
"name": pi.Name,
"email": pi.Email,
"date": *date,
})
return json.Marshal(obj)
}

func (pi *PersonInfo) String() string {
Expand All @@ -76,6 +72,18 @@ func (pi *PersonInfo) tree(name string, depth int) []string {
}

func (pi *PersonInfo) resolve(p []string) (interface{}, []string, error) {
if p == nil {
date, err := pi.date()
if err != nil {
return nil, nil, err
}
return map[string]interface{}{
"name": pi.Name,
"email": pi.Email,
"date": date.Format(time.RFC3339),
}, nil, nil
}

switch p[0] {
case "name":
return pi.Name, p[1:], nil
Expand Down Expand Up @@ -181,6 +189,23 @@ func (c *Commit) RawData() []byte {
}

func (c *Commit) Resolve(path []string) (interface{}, []string, error) {
if path == nil {
obj := map[string]interface{}{
"parents": c.Parents,
"author": c.Author,
"committer": c.Committer,
"message": c.Message,
"tree": &node.Link{Cid: c.GitTree},
"mergetag": c.MergeTag,
}

if c.Sig != nil {
obj["signature"] = c.Sig.Text
}

return obj, nil, nil
}

if len(path) == 0 {
return nil, nil, fmt.Errorf("zero length path")
}
Expand Down Expand Up @@ -212,6 +237,9 @@ func (c *Commit) Resolve(path []string) (interface{}, []string, error) {
}
return c.Committer.resolve(path[1:])
case "signature":
if c.Sig == nil {
return nil, nil, errors.New("no signature")
}
return c.Sig.Text, path[1:], nil
case "message":
return c.Message, path[1:], nil
Expand Down Expand Up @@ -284,6 +312,15 @@ func (c *Commit) GitSha() []byte {
}

func (t *MergeTag) resolve(path []string) (interface{}, []string, error) {
if path == nil {
return map[string]interface{}{
"object": &node.Link{Cid: t.Object},
"tag": t.Tag,
"tagger": t.Tagger,
"text": t.Text,
"type": t.Type,
}, nil, nil
}
if len(path) == 0 {
return nil, nil, fmt.Errorf("zero length path")
}
Expand Down
21 changes: 21 additions & 0 deletions git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ func testNode(t *testing.T, nd node.Node) error {
if !ok {
t.Fatal("Invalid mergetag")
}
obj, _, err := tag.resolve(nil)
assert(t, err == nil)
assert(t, obj != nil)

ttype, rest, err := commit.Resolve([]string{"mergetag", "0", "type"})
assert(t, err == nil)
Expand Down Expand Up @@ -224,6 +227,10 @@ func testNode(t *testing.T, nd node.Node) error {
assert(t, text.(string) == tag.Text)
}

obj, _, err := commit.Resolve(nil)
assert(t, err == nil)
assert(t, obj != nil)

case "[git tag object]":
tag, ok := nd.(*Tag)
if !ok {
Expand All @@ -235,12 +242,16 @@ func testNode(t *testing.T, nd node.Node) error {
assert(t, tag.Object.Defined())
assert(t, tag.Loggable()["type"] == "git_tag")
assert(t, tag.Tree("", -1) != nil)
var obj interface{}
obj, rest, err := tag.ResolveLink([]string{"object", "aoeu"})
assert(t, err == nil)
assert(t, obj != nil)
assert(t, rest != nil)
assert(t, len(rest) == 1)
assert(t, rest[0] == "aoeu")
obj, _, err = tag.Resolve(nil)
assert(t, err == nil)
assert(t, obj != nil)
case "[git tree object]":
tree, ok := nd.(*Tree)
if !ok {
Expand All @@ -250,6 +261,9 @@ func testNode(t *testing.T, nd node.Node) error {
assert(t, reflect.DeepEqual(tree.RawData(), tree.RawData()))
assert(t, tree.entries != nil)
assert(t, tree.Tree("", 0) == nil)
obj, _, err := tree.Resolve(nil)
assert(t, err == nil)
assert(t, reflect.DeepEqual(obj, tree.entries))
}
return nil
}
Expand Down Expand Up @@ -348,6 +362,13 @@ func TestParsePersonInfo(t *testing.T) {
date, _, err := pi.resolve([]string{"date"})
assert(t, string(piJSON) == `{"date":"2018-12-30T17:34:12+01:00","email":"[email protected]","name":"Łukasz Magiera"}`)
assert(t, date == "2018-12-30T17:34:12+01:00")
obj, _, err := pi.resolve(nil)
assert(t, err == nil)
assert(t, reflect.DeepEqual(obj, map[string]interface{}{
"date": "2018-12-30T17:34:12+01:00",
"email": "[email protected]",
"name": "Łukasz Magiera",
}))

pi, err = parsePersonInfo([]byte("prefix Sameer <[email protected]> 1545162499 -0500"))
piJSON, err = pi.MarshalJSON()
Expand Down
9 changes: 9 additions & 0 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ func (t *Tag) RawData() []byte {
}

func (t *Tag) Resolve(path []string) (interface{}, []string, error) {
if path == nil {
return map[string]interface{}{
"object": &node.Link{Cid: t.Object},
"type": t.Type,
"tagger": t.Tagger,
"message": t.Message,
"tag": t.Tag,
}, nil, nil
}
if len(path) == 0 {
return nil, nil, fmt.Errorf("zero length path")
}
Expand Down
4 changes: 4 additions & 0 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func (t *Tree) RawData() []byte {
}

func (t *Tree) Resolve(p []string) (interface{}, []string, error) {
if p == nil {
return t.entries, nil, nil
}

e, ok := t.entries[p[0]]
if !ok {
return nil, nil, errors.New("no such link")
Expand Down