-
Notifications
You must be signed in to change notification settings - Fork 85
/
tree.go
112 lines (105 loc) · 2.54 KB
/
tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2015 Google Inc.
//
// 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 main
import (
"fmt"
"io"
"sort"
"github.com/openconfig/goyang/pkg/indent"
"github.com/openconfig/goyang/pkg/yang"
)
func init() {
register(&formatter{
name: "tree",
f: doTree,
help: "display in a tree format",
})
}
func doTree(w io.Writer, entries []*yang.Entry) {
for _, e := range entries {
Write(w, e)
}
}
// Write writes e, formatted, and all of its children, to w.
func Write(w io.Writer, e *yang.Entry) {
if e.Description != "" {
fmt.Fprintln(w)
fmt.Fprintln(indent.NewWriter(w, "// "), e.Description)
}
if len(e.Exts) > 0 {
fmt.Fprintf(w, "extensions: {\n")
for _, ext := range e.Exts {
if n := ext.NName(); n != "" {
fmt.Fprintf(w, " %s %s;\n", ext.Kind(), n)
} else {
fmt.Fprintf(w, " %s;\n", ext.Kind())
}
}
fmt.Fprintln(w, "}")
}
switch {
case e.RPC != nil:
fmt.Fprintf(w, "RPC: ")
case e.ReadOnly():
fmt.Fprintf(w, "RO: ")
default:
fmt.Fprintf(w, "rw: ")
}
if e.Type != nil {
fmt.Fprintf(w, "%s ", getTypeName(e))
}
name := e.Name
if e.Prefix != nil {
name = e.Prefix.Name + ":" + name
}
switch {
case e.Dir == nil && e.ListAttr != nil:
fmt.Fprintf(w, "[]%s\n", name)
return
case e.Dir == nil:
fmt.Fprintf(w, "%s\n", name)
return
case e.ListAttr != nil:
fmt.Fprintf(w, "[%s]%s {\n", e.Key, name) //}
default:
fmt.Fprintf(w, "%s {\n", name) //}
}
if r := e.RPC; r != nil {
if r.Input != nil {
Write(indent.NewWriter(w, " "), r.Input)
}
if r.Output != nil {
Write(indent.NewWriter(w, " "), r.Output)
}
}
var names []string
for k := range e.Dir {
names = append(names, k)
}
sort.Strings(names)
for _, k := range names {
Write(indent.NewWriter(w, " "), e.Dir[k])
}
// { to match the brace below to keep brace matching working
fmt.Fprintln(w, "}")
}
func getTypeName(e *yang.Entry) string {
if e == nil || e.Type == nil {
return ""
}
// Return our root's type name.
// This is should be the builtin type-name
// for this entry.
return e.Type.Root.Name
}