forked from oras-project/oras
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: properly print tree for
oras discover
(oras-project#1005)
Signed-off-by: Shiwei Zhang <[email protected]>
- Loading branch information
Showing
7 changed files
with
504 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// Package tree pretty prints trees | ||
package tree | ||
|
||
import "reflect" | ||
|
||
// Node represents a tree node. | ||
type Node struct { | ||
Value any | ||
Nodes []*Node | ||
} | ||
|
||
// New creates a new tree / root node. | ||
func New(value any) *Node { | ||
return &Node{ | ||
Value: value, | ||
} | ||
} | ||
|
||
// Add adds a leaf node. | ||
func (n *Node) Add(value any) *Node { | ||
node := New(value) | ||
n.Nodes = append(n.Nodes, node) | ||
return node | ||
} | ||
|
||
// AddPath adds a chain of nodes. | ||
func (n *Node) AddPath(values ...any) *Node { | ||
if len(values) == 0 { | ||
return nil | ||
} | ||
|
||
current := n | ||
for _, value := range values { | ||
if node := current.Find(value); node == nil { | ||
current = current.Add(value) | ||
} else { | ||
current = node | ||
} | ||
} | ||
return current | ||
} | ||
|
||
// Find finds the child node with the target value. | ||
// Nil if not found. | ||
func (n *Node) Find(value any) *Node { | ||
for _, node := range n.Nodes { | ||
if reflect.DeepEqual(node.Value, value) { | ||
return node | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package tree | ||
|
||
import ( | ||
"bytes" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestNode_Add(t *testing.T) { | ||
root := &Node{ | ||
Value: "root", | ||
} | ||
|
||
nodeNil := root.Add(nil) | ||
want := &Node{} | ||
if !reflect.DeepEqual(nodeNil, want) { | ||
t.Errorf("Node.Add() = %v, want %v", nodeNil, want) | ||
} | ||
|
||
nodeFoo := root.Add("foo") | ||
want = &Node{ | ||
Value: "foo", | ||
} | ||
if !reflect.DeepEqual(nodeFoo, want) { | ||
t.Errorf("Node.Add() = %v, want %v", nodeFoo, want) | ||
} | ||
nodeBar := nodeFoo.Add("bar") | ||
want = &Node{ | ||
Value: "bar", | ||
} | ||
if !reflect.DeepEqual(nodeBar, want) { | ||
t.Errorf("Node.Add() = %v, want %v", nodeBar, want) | ||
} | ||
|
||
node42 := root.Add(42) | ||
want = &Node{ | ||
Value: 42, | ||
} | ||
if !reflect.DeepEqual(node42, want) { | ||
t.Errorf("Node.Add() = %v, want %v", node42, want) | ||
} | ||
|
||
buf := bytes.NewBuffer(nil) | ||
printer := NewPrinter(buf) | ||
if err := printer.Print(root); err != nil { | ||
t.Fatalf("Printer.Print() error = %v", err) | ||
} | ||
gotPrint := buf.String() | ||
// root | ||
// ├── <nil> | ||
// ├── foo | ||
// │ └── bar | ||
// └── 42 | ||
wantPrint := "root\n├── <nil>\n├── foo\n│ └── bar\n└── 42\n" | ||
if gotPrint != wantPrint { | ||
t.Errorf("Node = %s, want %s", gotPrint, wantPrint) | ||
} | ||
} | ||
|
||
func TestNode_AddPath(t *testing.T) { | ||
root := &Node{ | ||
Value: "root", | ||
} | ||
|
||
nodeNil := root.AddPath() | ||
var want *Node | ||
if !reflect.DeepEqual(nodeNil, want) { | ||
t.Errorf("Node.AddPath() = %v, want %v", nodeNil, want) | ||
} | ||
|
||
nodeBar := root.AddPath("foo", "bar") | ||
want = &Node{ | ||
Value: "bar", | ||
} | ||
if !reflect.DeepEqual(nodeBar, want) { | ||
t.Errorf("Node.AddPath() = %v, want %v", nodeBar, want) | ||
} | ||
nodeBar2 := root.AddPath("foo", "bar2") | ||
want = &Node{ | ||
Value: "bar2", | ||
} | ||
if !reflect.DeepEqual(nodeBar2, want) { | ||
t.Errorf("Node.AddPath() = %v, want %v", nodeBar2, want) | ||
} | ||
|
||
node42 := root.AddPath(42) | ||
want = &Node{ | ||
Value: 42, | ||
} | ||
if !reflect.DeepEqual(node42, want) { | ||
t.Errorf("Node.AddPath() = %v, want %v", node42, want) | ||
} | ||
|
||
buf := bytes.NewBuffer(nil) | ||
printer := NewPrinter(buf) | ||
if err := printer.Print(root); err != nil { | ||
t.Fatalf("Printer.Print() error = %v", err) | ||
} | ||
gotPrint := buf.String() | ||
// root | ||
// ├── foo | ||
// │ ├── bar | ||
// │ └── bar2 | ||
// └── 42 | ||
wantPrint := "root\n├── foo\n│ ├── bar\n│ └── bar2\n└── 42\n" | ||
if gotPrint != wantPrint { | ||
t.Errorf("Node = %s, want %s", gotPrint, wantPrint) | ||
} | ||
} | ||
|
||
func TestNode_Find(t *testing.T) { | ||
root := &Node{ | ||
Value: "root", | ||
Nodes: []*Node{ | ||
{ | ||
Value: "foo", | ||
Nodes: []*Node{ | ||
{ | ||
Value: "bar", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Value: 42, | ||
}, | ||
}, | ||
} | ||
tests := []struct { | ||
name string | ||
value any | ||
want *Node | ||
}{ | ||
{ | ||
name: "find existing node", | ||
value: 42, | ||
want: root.Nodes[1], | ||
}, | ||
{ | ||
name: "find non-existing node", | ||
value: "hello", | ||
want: nil, | ||
}, | ||
{ | ||
name: "find non-existing node but it is a grand child", | ||
value: "bar", | ||
want: nil, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := root.Find(tt.value); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Node.Find() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package tree | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
// Box-drawing symbols | ||
const ( | ||
EdgeEmpty = " " | ||
EdgePipe = "│ " | ||
EdgeItem = "├── " | ||
EdgeLast = "└── " | ||
) | ||
|
||
// DefaultPrinter prints the tree to the stdout with default settings. | ||
var DefaultPrinter = NewPrinter(os.Stdout) | ||
|
||
// Printer prints the tree. | ||
type Printer struct { | ||
writer io.Writer | ||
} | ||
|
||
// NewPrinter create s a new printer. | ||
func NewPrinter(writer io.Writer) *Printer { | ||
return &Printer{ | ||
writer: writer, | ||
} | ||
} | ||
|
||
// Print prints a tree. | ||
func (p *Printer) Print(root *Node) error { | ||
return p.print("", root) | ||
} | ||
|
||
// print prints a tree recursively. | ||
func (p *Printer) print(prefix string, n *Node) error { | ||
if _, err := fmt.Fprintln(p.writer, n.Value); err != nil { | ||
return err | ||
} | ||
size := len(n.Nodes) | ||
if size == 0 { | ||
return nil | ||
} | ||
|
||
prefixItem := prefix + EdgeItem | ||
prefixPipe := prefix + EdgePipe | ||
last := size - 1 | ||
for _, n := range n.Nodes[:last] { | ||
if _, err := io.WriteString(p.writer, prefixItem); err != nil { | ||
return err | ||
} | ||
if err := p.print(prefixPipe, n); err != nil { | ||
return nil | ||
} | ||
} | ||
if _, err := io.WriteString(p.writer, prefix+EdgeLast); err != nil { | ||
return err | ||
} | ||
return p.print(prefix+EdgeEmpty, n.Nodes[last]) | ||
} | ||
|
||
// Print prints the tree using the default printer. | ||
func Print(root *Node) error { | ||
return DefaultPrinter.Print(root) | ||
} |
Oops, something went wrong.