-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Relationship labels #50
Changes from 4 commits
6c61299
92d5d6c
5b3631d
163fb48
20324e9
bd1d2f3
4a35116
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.19.6 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type RelationshipLabel struct { | ||
PkName string | ||
FkName string | ||
Label string | ||
} | ||
|
||
func parseLabels(labels []string) []RelationshipLabel { | ||
var relationshipLabels []RelationshipLabel | ||
for _, label := range labels { | ||
parsed, err := parseLabel(label) | ||
if err != nil { | ||
logrus.Warnf("label '%s' is not in the correct format", label) | ||
continue | ||
} | ||
relationshipLabels = append(relationshipLabels, parsed) | ||
} | ||
return relationshipLabels | ||
} | ||
|
||
func parseLabel(label string) (RelationshipLabel, error) { | ||
label = strings.Trim(label, " \t") | ||
matched, groups := match(label) | ||
if !matched { | ||
return RelationshipLabel{}, errors.New("invalid relationship label") | ||
} | ||
|
||
return RelationshipLabel{ | ||
PkName: string(groups[1]), | ||
FkName: string(groups[2]), | ||
Label: string(groups[3]), | ||
}, nil | ||
} | ||
|
||
var labelRegex = regexp.MustCompile(`([\w\._-]+)[\s]+([\w\._-]+)[\s]+:[\s]+([\w._-]+)`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will add comments here for the details of the regex |
||
|
||
func match(label string) (bool, [][]byte) { | ||
groups := labelRegex.FindSubmatch([]byte(label)) | ||
if groups == nil { | ||
return false, [][]byte{} | ||
} | ||
return true, groups | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,12 +59,13 @@ func (d diagram) Create(result *database.Result) error { | |
} | ||
|
||
var constraints []ErdConstraintData | ||
relationshipLabelMap := BuildRelationshipLabelMap(d.config) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Building the map is |
||
for _, constraint := range allConstraints { | ||
if shouldSkipConstraint(d.config, tableData, constraint) { | ||
continue | ||
} | ||
|
||
constraints = append(constraints, getConstraintData(d.config, constraint)) | ||
constraints = append(constraints, getConstraintData(d.config, relationshipLabelMap, constraint)) | ||
} | ||
|
||
diagramData := ErdDiagramData{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package diagram | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/KarnerTh/mermerd/config" | ||
) | ||
|
||
type RelationshipLabelMap interface { | ||
AddRelationshipLabel(label config.RelationshipLabel) | ||
LookupRelationshipLabel(pkName, fkName string) (label config.RelationshipLabel, found bool) | ||
} | ||
|
||
type relationshipLabelMap struct { | ||
mapping map[string]config.RelationshipLabel | ||
} | ||
|
||
func (r *relationshipLabelMap) AddRelationshipLabel(label config.RelationshipLabel) { | ||
if r.mapping == nil { | ||
r.mapping = make(map[string]config.RelationshipLabel) | ||
} | ||
key := r.buildMapKey(label.PkName, label.FkName) | ||
r.mapping[key] = label | ||
} | ||
|
||
func (r *relationshipLabelMap) LookupRelationshipLabel(pkName, fkName string) (label config.RelationshipLabel, found bool) { | ||
key := r.buildMapKey(pkName, fkName) | ||
label, found = r.mapping[key] | ||
return | ||
} | ||
|
||
func (r *relationshipLabelMap) buildMapKey(pkName, fkName string) string { | ||
return fmt.Sprintf("%s-%s", pkName, fkName) | ||
} | ||
|
||
func BuildRelationshipLabelMap(c config.MermerdConfig) RelationshipLabelMap { | ||
labelMap := &relationshipLabelMap{} | ||
for _, label := range c.RelationshipLabels() { | ||
labelMap.AddRelationshipLabel(label) | ||
} | ||
return labelMap | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be helpful for users of goenv