d2/d2oracle/get.go
Alexander Wang 524c089a74 oss
Co-authored-by: Anmol Sethi <hi@nhooyr.io>
2022-11-03 06:54:49 -07:00

51 lines
988 B
Go

package d2oracle
import (
"fmt"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2parser"
)
func GetParentID(g *d2graph.Graph, absID string) (string, error) {
mk, err := d2parser.ParseMapKey(absID)
if err != nil {
return "", err
}
obj, ok := g.Root.HasChild(d2graph.Key(mk.Key))
if !ok {
return "", fmt.Errorf("%v parent not found", absID)
}
return obj.Parent.AbsID(), nil
}
func GetObj(g *d2graph.Graph, absID string) *d2graph.Object {
mk, _ := d2parser.ParseMapKey(absID)
obj, _ := g.Root.HasChild(d2graph.Key(mk.Key))
return obj
}
func GetEdge(g *d2graph.Graph, absID string) *d2graph.Edge {
for _, e := range g.Edges {
if e.AbsID() == absID {
return e
}
}
return nil
}
func IsLabelKeyID(key, label string) bool {
mk, err := d2parser.ParseMapKey(key)
if err != nil {
return false
}
if len(mk.Edges) > 0 {
return false
}
if mk.Key == nil {
return false
}
return mk.Key.Path[len(mk.Key.Path)-1].Unbox().ScalarString() == label
}