pr comments
This commit is contained in:
parent
34b839c7bd
commit
b41baddcee
1 changed files with 12 additions and 11 deletions
23
d2ir/d2ir.go
23
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue