Repository navigation
Expand file tree
/
Copy pathcompiler_reference_wire.go
More file actions
88 lines (82 loc) · 2.82 KB
/
Copy pathcompiler_reference_wire.go
File metadata and controls
88 lines (82 loc) · 2.82 KB
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
package knowledge
import (
"errors"
"fmt"
)
type wireReferences struct {
toWire, toCanonical map[string]string
}
func aliasReferences(entries []document, prefix string) wireReferences {
refs := wireReferences{toWire: map[string]string{}, toCanonical: map[string]string{}}
for i, entry := range entries {
canonical := textValue(entry["id"])
alias := fmt.Sprintf("%s%d", prefix, i)
refs.toWire[canonical] = alias
refs.toCanonical[alias] = canonical
entry["id"] = alias
}
return refs
}
type compilerReferenceWire struct {
payload document
notes, pages wireReferences
organization bool
}
// Request-local reference types keep evidence notes distinct from existing pages.
// Canonical references remain the authority for cache keys and stored receipts.
func newCompilerReferenceWire(role string, payload document) (compilerReferenceWire, error) {
wire := compilerReferenceWire{payload: payload}
if role != "discover" && role != "organize_subjects" {
return wire, nil
}
wire.organization = true
wire.payload = clone(payload)
wire.notes = aliasReferences(items(wire.payload["catalog"]), "N")
wire.pages = aliasReferences(items(wire.payload["existing_subjects"]), "P")
wire.payload["reference_types"] = document{"notes": "N references from catalog.id", "existing_subject_id": "P references from existing_subjects.id, or empty for a new page"}
for _, subject := range items(wire.payload["proposed_subjects"]) {
delete(subject, "id")
if target := textValue(subject["existing_subject_id"]); target != "" {
alias, ok := wire.pages.toWire[target]
if !ok {
return wire, errors.New("knowledge proposal page reference is outside the inspected catalog")
}
subject["existing_subject_id"] = alias
}
notes := []string{}
for _, id := range stringsValue(subject["notes"]) {
alias, ok := wire.notes.toWire[id]
if !ok {
return wire, errors.New("knowledge proposal note reference is outside the inspected catalog")
}
notes = append(notes, alias)
}
subject["notes"] = notes
}
return wire, nil
}
func (wire compilerReferenceWire) decode(result document) error {
if !wire.organization {
return nil
}
for _, subject := range items(result["subjects"]) {
delete(subject, "id")
if target := textValue(subject["existing_subject_id"]); target != "" {
canonical, ok := wire.pages.toCanonical[target]
if !ok {
return errors.New("knowledge organization referenced an uninspected existing page; expected a P reference")
}
subject["existing_subject_id"] = canonical
}
notes := []string{}
for _, id := range stringsValue(subject["notes"]) {
canonical, ok := wire.notes.toCanonical[id]
if !ok {
return errors.New("knowledge organization referenced an uninspected evidence note; expected an N reference")
}
notes = append(notes, canonical)
}
subject["notes"] = notes
}
return nil
}