-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathentity_types.go
More file actions
86 lines (68 loc) · 2.36 KB
/
Copy pathentity_types.go
File metadata and controls
86 lines (68 loc) · 2.36 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
package zep
import (
"encoding/json"
"fmt"
)
// GraphOntologyOptions contains options for graph ontology operations
type GraphOntologyOptions struct {
UserIDs []string
GraphIDs []string
}
// GraphOntologyOption is a functional option for configuring graph ontology operations
type GraphOntologyOption func(*GraphOntologyOptions)
// ForUsers allows to specify target user identifiers when setting entity/edge types.
func ForUsers(userIDs []string) GraphOntologyOption {
return func(opts *GraphOntologyOptions) {
opts.UserIDs = userIDs
}
}
// ForGraphs allows to specify target graph identifiers when setting entity/edge types.
func ForGraphs(graphIDs []string) GraphOntologyOption {
return func(opts *GraphOntologyOptions) {
opts.GraphIDs = graphIDs
}
}
type BaseEntity struct{}
type EntityDefinition interface {
// This is a marker interface - any struct embedding BaseEntity will satisfy it
isEntityDefinition()
}
// isEntityDefinition is a marker method of EntityDefinition interface
func (BaseEntity) isEntityDefinition() {}
// UnmarshalNodeAttributes unmarshals a map[string]interface{} into a struct
// that embeds BaseEntity
func UnmarshalNodeAttributes(attributes map[string]interface{}, dest EntityDefinition) error {
jsonData, err := json.Marshal(attributes)
if err != nil {
return fmt.Errorf("error marshaling node attributes: %w", err)
}
err = json.Unmarshal(jsonData, dest)
if err != nil {
return fmt.Errorf("error unmarshaling to struct: %w", err)
}
return nil
}
type BaseEdge struct{}
type EdgeDefinitionWithSourceTargets struct {
EdgeModel EdgeDefinition `json:"edge_model"`
SourceTargets []EntityEdgeSourceTarget `json:"source_targets,omitempty"`
}
type EdgeDefinition interface {
// This is a marker interface - any struct embedding BaseEdge will satisfy it
isEdgeDefinition()
}
// isEdgeDefinition is a marker method of EntityDefinition interface
func (BaseEdge) isEdgeDefinition() {}
// UnmarshalEdgeAttributes unmarshals a map[string]interface{} into a struct
// that embeds BaseEdge
func UnmarshalEdgeAttributes(attributes map[string]interface{}, dest EdgeDefinition) error {
jsonData, err := json.Marshal(attributes)
if err != nil {
return fmt.Errorf("error marshaling node attributes: %w", err)
}
err = json.Unmarshal(jsonData, dest)
if err != nil {
return fmt.Errorf("error unmarshaling to struct: %w", err)
}
return nil
}