File: node.go

package info (click to toggle)
golang-code.forgejo-f3-gof3 3.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,952 kB
  • sloc: sh: 100; makefile: 65
file content (414 lines) | stat: -rw-r--r-- 9,854 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright Earl Warren <contact@earl-warren.org>
// Copyright Loïc Dachary <loic@dachary.org>
// SPDX-License-Identifier: MIT

package generic

import (
	"context"
	"fmt"
	"sort"

	"code.forgejo.org/f3/gof3/v3/f3"
	"code.forgejo.org/f3/gof3/v3/id"
	"code.forgejo.org/f3/gof3/v3/kind"
	"code.forgejo.org/f3/gof3/v3/logger"
	"code.forgejo.org/f3/gof3/v3/path"
	"code.forgejo.org/f3/gof3/v3/util"
)

type ChildrenSlice []NodeInterface

func NewChildrenSlice(len int) ChildrenSlice {
	return make([]NodeInterface, 0, len)
}

func (o ChildrenSlice) Len() int {
	return len(o)
}

func (o ChildrenSlice) Swap(i, j int) {
	o[i], o[j] = o[j], o[i]
}

func (o ChildrenSlice) Less(i, j int) bool {
	return o[i].GetID().String() < o[j].GetID().String()
}

type NodeChildren map[id.NodeID]NodeInterface

func NewNodeChildren() NodeChildren {
	return make(NodeChildren)
}

var (
	NilNode   = &Node{isNil: true}
	NilParent = NilNode
)

type Node struct {
	logger.Logger

	tree  TreeInterface
	isNil bool
	sync  bool
	kind  kind.Kind
	id    id.NodeID

	driver NodeDriverInterface
	self   NodeInterface
	parent NodeInterface

	children NodeChildren
}

func NewElementNode() path.PathElement {
	return NewNode().(path.PathElement)
}

func NewNode() NodeInterface {
	node := &Node{}
	return node.Init(node)
}

func NewNodeFromID[T any](i T) NodeInterface {
	node := NewNode()
	node.SetID(id.NewNodeID(i))
	return node
}

func (o *Node) Init(self NodeInterface) NodeInterface {
	o.SetTree(nil)
	o.SetIsNil(true)
	o.SetKind(kind.KindNil)
	o.SetID(id.NilID)
	o.SetSelf(self)
	o.SetParent(NilNode)
	o.children = NewNodeChildren()

	return self
}

func (o *Node) GetIsNil() bool      { return o == nil || o.isNil }
func (o *Node) SetIsNil(isNil bool) { o.isNil = isNil }

func (o *Node) GetIsSync() bool     { return o.sync }
func (o *Node) SetIsSync(sync bool) { o.sync = sync }

func (o *Node) GetSelf() NodeInterface     { return o.self }
func (o *Node) SetSelf(self NodeInterface) { o.self = self }

func (o *Node) GetParent() NodeInterface       { return o.parent }
func (o *Node) SetParent(parent NodeInterface) { o.parent = parent }

func (o *Node) GetKind() kind.Kind     { return o.kind }
func (o *Node) SetKind(kind kind.Kind) { o.kind = kind }

func (o *Node) GetID() id.NodeID {
	if o.id == nil {
		return id.NilID
	}
	return o.id
}
func (o *Node) SetID(id id.NodeID) { o.id = id }

func (o *Node) GetMappedID() id.NodeID {
	mappedID := o.GetDriver().GetMappedID()
	if mappedID == nil {
		mappedID = id.NilID
	}
	return mappedID
}

func (o *Node) SetMappedID(mapped id.NodeID) {
	o.GetDriver().SetMappedID(mapped)
}

func (o *Node) GetTree() TreeInterface { return o.tree }
func (o *Node) SetTree(tree TreeInterface) {
	o.tree = tree
	if tree != nil {
		o.SetLogger(tree.GetLogger())
	}
}

func (o *Node) GetDriver() NodeDriverInterface { return o.driver }
func (o *Node) SetDriver(driver NodeDriverInterface) {
	driver.SetNode(o)
	o.driver = driver
}

func (o *Node) GetNodeChildren() NodeChildren {
	return o.children
}

