-
Notifications
You must be signed in to change notification settings - Fork 1
/
element_impl.go
95 lines (83 loc) · 1.9 KB
/
element_impl.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
// Copyright (C) 2022 PuzzleDB Contributors.
//
// 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 document
// Schema format (version 1)
//
// map[uint8]any
// 1: name - string
// 2: type - uint8
const (
elementNameIdx = 1
elementTypeIdx = 2
)
type elementMap = map[uint8]any
type element struct {
data elementMap
}
// NewElement returns a blank element.
func NewElement() Element {
e := &element{
data: elementMap{},
}
return e
}
func newElementWith(obj any) (Element, error) {
em, ok := obj.(elementMap)
if !ok {
return nil, newErrElementInvalid(obj)
}
e := &element{
data: em,
}
return e, nil
}
// SetName sets the specified name to the element.
func (e *element) SetName(name string) Element {
e.data[elementNameIdx] = name
return e
}
// Name returns the unique name.
func (e *element) Name() string {
v, ok := e.data[elementNameIdx]
if !ok {
return ""
}
switch name := v.(type) {
case string:
return name
default:
return ""
}
}
// SetType sets the specified type to the element.
func (e *element) SetType(t ElementType) Element {
e.data[elementTypeIdx] = uint8(t)
return e
}
// Type returns the index type.
func (e *element) Type() ElementType {
v, ok := e.data[elementTypeIdx]
if !ok {
return 0
}
et, err := NewElementTypeWith(v)
if err != nil {
return 0
}
return et
}
// Data returns the raw representation data in memory.
func (e *element) Data() any {
return e.data
}