-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.go
98 lines (78 loc) · 2.58 KB
/
point.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
// Copyright (C) 2021 David Vogel
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"time"
"github.com/vugu/vgrouter"
)
type Point struct {
vgrouter.NavigatorRef `json:"-"`
site *Site
key string
Name string
CreatedAt time.Time
Position CoordinateOptimizable
}
func (s *Site) NewPoint(name string) *Point {
p := new(Point)
p.initData()
p.initReferences(s, s.shortIDGen.MustGenerate())
p.Name = name
return p
}
// initData initializes the object with default values and other stuff.
func (p *Point) initData() {
p.CreatedAt = time.Now()
}
// initReferences updates references from and to this object and its key.
// This is only used internally to update references for copies or marshalled objects.
// This can't be used on its own to transfer an object from one parent to another.
func (p *Point) initReferences(newParent *Site, newKey string) {
p.site, p.key = newParent, newKey
p.site.Points[p.Key()] = p
}
func (p *Point) Key() string {
return p.key
}
// Delete removes the parent's reference to this object.
func (p *Point) Delete() {
delete(p.site.Points, p.Key())
}
// Copy returns a copy of the given object.
// Expensive data like images will not be copied, but referenced.
func (p *Point) Copy(newParent *Site, newKey string) *Point {
copy := new(Point)
copy.initData()
copy.initReferences(newParent, newKey)
copy.Name = p.Name
copy.CreatedAt = p.CreatedAt
copy.Position = p.Position
return copy
}
func (p *Point) UnmarshalJSON(data []byte) error {
p.initData()
// Unmarshal structure normally. Cast it into a different type to prevent recursion with json.Unmarshal.
type tempType *Point
if err := json.Unmarshal(data, tempType(p)); err != nil {
return err
}
// Update parent references and keys.
return nil
}
// GetTweakablesAndResiduals returns a list of tweakable variables and residuals.
func (p *Point) GetTweakablesAndResiduals() ([]Tweakable, []Residualer) {
return p.Position.GetTweakablesAndResiduals()
}