Skip to content

Commit

Permalink
implement pureMardown flag support
Browse files Browse the repository at this point in the history
  • Loading branch information
chicco785 committed Oct 25, 2023
1 parent 4d72b69 commit 6689ad7
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 4 deletions.
24 changes: 20 additions & 4 deletions pkg/proto/writer_markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ func EnumToMarkdown(enum *Enum, wc *WriterConfig) (body string, diagram string)
if wc.visualize {
diagram = "\n" + ToMermaid(enum.Name, enum)
}
body = fmt.Sprintf("## Enum: %s\n%s\n\n%s\n\n%s\n\n", enum.Name, fmt.Sprintf(fqn, enum.Qualifier), enum.Comment.ToMarkdownBlockQuote(), enumTable.String())
if wc.pureMarkdown {
body = fmt.Sprintf("## Enum: %s\n%s\n\n%s\n\n%s\n\n", enum.Name, fmt.Sprintf(fqnPureMd, enum.Qualifier), enum.Comment.ToMarkdownText(), enumTable.String())
} else {
body = fmt.Sprintf("## Enum: %s\n%s\n\n%s\n\n%s\n\n", enum.Name, fmt.Sprintf(fqn, enum.Qualifier), enum.Comment.ToMarkdownBlockQuote(), enumTable.String())
}
return body, diagram
}

Expand Down Expand Up @@ -87,8 +91,11 @@ func MessageToMarkdown(message *Message, wc *WriterConfig) (body string, diagram
diagram = "\n" + ToMermaid(message.Name, message)
}

body = fmt.Sprintf("## Message: %s\n%s\n\n%s\n\n%s\n\n", message.Name, fmt.Sprintf(fqn, message.Qualifier), message.Comment.ToMarkdownBlockQuote(), attributeTable.String())

if wc.pureMarkdown {
body = fmt.Sprintf("## Message: %s\n%s\n\n%s\n\n%s\n\n", message.Name, fmt.Sprintf(fqnPureMd, message.Qualifier), message.Comment.ToMarkdownText(), attributeTable.String())
} else {
body = fmt.Sprintf("## Message: %s\n%s\n\n%s\n\n%s\n\n", message.Name, fmt.Sprintf(fqn, message.Qualifier), message.Comment.ToMarkdownBlockQuote(), attributeTable.String())
}
for _, e := range message.Enums {
eBody, eDiagram := EnumToMarkdown(e, wc)
body += eBody
Expand Down Expand Up @@ -122,6 +129,9 @@ func ServiceToMarkdown(s *Service, wc *WriterConfig) string {
table = ToMermaid(s.Name, s) + "\n\n" + table
}

if wc.pureMarkdown {
return fmt.Sprintf("## Service: %s\n%s\n\n%s\n\n%s\n\n", s.Name, fmt.Sprintf(fqnPureMd, s.Qualifier), s.Comment.ToMarkdownText(), table)
}
return fmt.Sprintf("## Service: %s\n%s\n\n%s\n\n%s\n\n", s.Name, fmt.Sprintf(fqn, s.Qualifier), s.Comment.ToMarkdownBlockQuote(), table)
}

