forked from aristanetworks/goyang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
135 lines (124 loc) · 3.13 KB
/
types.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 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"
"strings"
"github.com/openconfig/goyang/pkg/indent"
"github.com/openconfig/goyang/pkg/yang"
"github.com/pborman/getopt"
)
var (
typesDebug bool
typesVerbose bool
)
func init() {
flags := getopt.New()
register(&formatter{
name: "types",
f: doTypes,
help: "display found types",
flags: flags,
})
flags.BoolVarLong(&typesDebug, "types_debug", 0, "display debug information")
flags.BoolVarLong(&typesVerbose, "types_verbose", 0, "include base information")
}
func doTypes(w io.Writer, entries []*yang.Entry) {
types := Types{}
for _, e := range entries {
types.AddEntry(e)
}
for t := range types {
printType(w, t, typesVerbose)
}
if typesDebug {
for _, e := range entries {
showall(w, e)
}
}
}
// Types keeps track of all the YangTypes defined.
type Types map[*yang.YangType]struct{}
// AddEntry adds all types defined in e and its descendants to t.
func (t Types) AddEntry(e *yang.Entry) {
if e == nil {
return
}
if e.Type != nil {
t[e.Type.Root] = struct{}{}
}
for _, d := range e.Dir {
t.AddEntry(d)
}
}
// printType prints type t in a moderately human readable format to w.
func printType(w io.Writer, t *yang.YangType, verbose bool) {
if verbose && t.Base != nil {
base := yang.Source(t.Base)
if base == "unknown" {
base = "unnamed type"
}
fmt.Fprintf(w, "%s: ", base)
}
fmt.Fprintf(w, "%s", t.Root.Name)
if t.Kind.String() != t.Root.Name {
fmt.Fprintf(w, "(%s)", t.Kind)
}
if t.Units != "" {
fmt.Fprintf(w, " units=%s", t.Units)
}
if t.Default != "" {
fmt.Fprintf(w, " default=%q", t.Default)
}
if t.FractionDigits != 0 {
fmt.Fprintf(w, " fraction-digits=%d", t.FractionDigits)
}
if len(t.Length) > 0 {
fmt.Fprintf(w, " length=%s", t.Length)
}
if t.Kind == yang.YinstanceIdentifier && !t.OptionalInstance {
fmt.Fprintf(w, " required")
}
if t.Kind == yang.Yleafref && t.Path != "" {
fmt.Fprintf(w, " path=%q", t.Path)
}
if len(t.Pattern) > 0 {
fmt.Fprintf(w, " pattern=%s", strings.Join(t.Pattern, "|"))
}
b := yang.BaseTypedefs[t.Kind.String()].YangType
if len(t.Range) > 0 && !t.Range.Equal(b.Range) {
fmt.Fprintf(w, " range=%s", t.Range)
}
if len(t.Type) > 0 {
fmt.Fprintf(w, "{\n")
for _, t := range t.Type {
printType(indent.NewWriter(w, " "), t, verbose)
}
fmt.Fprintf(w, "}")
}
fmt.Fprintf(w, ";\n")
}
func showall(w io.Writer, e *yang.Entry) {
if e == nil {
return
}
if e.Type != nil {
fmt.Fprintf(w, "\n%s\n ", e.Node.Statement().Location())
printType(w, e.Type.Root, false)
}
for _, d := range e.Dir {
showall(w, d)
}
}