-
Notifications
You must be signed in to change notification settings - Fork 0
/
plantuml.go
123 lines (102 loc) · 2.7 KB
/
plantuml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package plantuml
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/steinfletcher/apitest"
"io"
"strconv"
"strings"
)
const requestOperation = "->"
const responseOperation = "-->"
type Formatter struct {
writer io.Writer
}
type DSL struct {
count int
data bytes.Buffer
}
func (r *DSL) AddRequestRow(source, target, description, body string) {
r.addRow(requestOperation, source, target, description, body)
}
func (r *DSL) AddResponseRow(source, target, description, body string) {
r.addRow(responseOperation, source, target, description, body)
}
func (r *DSL) addRow(operation, source, target, description, body string) {
var notePosition = "left"
if operation == requestOperation {
notePosition = "right"
}
var note string
if body != "" {
note = fmt.Sprintf("\nnote %s\n%s\nend note", notePosition, escape(body))
}
r.count += 1
r.data.WriteString(fmt.Sprintf("%s%s%s: (%d) %s %s\n",
source,
operation,
target,
r.count,
description,
note))
}
func (r *DSL) ToString() string {
return fmt.Sprintf("\n@startuml\nskinparam noteFontSize 11\nskinparam monochrome true\n%s\n@enduml", r.data.String())
}
func (r *Formatter) Format(recorder *apitest.Recorder) {
var sb strings.Builder
meta, err := json.Marshal(recorder.Meta)
markup, err := buildMarkup(recorder)
if err != nil {
panic(err)
}
sb.Write(meta)
sb.Write([]byte(markup))
_, err = r.writer.Write([]byte(sb.String()))
if err != nil {
panic(err)
}
}
func NewFormatter(writer io.Writer) apitest.ReportFormatter {
return &Formatter{writer: writer}
}
func buildMarkup(r *apitest.Recorder) (string, error) {
if len(r.Events) == 0 {
return "", errors.New("no events are defined")
}
dsl := &DSL{}
for _, event := range r.Events {
switch v := event.(type) {
case apitest.HttpRequest:
httpReq := v.Value
entry, err := apitest.NewHttpRequestLogEntry(httpReq)
if err != nil {
return "", err
}
entry.Timestamp = v.Timestamp
dsl.AddRequestRow(v.Source, v.Target, fmt.Sprintf("%s %s", httpReq.Method, httpReq.URL), formatNote(entry))
case apitest.HttpResponse:
entry, err := apitest.NewHttpResponseLogEntry(v.Value)
if err != nil {
return "", err
}
entry.Timestamp = v.Timestamp
dsl.AddResponseRow(v.Source, v.Target, strconv.Itoa(v.Value.StatusCode), formatNote(entry))
case apitest.MessageRequest:
dsl.AddRequestRow(v.Source, v.Target, v.Header, v.Body)
case apitest.MessageResponse:
dsl.AddResponseRow(v.Source, v.Target, v.Header, v.Body)
default:
panic("received unknown event type")
}
}
return dsl.ToString(), nil
}
func escape(in string) string {
return in
}
func formatNote(entry apitest.LogEntry) string {
return fmt.Sprintf("%s%s", entry.Header, entry.Body)
}