add api func

This commit is contained in:
Alexander Wang 2023-05-09 16:17:59 -07:00
parent b5423052d9
commit 32794f60e9
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE

View file

@ -7,6 +7,23 @@ import (
"oss.terrastruct.com/d2/d2parser" "oss.terrastruct.com/d2/d2parser"
) )
func GetChildrenIDs(g *d2graph.Graph, absID string) (ids []string, _ error) {
mk, err := d2parser.ParseMapKey(absID)
if err != nil {
return nil, err
}
obj, ok := g.Root.HasChild(d2graph.Key(mk.Key))
if !ok {
return nil, fmt.Errorf("%v not found", absID)
}
for _, ch := range obj.ChildrenArray {
ids = append(ids, ch.AbsID())
}
return ids, nil
}
func GetParentID(g *d2graph.Graph, absID string) (string, error) { func GetParentID(g *d2graph.Graph, absID string) (string, error) {
mk, err := d2parser.ParseMapKey(absID) mk, err := d2parser.ParseMapKey(absID)
if err != nil { if err != nil {