forked from sleepinggenius2/gosmi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
module.go
131 lines (113 loc) · 3.32 KB
/
module.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
package gosmi
import (
"errors"
"fmt"
"io"
"github.com/sleepinggenius2/gosmi/models"
"github.com/sleepinggenius2/gosmi/smi"
"github.com/sleepinggenius2/gosmi/types"
)
type SmiModule struct {
models.Module
smiModule *types.SmiModule
}
func (m SmiModule) GetIdentityNode() (node SmiNode, ok bool) {
smiIdentityNode := smi.GetModuleIdentityNode(m.smiModule)
if smiIdentityNode == nil {
return
}
return CreateNode(smiIdentityNode), true
}
func (m SmiModule) GetImports() (imports []models.Import) {
for smiImport := smi.GetFirstImport(m.smiModule); smiImport != nil; smiImport = smi.GetNextImport(smiImport) {
_import := models.Import{
Module: string(smiImport.Module),
Name: string(smiImport.Name),
}
imports = append(imports, _import)
}
return
}
func (m SmiModule) GetNode(name string) (node SmiNode, err error) {
return GetNode(name, m)
}
func (m SmiModule) GetNodes(kind ...types.NodeKind) (nodes []SmiNode) {
nodeKind := types.NodeAny
if len(kind) > 0 && kind[0] != types.NodeUnknown {
nodeKind = kind[0]
}
for smiNode := smi.GetFirstNode(m.smiModule, nodeKind); smiNode != nil; smiNode = smi.GetNextNode(smiNode, nodeKind) {
nodes = append(nodes, CreateNode(smiNode))
}
return
}
func (m SmiModule) GetRevisions() (revisions []models.Revision) {
for smiRevision := smi.GetFirstRevision(m.smiModule); smiRevision != nil; smiRevision = smi.GetNextRevision(smiRevision) {
revision := models.Revision{
Date: smiRevision.Date,
Description: smiRevision.Description,
}
revisions = append(revisions, revision)
}
return
}
func (m SmiModule) GetType(name string) (outType SmiType, err error) {
return GetType(name, m)
}
func (m SmiModule) GetTypes() (types []SmiType) {
for smiType := smi.GetFirstType(m.smiModule); smiType != nil; smiType = smi.GetNextType(smiType) {
types = append(types, CreateType(smiType))
}
return
}
func (m SmiModule) GetRaw() (module *types.SmiModule) {
return m.smiModule
}
func (m *SmiModule) SetRaw(smiModule *types.SmiModule) {
m.smiModule = smiModule
}
func CreateModule(smiModule *types.SmiModule) (module SmiModule) {
return SmiModule{
Module: models.Module{
ContactInfo: smiModule.ContactInfo,
Description: smiModule.Description,
Language: smiModule.Language,
Name: string(smiModule.Name),
Organization: smiModule.Organization,
Path: smiModule.Path,
Reference: smiModule.Reference,
},
smiModule: smiModule,
}
}
func LoadModuleFromReader(r io.Reader) error {
moduleName := smi.LoadModuleFromReader(r)
if moduleName == "" {
return errors.New("Fail to load module from binary data")
}
return nil
}
func LoadModule(modulePath string) (string, error) {
moduleName := smi.LoadModule(modulePath)
if moduleName == "" {
return "", fmt.Errorf("Could not load module at %s", modulePath)
}
return moduleName, nil
}
func GetLoadedModules() (modules []SmiModule) {
for smiModule := smi.GetFirstModule(); smiModule != nil; smiModule = smi.GetNextModule(smiModule) {
modules = append(modules, CreateModule(smiModule))
}
return
}
func IsLoaded(moduleName string) bool {
return smi.IsLoaded(moduleName)
}
func GetModule(name string) (module SmiModule, err error) {
smiModule := smi.GetModule(name)
if smiModule == nil {
err = fmt.Errorf("Could not find module named %s", name)
return
}
return CreateModule(smiModule), nil
}