11package query
22
33import (
4+ "fmt"
45 "strings"
56
67 "github.com/flanksource/duty/context"
@@ -22,6 +23,25 @@ type ConfigTreeOptions struct {
2223 Outgoing RelationType
2324}
2425
26+ func (n * ConfigTreeNode ) OutgoingIDs () []uuid.UUID {
27+ var ids []uuid.UUID
28+ n .collectOutgoing (& ids , make (map [uuid.UUID ]bool ))
29+ return ids
30+ }
31+
32+ func (n * ConfigTreeNode ) collectOutgoing (ids * []uuid.UUID , seen map [uuid.UUID ]bool ) {
33+ if seen [n .ID ] {
34+ return
35+ }
36+ seen [n .ID ] = true
37+ if n .EdgeType != "parent" {
38+ * ids = append (* ids , n .ID )
39+ }
40+ for _ , c := range n .Children {
41+ c .collectOutgoing (ids , seen )
42+ }
43+ }
44+
2545func ConfigTree (ctx context.Context , configID uuid.UUID , opts ConfigTreeOptions ) (* ConfigTreeNode , error ) {
2646 config , err := GetCachedConfig (ctx , configID .String ())
2747 if err != nil {
@@ -31,7 +51,10 @@ func ConfigTree(ctx context.Context, configID uuid.UUID, opts ConfigTreeOptions)
3151 return nil , nil
3252 }
3353
34- parents := resolveParentsFromPath (ctx , config )
54+ parents , err := resolveParentsFromPath (ctx , config )
55+ if err != nil {
56+ return nil , err
57+ }
3558
3659 childIDs , err := ExpandConfigChildren (ctx , []uuid.UUID {config .ID })
3760 if err != nil {
@@ -54,7 +77,7 @@ func ConfigTree(ctx context.Context, configID uuid.UUID, opts ConfigTreeOptions)
5477 if opts .Incoming != "" {
5578 relType = opts .Incoming
5679 }
57- outType := Hard
80+ outType := Both
5881 if opts .Outgoing != "" {
5982 outType = opts .Outgoing
6083 }
@@ -72,9 +95,9 @@ func ConfigTree(ctx context.Context, configID uuid.UUID, opts ConfigTreeOptions)
7295 return buildConfigTree (config , parents , children , related ), nil
7396}
7497
75- func resolveParentsFromPath (ctx context.Context , config * models.ConfigItem ) []models.ConfigItem {
98+ func resolveParentsFromPath (ctx context.Context , config * models.ConfigItem ) ( []models.ConfigItem , error ) {
7699 if config .Path == "" {
77- return nil
100+ return nil , nil
78101 }
79102 segments := strings .Split (config .Path , "." )
80103 var parentIDs []uuid.UUID
@@ -86,11 +109,14 @@ func resolveParentsFromPath(ctx context.Context, config *models.ConfigItem) []mo
86109 parentIDs = append (parentIDs , id )
87110 }
88111 if len (parentIDs ) == 0 {
89- return nil
112+ return nil , nil
90113 }
91114 items , err := GetConfigsByIDs (ctx , parentIDs )
92- if err != nil || len (items ) == 0 {
93- return nil
115+ if err != nil {
116+ return nil , fmt .Errorf ("resolving parents from path: %w" , err )
117+ }
118+ if len (items ) == 0 {
119+ return nil , nil
94120 }
95121 byID := make (map [uuid.UUID ]models.ConfigItem , len (items ))
96122 for _ , ci := range items {
@@ -102,7 +128,7 @@ func resolveParentsFromPath(ctx context.Context, config *models.ConfigItem) []mo
102128 parents = append (parents , ci )
103129 }
104130 }
105- return parents
131+ return parents , nil
106132}
107133
108134func ExpandConfigChildren (ctx context.Context , ids []uuid.UUID ) ([]uuid.UUID , error ) {
0 commit comments