From b41baddcee46bee4c68d2276b95f128a93ccba31 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Tue, 14 Feb 2023 21:26:29 -0800 Subject: [PATCH] pr comments --- d2ir/d2ir.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/d2ir/d2ir.go b/d2ir/d2ir.go index a55b1dc3d..9f37917e3 100644 --- a/d2ir/d2ir.go +++ b/d2ir/d2ir.go @@ -379,7 +379,9 @@ func (eid *EdgeID) Match(eid2 *EdgeID) bool { return true } -func (eid *EdgeID) resolveToCommon(m *Map) (*d2ast.KeyPath, *EdgeID, *Map, error) { +// resolve resolves both underscores and commons in eid. +// It returns the new eid, containing map adjusted for underscores and common ida. +func (eid *EdgeID) resolve(m *Map) (_ *EdgeID, _ *Map, common []string, _ error) { eid = eid.Copy() maxUnderscores := go2.Max(countUnderscores(eid.SrcPath), countUnderscores(eid.DstPath)) for i := 0; i < maxUnderscores; i++ { @@ -401,17 +403,16 @@ func (eid *EdgeID) resolveToCommon(m *Map) (*d2ast.KeyPath, *EdgeID, *Map, error } } - common := &d2ast.KeyPath{} for len(eid.SrcPath) > 1 && len(eid.DstPath) > 1 { if !strings.EqualFold(eid.SrcPath[0], eid.DstPath[0]) { - return common, eid, m, nil + return eid, m, common, nil } - common.Path = append(common.Path, d2ast.MakeValueBox(d2ast.RawString(eid.SrcPath[0], true)).StringBox()) + common = append(common, eid.SrcPath[0]) eid.SrcPath = eid.SrcPath[1:] eid.DstPath = eid.DstPath[1:] } - return common, eid, m, nil + return eid, m, common, nil } type Edge struct { @@ -730,12 +731,12 @@ func (m *Map) DeleteField(ida ...string) *Field { } func (m *Map) GetEdges(eid *EdgeID) []*Edge { - common, eid, m, err := eid.resolveToCommon(m) + eid, m, common, err := eid.resolve(m) if err != nil { return nil } - if len(common.Path) > 0 { - f := m.GetField(common.IDA()...) + if len(common) > 0 { + f := m.GetField(common...) if f == nil { return nil } @@ -759,12 +760,12 @@ func (m *Map) CreateEdge(eid *EdgeID, refctx *RefContext) (*Edge, error) { return nil, d2parser.Errorf(refctx.Edge, "cannot create edge inside edge") } - common, eid, m, err := eid.resolveToCommon(m) + eid, m, common, err := eid.resolve(m) if err != nil { return nil, d2parser.Errorf(refctx.Edge, err.Error()) } - if len(common.Path) > 0 { - f, err := m.EnsureField(common, nil) + if len(common) > 0 { + f, err := m.EnsureField(d2ast.MakeKeyPath(common), nil) if err != nil { return nil, err }