-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.go
93 lines (76 loc) · 2.3 KB
/
structs.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
// Copyright 2017 Tomas Machalek <[email protected]>
//
// 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 vertigo
import (
"fmt"
"github.com/rs/zerolog/log"
)
// -------------------------------------------------------
type structAttrs struct {
elms map[string]*Structure
}
func (sa *structAttrs) Begin(v *Structure) error {
_, ok := sa.elms[v.Name]
if ok {
return fmt.Errorf("recursive structures not supported (element %s)", v.Name)
}
sa.elms[v.Name] = v
return nil
}
func (sa *structAttrs) End(name string) (*Structure, error) {
tmp, ok := sa.elms[name]
if !ok {
return nil, fmt.Errorf("cannot close unopened structure %s", name)
}
delete(sa.elms, name)
return tmp, nil
}
func (sa *structAttrs) GetAttrs() map[string]string {
ans := make(map[string]string)
for k, v := range sa.elms {
for k2, v2 := range v.Attrs {
ans[k+"."+k2] = v2
}
}
return ans
}
func (sa *structAttrs) Size() int {
return len(sa.elms)
}
func newStructAttrs() *structAttrs {
return &structAttrs{elms: make(map[string]*Structure)}
}
// -------------------------------------------------------
// nilStructAttrs can be used e.g. in case user is not
// interested in attaching complete structural attr. information
// to each token and wants to use a custom struct. attr processing
// instead. In such case a significant amount of memory can be
// saved.
type nilStructAttrs struct{}
func (nsa *nilStructAttrs) Begin(v *Structure) error {
return nil
}
func (nsa *nilStructAttrs) End(name string) (*Structure, error) {
return &Structure{Name: name}, nil
}
func (nsa *nilStructAttrs) GetAttrs() map[string]string {
return make(map[string]string)
}
func (nsa *nilStructAttrs) Size() int {
return 0
}
func newNilStructAttrs() *nilStructAttrs {
log.Warn().Msg("using nil structattr accumulator")
return &nilStructAttrs{}
}