forked from tliron/puccini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphase1-read.go
165 lines (138 loc) · 4.7 KB
/
phase1-read.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
156
157
158
159
160
161
162
163
164
165
package parser
import (
"fmt"
"sort"
"github.com/tliron/kutil/reflection"
urlpkg "github.com/tliron/kutil/url"
"github.com/tliron/kutil/util"
"github.com/tliron/puccini/tosca"
"github.com/tliron/puccini/tosca/csar"
"github.com/tliron/puccini/tosca/grammars"
"github.com/tliron/yamlkeys"
)
func (self *ServiceContext) ReadRoot(url urlpkg.URL, origins []urlpkg.URL, template string) bool {
toscaContext := tosca.NewContext(self.Stylist, self.Quirks)
toscaContext.Origins = origins
toscaContext.URL = url
var ok bool
self.readWork.Add(1)
self.Root, ok = self.read(nil, toscaContext, nil, nil, "$Root", template)
self.readWork.Wait()
self.unitsLock.Lock()
sort.Sort(self.Units)
self.unitsLock.Unlock()
return ok
}
func (self *ServiceContext) read(promise util.Promise, toscaContext *tosca.Context, container *Unit, nameTransformer tosca.NameTransformer, readerName string, template string) (*Unit, bool) {
defer self.readWork.Done()
if promise != nil {
// For the goroutines waiting for our cached entityPtr
defer promise.Release()
}
logRead.Infof("%s: %s", readerName, toscaContext.URL.Key())
switch toscaContext.URL.Format() {
case "csar", "zip":
var err error
if toscaContext.URL, err = csar.GetURL(toscaContext.URL, template); err != nil {
toscaContext.ReportError(err)
unit := NewUnitNoEntity(toscaContext, container, nameTransformer)
self.AddUnit(unit)
return unit, false
}
}
// Read ARD
var err error
if toscaContext.Data, toscaContext.Locator, err = urlpkg.ReadARD(toscaContext.URL, true); err != nil {
if decodeError, ok := err.(*yamlkeys.DecodeError); ok {
err = NewYAMLDecodeError(decodeError)
}
toscaContext.ReportError(err)
unit := NewUnitNoEntity(toscaContext, container, nameTransformer)
self.AddUnit(unit)
return unit, false
}
// Detect grammar
if !grammars.Detect(toscaContext) {
unit := NewUnitNoEntity(toscaContext, container, nameTransformer)
self.AddUnit(unit)
return unit, false
}
// Read entityPtr
read, ok := toscaContext.Grammar.Readers[readerName]
if !ok {
panic(fmt.Sprintf("grammar does not support reader %q", readerName))
}
entityPtr := read(toscaContext)
if entityPtr == nil {
// Even if there are problems, the reader should return an entityPtr
panic(fmt.Sprintf("reader %q returned a non-entity: %T", reflection.GetFunctionName(read), entityPtr))
}
// Validate required fields
reflection.TraverseEntities(entityPtr, false, tosca.ValidateRequiredFields)
self.Context.readCache.Store(toscaContext.URL.Key(), entityPtr)
return self.AddImportUnit(entityPtr, container, nameTransformer), true
}
// From tosca.Importer interface
func (self *ServiceContext) goReadImports(container *Unit) {
importSpecs := tosca.GetImportSpecs(container.EntityPtr)
// Implicit import
if !container.GetContext().HasQuirk(tosca.QuirkImportsImplicitDisable) {
if implicitImportSpec, ok := grammars.GetImplicitImportSpec(container.GetContext()); ok {
importSpecs = append(importSpecs, implicitImportSpec)
}
}
for _, importSpec := range importSpecs {
key := importSpec.URL.Key()
// Skip if causes import loop
skip := false
for container_ := container; container_ != nil; container_ = container_.Container {
url := container_.GetContext().URL
if url.Key() == key {
if !importSpec.Implicit {
// Import loops are considered errors
container.GetContext().ReportImportLoop(url)
}
skip = true
break
}
}
if skip {
continue
}
promise := util.NewPromise()
if cached, inCache := self.Context.readCache.LoadOrStore(key, promise); inCache {
switch cached_ := cached.(type) {
case util.Promise:
// Wait for promise
logRead.Debugf("wait for promise: %s", key)
self.readWork.Add(1)
go self.waitForPromise(cached_, key, container, importSpec.NameTransformer)
default: // entityPtr
// Cache hit
logRead.Debugf("cache hit: %s", key)
self.AddImportUnit(cached, container, importSpec.NameTransformer)
}
} else {
importToscaContext := container.GetContext().NewImportContext(importSpec.URL)
// Read (concurrently)
self.readWork.Add(1)
go self.read(promise, importToscaContext, container, importSpec.NameTransformer, "$Unit", "")
}
}
}
func (self *ServiceContext) waitForPromise(promise util.Promise, key string, container *Unit, nameTransformer tosca.NameTransformer) {
defer self.readWork.Done()
promise.Wait()
if cached, inCache := self.Context.readCache.Load(key); inCache {
switch cached.(type) {
case util.Promise:
logRead.Debugf("promise broken: %s", key)
default: // entityPtr
// Cache hit
logRead.Debugf("promise kept: %s", key)
self.AddImportUnit(cached, container, nameTransformer)
}
} else {
logRead.Debugf("promise broken (empty): %s", key)
}
}