Skip to content

Commit

Permalink
format for isurus
Browse files Browse the repository at this point in the history
  • Loading branch information
mazrean committed Aug 17, 2024
1 parent 256e632 commit f28c31b
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 160 deletions.
2 changes: 1 addition & 1 deletion dbdoc/dbdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func Run(conf Config) error {
}
defer f.Close()

err = writeMermaid(f, nodes)
err = WriteMermaid(f, nodes)
if err != nil {
return fmt.Errorf("failed to write mermaid: %w", err)
}
Expand Down
36 changes: 20 additions & 16 deletions dbdoc/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"golang.org/x/tools/go/ssa"
)

func BuildFuncs(ctx *Context, pkgs []*packages.Package, ssaProgram *ssa.Program, loopRangeMap LoopRangeMap) ([]function, error) {
var funcs []function
func BuildFuncs(ctx *Context, pkgs []*packages.Package, ssaProgram *ssa.Program, loopRangeMap LoopRangeMap) ([]Function, error) {
var funcs []Function
for _, pkg := range pkgs {
for _, def := range pkg.TypesInfo.Defs {
if def == nil {
Expand Down Expand Up @@ -46,34 +46,38 @@ func BuildFuncs(ctx *Context, pkgs []*packages.Package, ssaProgram *ssa.Program,
continue
}

queries := make([]query, 0, len(stringLiterals))
queries := make([]Query, 0, len(stringLiterals))
for _, strLiteral := range stringLiterals {
newQueries := AnalyzeSQL(ctx, strLiteral)
queries = append(queries, newQueries...)
}

loopRanges := loopRangeMap[ssaFunc.Name()]
queriesInLoop := make([]inLoop[query], 0, len(queries))
queriesInLoop := make([]InLoop[Query], 0, len(queries))
for _, q := range queries {
queriesInLoop = append(queriesInLoop, inLoop[query]{
value: q,
inLoop: loopRanges.Search(ctx.FileSet, q.pos),
queriesInLoop = append(queriesInLoop, InLoop[Query]{
Value: q,
InLoop: loopRanges.Search(ctx.FileSet, q.Pos),
})
}

callsInLoop := make([]inLoop[string], 0, len(calls))
callsInLoop := make([]InLoop[Call], 0, len(calls))
for _, call := range calls {
callsInLoop = append(callsInLoop, inLoop[string]{
value: call.id,
inLoop: loopRanges.Search(ctx.FileSet, call.pos),
callsInLoop = append(callsInLoop, InLoop[Call]{
Value: Call{
FunctionID: call.id,
Pos: call.pos,
},
InLoop: loopRanges.Search(ctx.FileSet, call.pos),
})
}

funcs = append(funcs, function{
id: def.Id(),
name: def.Name(),
queries: queriesInLoop,
calls: callsInLoop,
funcs = append(funcs, Function{
ID: def.Id(),
Name: def.Name(),
Pos: def.Pos(),
Queries: queriesInLoop,
Calls: callsInLoop,
})
}
}
Expand Down
94 changes: 47 additions & 47 deletions dbdoc/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,78 +10,78 @@ import (
"github.com/mazrean/isucrud/internal/pkg/analyze"
)

func BuildGraph(funcs []function, ignoreFuncs, ignoreFuncPrefixes []string, ignoreMain, ignoreInitialize bool) []*node {
func BuildGraph(funcs []Function, ignoreFuncs, ignoreFuncPrefixes []string, ignoreMain, ignoreInitialize bool) []*Node {
type tmpEdge struct {
label string
edgeType edgeType
edgeType EdgeType
childID string
inLoop bool
}
type tmpNode struct {
*node
*Node
edges []tmpEdge
}
tmpNodeMap := make(map[string]tmpNode, len(funcs))
FUNC_LOOP:
for _, f := range funcs {
if (ignoreMain && f.name == "main") ||
(ignoreInitialize && analyze.IsInitializeFuncName(f.name)) {
if (ignoreMain && f.Name == "main") ||
(ignoreInitialize && analyze.IsInitializeFuncName(f.Name)) {
continue
}

for _, ignore := range ignoreFuncs {
if f.name == ignore {
if f.Name == ignore {
continue FUNC_LOOP
}
}

for _, ignorePrefix := range ignoreFuncPrefixes {
if strings.HasPrefix(f.name, ignorePrefix) {
if strings.HasPrefix(f.Name, ignorePrefix) {
continue FUNC_LOOP
}
}

var edges []tmpEdge
for _, q := range f.queries {
id := tableID(q.value.table)
for _, q := range f.Queries {
id := tableID(q.Value.Table)
tmpNodeMap[id] = tmpNode{
node: &node{
id: id,
label: q.value.table,
nodeType: nodeTypeTable,
Node: &Node{
ID: id,
Label: q.Value.Table,
NodeType: NodeTypeTable,
},
}

var edgeType edgeType
switch q.value.queryType {
case queryTypeSelect:
edgeType = edgeTypeSelect
case queryTypeInsert:
edgeType = edgeTypeInsert
case queryTypeUpdate:
edgeType = edgeTypeUpdate
case queryTypeDelete:
edgeType = edgeTypeDelete
var edgeType EdgeType
switch q.Value.QueryType {
case QueryTypeSelect:
edgeType = EdgeTypeSelect
case QueryTypeInsert:
edgeType = EdgeTypeInsert
case QueryTypeUpdate:
edgeType = EdgeTypeUpdate
case QueryTypeDelete:
edgeType = EdgeTypeDelete
default:
log.Printf("unknown query type: %v\n", q.value.queryType)
log.Printf("unknown query type: %v\n", q.Value.QueryType)
continue
}

edges = append(edges, tmpEdge{
label: "",
edgeType: edgeType,
childID: tableID(q.value.table),
inLoop: q.inLoop,
childID: tableID(q.Value.Table),
inLoop: q.InLoop,
})
}

for _, c := range f.calls {
id := funcID(c.value)
for _, c := range f.Calls {
id := funcID(c.Value.FunctionID)
edges = append(edges, tmpEdge{
label: "",
edgeType: edgeTypeCall,
edgeType: EdgeTypeCall,
childID: id,
inLoop: c.inLoop,
inLoop: c.InLoop,
})
}

Expand All @@ -97,20 +97,20 @@ FUNC_LOOP:
})
edges = slices.Compact(edges)

id := funcID(f.id)
id := funcID(f.ID)
tmpNodeMap[id] = tmpNode{
node: &node{
id: id,
label: f.name,
nodeType: nodeTypeFunction,
Node: &Node{
ID: id,
Label: f.Name,
NodeType: NodeTypeFunction,
},
edges: edges,
}
}

type revEdge struct {
label string
edgeType edgeType
edgeType EdgeType
parentID string
inLoop bool
}
Expand All @@ -120,7 +120,7 @@ FUNC_LOOP:
revEdgeMap[tmpEdge.childID] = append(revEdgeMap[tmpEdge.childID], revEdge{
label: tmpEdge.label,
edgeType: tmpEdge.edgeType,
parentID: tmpNode.id,
parentID: tmpNode.ID,
inLoop: tmpEdge.inLoop,
})
}
Expand All @@ -129,7 +129,7 @@ FUNC_LOOP:
newNodeMap := make(map[string]tmpNode, len(tmpNodeMap))
nodeQueue := list.New()
for id, node := range tmpNodeMap {
if node.nodeType == nodeTypeTable {
if node.NodeType == NodeTypeTable {
newNodeMap[id] = node
nodeQueue.PushBack(node)
delete(tmpNodeMap, id)
Expand All @@ -141,28 +141,28 @@ FUNC_LOOP:
nodeQueue.Remove(element)

node := element.Value.(tmpNode)
for _, edge := range revEdgeMap[node.id] {
for _, edge := range revEdgeMap[node.ID] {
parent := tmpNodeMap[edge.parentID]
newNodeMap[edge.parentID] = parent
nodeQueue.PushBack(parent)
}
delete(revEdgeMap, node.id)
delete(revEdgeMap, node.ID)
}

var nodes []*node
var nodes []*Node
for _, tmpNode := range newNodeMap {
node := tmpNode.node
node := tmpNode.Node
for _, tmpEdge := range tmpNode.edges {
child, ok := newNodeMap[tmpEdge.childID]
if !ok {
continue
}

node.edges = append(node.edges, edge{
label: tmpEdge.label,
node: child.node,
edgeType: tmpEdge.edgeType,
inLoop: tmpEdge.inLoop,
node.Edges = append(node.Edges, Edge{
Label: tmpEdge.label,
Node: child.Node,
EdgeType: tmpEdge.edgeType,
InLoop: tmpEdge.inLoop,
})
}
nodes = append(nodes, node)
Expand Down
8 changes: 4 additions & 4 deletions dbdoc/loopmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ func (v *loopRangeVisitor) Visit(node ast.Node) ast.Visitor {
switch n := node.(type) {
case *ast.ForStmt:
v.lr = append(v.lr, LoopRange{
start: n.Body.Lbrace,
end: n.Body.Rbrace,
Start: n.Body.Lbrace,
End: n.Body.Rbrace,
})
return nil
case *ast.RangeStmt:
v.lr = append(v.lr, LoopRange{
start: n.Body.Lbrace,
end: n.Body.Rbrace,
Start: n.Body.Lbrace,
End: n.Body.Rbrace,
})
return nil
}
Expand Down
40 changes: 20 additions & 20 deletions dbdoc/mermaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ var (
color string
valid bool
}{
nodeTypeTable: {"table", "テーブル", tableNodeColor, true},
nodeTypeFunction: {"func", "関数", funcNodeColor, true},
NodeTypeTable: {"table", "テーブル", tableNodeColor, true},
NodeTypeFunction: {"func", "関数", funcNodeColor, true},
}
edgeTypes = []struct {
label string
color string
valid bool
}{
edgeTypeInsert: {"INSERT", insertLinkColor, true},
edgeTypeUpdate: {"UPDATE", updateLinkColor, true},
edgeTypeDelete: {"DELETE", deleteLinkColor, true},
edgeTypeSelect: {"SELECT", selectLinkColor, true},
edgeTypeCall: {"関数呼び出し", callLinkColor, true},
EdgeTypeInsert: {"INSERT", insertLinkColor, true},
EdgeTypeUpdate: {"UPDATE", updateLinkColor, true},
EdgeTypeDelete: {"DELETE", deleteLinkColor, true},
EdgeTypeSelect: {"SELECT", selectLinkColor, true},
EdgeTypeCall: {"関数呼び出し", callLinkColor, true},
}
)

func writeMermaid(w io.StringWriter, nodes []*node) error {
func WriteMermaid(w io.StringWriter, nodes []*Node) error {
_, err := w.WriteString("# DB Graph\n")
if err != nil {
return fmt.Errorf("failed to write header: %w", err)
Expand Down Expand Up @@ -100,43 +100,43 @@ func writeMermaid(w io.StringWriter, nodes []*node) error {
}
}

edgeLinksMap := map[edgeType][]string{}
edgeLinksMap := map[EdgeType][]string{}
edgeID := 0
for _, node := range nodes {
var src string
if nodeType := nodeTypes[node.nodeType]; nodeType.valid {
src = fmt.Sprintf("%s[%s]:::%s", node.id, node.label, nodeType.name)
if nodeType := nodeTypes[node.NodeType]; nodeType.valid {
src = fmt.Sprintf("%s[%s]:::%s", node.ID, node.Label, nodeType.name)
} else {
log.Printf("unknown node type: %v\n", node.nodeType)
log.Printf("unknown node type: %v\n", node.NodeType)
continue
}

for _, edge := range node.edges {
for _, edge := range node.Edges {
var dst string
if nodeType := nodeTypes[edge.node.nodeType]; nodeType.valid {
dst = fmt.Sprintf("%s[%s]:::%s", edge.node.id, edge.node.label, nodeType.name)
if nodeType := nodeTypes[edge.Node.NodeType]; nodeType.valid {
dst = fmt.Sprintf("%s[%s]:::%s", edge.Node.ID, edge.Node.Label, nodeType.name)
} else {
log.Printf("unknown node type: %v\n", node.nodeType)
log.Printf("unknown node type: %v\n", node.NodeType)
continue
}

line := "--"
if edge.inLoop {
if edge.InLoop {
line = "=="
}

var edgeExpr string
if edge.label == "" {
if edge.Label == "" {
edgeExpr = fmt.Sprintf("%s>", line)
} else {
edgeExpr = fmt.Sprintf("%s %s %s>", line, edge.label, line)
edgeExpr = fmt.Sprintf("%s %s %s>", line, edge.Label, line)
}
_, err = w.WriteString(fmt.Sprintf(" %s %s %s\n", src, edgeExpr, dst))
if err != nil {
return fmt.Errorf("failed to write edge: %w", err)
}

edgeLinksMap[edge.edgeType] = append(edgeLinksMap[edge.edgeType], strconv.Itoa(edgeID))
edgeLinksMap[edge.EdgeType] = append(edgeLinksMap[edge.EdgeType], strconv.Itoa(edgeID))

edgeID++
}
Expand Down
Loading

0 comments on commit f28c31b

Please sign in to comment.