func (o *Node) GetChildren() ChildrenSlice {
	children := NewChildrenSlice(len(o.children))
	for _, child := range o.children {
		children = append(children, child)
	}
	sort.Sort(children)
	return children
}

func (o *Node) SetChildren(children NodeChildren) { o.children = children }

func (o *Node) String() string {
	driver := o.GetDriver().String()
	if driver != "" {
		driver = "=" + driver
	}
	return o.GetID().String() + driver
}

func (o *Node) Equals(ctx context.Context, other NodeInterface) bool {
	return o.GetKind() == other.GetKind() && o.GetID() == other.GetID()
}

func (o *Node) GetCurrentPath() path.Path {
	var p path.Path
	if o.GetIsNil() {
		p = path.NewPath()
	} else {
		p = o.GetParent().GetCurrentPath().Append(o)
	}
	return p
}

func (o *Node) ListPage(ctx context.Context, page int) ChildrenSlice {
	return o.GetDriver().ListPage(ctx, page)
}

func (o *Node) List(ctx context.Context) ChildrenSlice {
	o.Trace("%s", o.GetKind())
	children := NewNodeChildren()

	self := o.GetSelf()
	for page := 1; ; page++ {
		util.MaybeTerminate(ctx)
		childrenPage := self.ListPage(ctx, page)
		for _, child := range childrenPage {
			children[child.GetID()] = child
		}

		if len(childrenPage) < o.GetTree().GetPageSize() {
			break
		}
	}

	o.children = children
	return o.GetChildren()
}

func (o *Node) GetChild(id id.NodeID) NodeInterface {
	child, ok := o.children[id]
	if !ok {
		return NilNode
	}
	return child
}

func (o *Node) SetChild(child NodeInterface) NodeInterface {
	o.children[child.GetID()] = child
	return child
}

func (o *Node) DeleteChild(id id.NodeID) NodeInterface {
	if child, ok := o.children[id]; ok {
		delete(o.children, id)
		return child
	}
	return NilNode
}

func (o *Node) CreateChild(ctx context.Context) NodeInterface {
	tree := o.GetTree()
	child := tree.Factory(ctx, tree.GetChildrenKind(o.GetKind()))
	child.SetParent(o)
	return child
}

func (o *Node) GetIDFromName(ctx context.Context, name string) id.NodeID {
	return o.GetDriver().GetIDFromName(ctx, name)
}

func (o *Node) Get(ctx context.Context) NodeInterface {
	if o.GetDriver().Get(ctx) {
		o.SetIsSync(true)
	}
	return o
}

func (o *Node) Upsert(ctx context.Context) NodeInterface {
	if o.GetID() != id.NilID {
		o.GetDriver().Patch(ctx)
	} else {
		o.SetID(o.GetDriver().Put(ctx))
	}
	if o.GetParent() != NilNode {
		o.GetParent().SetChild(o)
	}
	return o
}

func (o *Node) Delete(ctx context.Context) NodeInterface {
	o.GetDriver().Delete(ctx)
	return o.GetParent().DeleteChild(o.GetID())
}

func (o *Node) NewFormat() f3.Interface {
	return o.GetDriver().NewFormat()
}

func (o *Node) FromFormat(f f3.Interface) NodeInterface {
	o.SetID(id.NewNodeID(f.GetID()))
	o.GetDriver().FromFormat(f)
	return o
}

func (o *Node) ToFormat() f3.Interface {
	if o == nil || o.GetDriver() == nil {
		return nil
	}
	return o.GetDriver().ToFormat()
}

func (o *Node) LookupMappedID(id id.NodeID) id.NodeID { return o.GetDriver().LookupMappedID(id) }

