pr comments

This commit is contained in:
Alexander Wang 2023-02-14 21:26:29 -08:00
parent 34b839c7bd
commit b41baddcee
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE

View file

@ -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
}