-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
rolodex_file.go
155 lines (136 loc) · 2.98 KB
/
rolodex_file.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package index
import (
"os"
"time"
"github.com/pb33f/libopenapi/datamodel"
"gopkg.in/yaml.v3"
)
type rolodexFile struct {
location string
rolodex *Rolodex
index *SpecIndex
localFile *LocalFile
remoteFile *RemoteFile
}
func (rf *rolodexFile) Name() string {
if rf.localFile != nil {
return rf.localFile.filename
}
if rf.remoteFile != nil {
return rf.remoteFile.filename
}
return ""
}
func (rf *rolodexFile) GetIndex() *SpecIndex {
if rf.localFile != nil {
return rf.localFile.GetIndex()
}
if rf.remoteFile != nil {
return rf.remoteFile.GetIndex()
}
return nil
}
func (rf *rolodexFile) Index(config *SpecIndexConfig) (*SpecIndex, error) {
if rf.index != nil {
return rf.index, nil
}
var content []byte
if rf.localFile != nil {
content = rf.localFile.data
}
if rf.remoteFile != nil {
content = rf.remoteFile.data
}
// first, we must parse the content of the file
info, err := datamodel.ExtractSpecInfoWithDocumentCheckSync(content, config.SkipDocumentCheck)
if err != nil {
return nil, err
}
// create a new index for this file and link it to this rolodex.
config.Rolodex = rf.rolodex
index := NewSpecIndexWithConfig(info.RootNode, config)
rf.index = index
return index, nil
}
func (rf *rolodexFile) GetContent() string {
if rf.localFile != nil {
return string(rf.localFile.data)
}
if rf.remoteFile != nil {
return string(rf.remoteFile.data)
}
return ""
}
func (rf *rolodexFile) GetContentAsYAMLNode() (*yaml.Node, error) {
if rf.localFile != nil {
return rf.localFile.GetContentAsYAMLNode()
}
if rf.remoteFile != nil {
return rf.remoteFile.GetContentAsYAMLNode()
}
return nil, nil
}
func (rf *rolodexFile) GetFileExtension() FileExtension {
if rf.localFile != nil {
return rf.localFile.extension
}
if rf.remoteFile != nil {
return rf.remoteFile.extension
}
return UNSUPPORTED
}
func (rf *rolodexFile) GetFullPath() string {
if rf.localFile != nil {
return rf.localFile.fullPath
}
if rf.remoteFile != nil {
return rf.remoteFile.fullPath
}
return ""
}
func (rf *rolodexFile) ModTime() time.Time {
if rf.localFile != nil {
return rf.localFile.lastModified
}
if rf.remoteFile != nil {
return rf.remoteFile.lastModified
}
return time.Now()
}
func (rf *rolodexFile) Size() int64 {
if rf.localFile != nil {
return rf.localFile.Size()
}
if rf.remoteFile != nil {
return rf.remoteFile.Size()
}
return 0
}
func (rf *rolodexFile) IsDir() bool {
// always false.
return false
}
func (rf *rolodexFile) Sys() interface{} {
// not implemented.
return nil
}
func (rf *rolodexFile) Mode() os.FileMode {
if rf.localFile != nil {
return rf.localFile.Mode()
}
if rf.remoteFile != nil {
return rf.remoteFile.Mode()
}
return os.FileMode(0)
}
func (rf *rolodexFile) GetErrors() []error {
if rf.localFile != nil {
return rf.localFile.readingErrors
}
if rf.remoteFile != nil {
return rf.remoteFile.seekingErrors
}
return nil
}