Expand Down Expand Up @@ -175,6 +185,8 @@ func PackageFormatOptions(p *Package) (body string) {

const fqn = "<div style=\"font-size: 12px; margin-top: -10px;\" class=\"fqn\">FQN: %s</div>"

const fqnPureMd = "* **FQN**: %s"

const footer = `
<!-- Created by: Proto Diagram Tool -->
<!-- https://github.com/GoogleCloudPlatform/proto-gen-md-diagrams -->`
Expand All @@ -188,6 +200,10 @@ func PackageToMarkDown(p *Package, wc *WriterConfig) string {
}
out += HandleEnums(p.Enums, wc)
out += HandleMessages(p.Messages, wc)
out = fmt.Sprintf("# Package: %s\n\n%s\n\n%s\n\n%s\n\n%s\n%s\n", p.Name, p.Comment.ToMarkdownBlockQuote(), PackageFormatImports(p), PackageFormatOptions(p), out, footer)
if wc.pureMarkdown {
out = fmt.Sprintf("# Package: %s\n\n%s\n\n%s\n\n%s\n\n%s\n%s\n", p.Name, p.Comment.ToMarkdownText(), PackageFormatImports(p), PackageFormatOptions(p), out, footer)
} else {
out = fmt.Sprintf("# Package: %s\n\n%s\n\n%s\n\n%s\n\n%s\n%s\n", p.Name, p.Comment.ToMarkdownBlockQuote(), PackageFormatImports(p), PackageFormatOptions(p), out, footer)
}
return out
}
184 changes: 184 additions & 0 deletions pkg/proto/writer_markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,34 @@ func TestEnumToMarkdown(t *testing.T) {
| T_02 | 1 | |
`, wantDiagram: ``},
{name: "Enum Markdown", args: args{
enum: &Enum{
Qualified: &Qualified{
Qualifier: "test.TestEnum",
Name: "TestEnum",
Comment: "Keen Enum",
},
Values: []*EnumValue{
NewEnumValue("test.TestEnum", "0", "T_01", ""),
NewEnumValue("test.TestEnum", "1", "T_02", ""),
},
},
wc: &WriterConfig{
visualize: false,
pureMarkdown: true,
},
}, wantBody: `## Enum: TestEnum
* **FQN**: test.TestEnum
Keen Enum
| Name | Ordinal | Description |
|------|---------|-------------|
| T_01 | 0 | |
| T_02 | 1 | |
`, wantDiagram: ``},
}
for _, tt := range tests {
Expand Down Expand Up @@ -122,6 +150,30 @@ func TestHandleEnums(t *testing.T) {
| T1 | 0 | |
`},
{name: "Enums", args: args{
enums: []*Enum{{
Qualified: &Qualified{
Qualifier: "test.Service",
Name: "TestEnum",
Comment: "",
},
Values: []*EnumValue{NewEnumValue("test.Service.TestEnum", "0", "T1", "")},
}},
wc: &WriterConfig{
visualize: false,
pureMarkdown: true,
},
}, wantBody: `## Enum: TestEnum
* **FQN**: test.Service
| Name | Ordinal | Description |
|------|---------|-------------|
| T1 | 0 | |
`},
}
for _, tt := range tests {
Expand Down Expand Up @@ -177,6 +229,44 @@ func TestHandleMessages(t *testing.T) {
| Name | 1 | string | | |
`},
{name: "Handle Message", args: args{
messages: []*Message{&Message{
Qualified: &Qualified{
Qualifier: "test.Service.Message",
Name: "Message",
Comment: "",
},
Attributes: []*Attribute{{
Qualified: &Qualified{
Qualifier: "test.Service.Message.Name",
Name: "Name",
Comment: "",
},
Repeated: false,
Map: false,
Kind: []string{"string"},
Ordinal: 1,
Annotations: []*Annotation{},
}},
Messages: []*Message{},
Enums: []*Enum{},
Reserved: []*Reserved{},
}},
wc: &WriterConfig{
visualize: false,
pureMarkdown: true,
},
}, wantBody: `## Message: Message
* **FQN**: test.Service.Message
| Field | Ordinal | Type | Label | Description |
|-------|---------|--------|-------|-------------|
| Name | 1 | string | | |
`},
}
for _, tt := range tests {
Expand Down Expand Up @@ -233,6 +323,44 @@ func TestMessageToMarkdown(t *testing.T) {
| Name | 1 | string | | |
`, wantDiagram: "\n### Message Diagram\n\n```mermaid\nclassDiagram\ndirection LR\n\n%% \n\nclass Message {\n + string Name\n}\n\n```"},
{name: "Handle Message", args: args{
message: &Message{
Qualified: &Qualified{
Qualifier: "test.Service.Message",
Name: "Message",
Comment: "",
},
Attributes: []*Attribute{{
Qualified: &Qualified{
Qualifier: "test.Service.Message.Name",
Name: "Name",
Comment: "",
},
Repeated: false,
Map: false,
Kind: []string{"string"},
Ordinal: 1,
Annotations: []*Annotation{},
}},
Messages: []*Message{},
Enums: []*Enum{},
Reserved: []*Reserved{},
},
wc: &WriterConfig{
visualize: true,
pureMarkdown: true,
},
}, wantBody: `## Message: Message
* **FQN**: test.Service.Message
| Field | Ordinal | Type | Label | Description |
|-------|---------|--------|-------|-------------|
| Name | 1 | string | | |
`, wantDiagram: "\n### Message Diagram\n\n```mermaid\nclassDiagram\ndirection LR\n\n%% \n\nclass Message {\n + string Name\n}\n\n```"},
}
for _, tt := range tests {
Expand Down Expand Up @@ -355,6 +483,43 @@ func TestPackageToMarkDown(t *testing.T) {
<!-- Created by: Proto Diagram Tool -->
<!-- https://github.com/GoogleCloudPlatform/proto-gen-md-diagrams -->
`},
{name: "Package", args: args{
p: &Package{
Path: "test/location/model.proto",
Name: "test.package",
Comment: "",
Options: []*Option{},
Imports: []*Import{},
Messages: []*Message{},
Enums: []*Enum{},
Services: []*Service{},
},
wc: &WriterConfig{
pureMarkdown: true,
},
}, want: `# Package: test.package
## Imports
| Import | Description |
|--------|-------------|
## Options
| Name | Value | Description |
|------|-------|-------------|
<!-- Created by: Proto Diagram Tool -->
<!-- https://github.com/GoogleCloudPlatform/proto-gen-md-diagrams -->
`},
Expand Down Expand Up @@ -392,6 +557,25 @@ func TestServiceToMarkdown(t *testing.T) {
|--------|----------------|-----------------|-------------|
`},
{name: "Service", args: args{s: &Service{
Qualified: &Qualified{
Qualifier: "test.Service",
Name: "Service",
Comment: "",
},
Methods: []*Rpc{},
}, wc: &WriterConfig{
pureMarkdown: true,
}}, want: `## Service: Service
* **FQN**: test.Service
| Method | Parameter (In) | Parameter (Out) | Description |
|--------|----------------|-----------------|-------------|
`},
}
for _, tt := range tests {
Expand Down

0 comments on commit 6689ad7

Please sign in to comment.