-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
106 lines (91 loc) · 2.48 KB
/
value.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
// Copyright 2019 Melvin Davis<hi@melvindavis.me>. All rights reserved.
// Use of this source code is governed by a Melvin Davis<hi@melvindavis.me>
// license that can be found in the LICENSE file.
package interpreter
import "encoding/json"
/*
* This file contains the defnition value type node
*/
//ValueNode is the node storing the information about a value in a column.
//It can be the child of a Column Node and cannot have any children
type ValueNode struct {
//UID is the unique id of the value node
UID string
//Word is the word with which the value node has to be matched
Word []rune
//PN is the parent node of the value. It will be a column node
PN *ColumnNode
//PUID is the UID of value's parent node
PUID string
//Name is the name of the node
Name string
//Resolved indicates that the node is resolved
Resolved bool
}
type valueNode struct {
UID string `json:"uid,omitempty"`
Word string `json:"word,omitempty"`
PUID string `json:"puid,omitempty"`
Name string `json:"name,omitempty"`
Resolved bool `json:"resolved,omitempty"`
Type string `json:"type,omitempty"`
}
//Copy will return a copy of the node
func (v *ValueNode) Copy() Node {
return &ValueNode{
UID: v.UID,
Word: v.Word,
PN: v.PN,
PUID: v.PUID,
Name: v.Name,
Resolved: v.Resolved,
}
}
//ID returns the unique id of the node
func (v *ValueNode) ID() string {
return v.UID
}
//Type returns Value Type
func (v *ValueNode) Type() Type {
return Value
}
//TokenWord returns the word property of the node
func (v *ValueNode) TokenWord() []rune {
return v.Word
}
//PID returns the PUID if the node
func (v *ValueNode) PID() string {
return v.PUID
}
//Parent returns the PN of the node
func (v *ValueNode) Parent() Node {
return v.PN
}
//MarshalJSON encodes the node into a serializable json
func (v *ValueNode) MarshalJSON() ([]byte, error) {
return json.Marshal(&valueNode{
v.UID, string(v.Word), v.PUID, v.Name, v.Resolved, "Value",
})
}
//UnmarshalJSON decodes the node from a json
func (v *ValueNode) UnmarshalJSON(data []byte) error {
m := &valueNode{}
err := json.Unmarshal(data, m)
if err != nil {
return err
}
v.UID = m.UID
v.Word = []rune(m.Word)
v.PUID = m.PUID
v.Name = m.Name
v.Resolved = m.Resolved
return nil
}
//IsResolved will return true if the node is resolved
func (v *ValueNode) IsResolved() bool {
return v.Resolved
}
//SetResolved will set the resolved state of the node
func (v *ValueNode) SetResolved(state bool) {
v.Resolved = state
}