func (o *Node) ApplyAndGet(ctx context.Context, p path.Path, options *ApplyOptions) bool {
	applyWrapInGet := func(options *ApplyOptions) ApplyFunc {
		return func(ctx context.Context, parent, p path.Path, node NodeInterface) {
			if options.fun != nil && (p.Empty() || options.where == ApplyEachNode) {
				options.fun(ctx, parent, p, node)
			}
			if p.Empty() {
				return
			}

			childID := p.First().(NodeInterface).GetID()
			// is it a known child?
			child := node.GetChild(childID)
			// if not, maybe it is a known child referenced by name
			if child.GetIsNil() {
				childIDFromName := node.GetIDFromName(ctx, childID.String())
				if childIDFromName != id.NilID {
					childID = childIDFromName
					child = node.GetChild(childID)
					p.First().(NodeInterface).SetID(childID)
				}
			}
			// not a known child by ID or by Name: make one
			if child.GetIsNil() {
				child = node.CreateChild(ctx)
				child.SetID(childID)
				if child.Get(ctx).GetIsSync() {
					// only set the child if the driver is able to get it, otherwise
					// it means that although the ID is known the child itself cannot be
					// obtained and is assumed to be not found
					node.SetChild(child)
				}
			}
		}
	}
	return o.Apply(ctx, path.NewPath(), p, NewApplyOptions(applyWrapInGet(options)).SetWhere(ApplyEachNode))
}

func (o *Node) WalkAndGet(ctx context.Context, parent path.Path, options *WalkOptions) {
	walkWrapInGet := func(fun WalkFunc) WalkFunc {
		return func(ctx context.Context, p path.Path, node NodeInterface) {
			node.Get(ctx)
			node.List(ctx)
			if fun != nil {
				fun(ctx, p, node)
			}
		}
	}

	o.Walk(ctx, parent, NewWalkOptions(walkWrapInGet(options.fun)))
}

func (o *Node) Walk(ctx context.Context, parent path.Path, options *WalkOptions) {
	util.MaybeTerminate(ctx)
	options.fun(ctx, parent, o.GetSelf())
	parent = parent.Append(o.GetSelf().(path.PathElement))
	for _, child := range o.GetSelf().GetChildren() {
		child.Walk(ctx, parent, options)
	}
}

func (o *Node) FindAndGet(ctx context.Context, p path.Path) NodeInterface {
	var r NodeInterface
	r = NilNode
	set := func(ctx context.Context, parent, p path.Path, node NodeInterface) {
		r = node
	}
	o.ApplyAndGet(ctx, p, NewApplyOptions(set))
	return r
}

func (o *Node) MustFind(p path.Path) NodeInterface {
	found := o.Find(p)
	if found.GetIsNil() {
		panic(fmt.Errorf("%s not found", p))
	}
	return found
}

func (o *Node) Find(p path.Path) NodeInterface {
	if p.Empty() {
		return o
	}

	child := o.GetChild(p.First().(NodeInterface).GetID())
	if child.GetIsNil() {
		o.Info("'%s' not found", p.String())
		return NilNode
	}

	return child.Find(p.RemoveFirst())
}

func (o *Node) Apply(ctx context.Context, parentPath, p path.Path, options *ApplyOptions) bool {
	o.Trace("parent '%s', node '%s', path '%s'", parentPath.ReadableString(), o.String(), p.ReadableString())

	util.MaybeTerminate(ctx)

	if p.Empty() {
		options.fun(ctx, parentPath, p, o.GetSelf())
		return true
	}

	i := p.First().(NodeInterface).GetID().String()

	if i == "." {
		return o.Apply(ctx, parentPath, p.RemoveFirst(), options)
	}

	if i == ".." {
		parent, parentPath := parentPath.Pop()
		return parent.(NodeInterface).Apply(ctx, parentPath, p.RemoveFirst(), options)
	}

	if options.where == ApplyEachNode {
		options.fun(ctx, parentPath, p, o.GetSelf())
	}

	child := o.GetChild(p.First().(NodeInterface).GetID())
	if child.GetIsNil() && options.search == ApplySearchByName {
		if childID := o.GetIDFromName(ctx, p.First().(NodeInterface).GetID().String()); childID != id.NilID {
			child = o.GetChild(childID)
		}
	}
	if child.GetIsNil() {
		return false
	}
	parentPath = parentPath.Append(o.GetSelf().(path.PathElement))
	return child.Apply(ctx, parentPath, p.RemoveFirst(), options)
}

type ErrorNodeNotFound error

func NewError[T error](message string, args ...any) T {
	e := fmt.Errorf(message, args...)
	return e.(T)
}