Skip to content

Commit fd023d2

Browse files
committed
add: message only from this file
1 parent adad216 commit fd023d2

File tree

5 files changed

+64
-44
lines changed

5 files changed

+64
-44
lines changed

main.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
// protodoc generates Protocol Buffer documentation.
1616
//
1717
// Usage:
18-
// protodoc [flags]
18+
// protodoc [flags]
1919
//
2020
// Flags:
21-
// -h, --help help for protodoc
22-
// -o, --languages value language options in field descriptions (default [Go,C++,Java,Python])
23-
// -p, --target-path string file path to save the documentation
24-
// -t, --title string title of documentation
21+
// --directories value comma separated map of target directory to parse options (e.g. 'dirA=message,dirB=message_service')
22+
// -d, --directory string target directory where Protocol Buffer files are.
23+
// -h, --help help for protodoc
24+
// -l, --languages value language options in field descriptions (Go, C++, Java, Python, Ruby, C#) (default [])
25+
// --message-only-from-this-file string if specified, it parses only the messages in this file within the directory
26+
// -o, --output string output file path to save documentation
27+
// -p, --parse value Protocol Buffer types to parse (message, service) (default [service,message])
28+
// -t, --title string title of documentation
2529
//
2630
package main
2731

@@ -48,7 +52,8 @@ var (
4852
title string
4953
outputPath string
5054

51-
targetDirectories mapString
55+
targetDirectories mapString
56+
messageOnlyFromThisFile string
5257
)
5358

5459
type mapString map[string][]parse.ParseOption
@@ -94,13 +99,14 @@ func init() {
9499
rootCommand.PersistentFlags().StringVarP(&outputPath, "output", "o", "", "output file path to save documentation")
95100

96101
rootCommand.PersistentFlags().Var(&targetDirectories, "directories", "comma separated map of target directory to parse options (e.g. 'dirA=message,dirB=message_service')")
102+
rootCommand.PersistentFlags().StringVar(&messageOnlyFromThisFile, "message-only-from-this-file", "", "if specified, it parses only the messages in this file within the directory")
97103
}
98104

99105
func CommandFunc(cmd *cobra.Command, args []string) error {
100106
var rs string
101107
if len(targetDirectories) == 0 {
102108
log.Println("opening", targetDirectory)
103-
proto, err := parse.ReadDir(targetDirectory)
109+
proto, err := parse.ReadDir(targetDirectory, "")
104110
if err != nil {
105111
return err
106112
}
@@ -121,7 +127,7 @@ func CommandFunc(cmd *cobra.Command, args []string) error {
121127
} else {
122128
for k, opts := range targetDirectories {
123129
log.Println("opening", k)
124-
proto, err := parse.ReadDir(k)
130+
proto, err := parse.ReadDir(k, messageOnlyFromThisFile)
125131
if err != nil {
126132
return err
127133
}

parse/markdown_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package parse
1717
import "testing"
1818

1919
func TestMarkdown(t *testing.T) {
20-
proto, err := ReadDir("testdata")
20+
proto, err := ReadDir("testdata", "")
2121
if err != nil {
2222
t.Fatal(err)
2323
}

parse/parse.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,33 +62,47 @@ const (
6262
parsingRPC
6363
)
6464

65-
func ReadDir(targetDir string) (*Proto, error) {
65+
func ReadDir(targetDir, messageOnlyFromThisFile string) (*Proto, error) {
6666
rm, err := walkDirExt(targetDir, ".proto")
6767
if err != nil {
6868
return nil, err
6969
}
7070

71-
var lines []string
71+
pr := &Proto{
72+
Services: []ProtoService{},
73+
Messages: []ProtoMessage{},
74+
}
7275
for _, fpath := range rm {
73-
f, err := os.OpenFile(fpath, os.O_RDONLY, 0444)
76+
p, err := ReadFile(fpath)
7477
if err != nil {
7578
return nil, err
7679
}
77-
78-
ls, err := readLines(f)
79-
if err != nil {
80-
f.Close()
81-
return nil, err
80+
pr.Services = append(pr.Services, p.Services...)
81+
if messageOnlyFromThisFile == "" ||
82+
(messageOnlyFromThisFile != "" && strings.HasSuffix(fpath, messageOnlyFromThisFile)) {
83+
pr.Messages = append(pr.Messages, p.Messages...)
8284
}
83-
lines = append(lines, ls...)
85+
}
86+
return pr, nil
87+
}
88+
89+
func ReadFile(fpath string) (*Proto, error) {
90+
f, err := os.OpenFile(fpath, os.O_RDONLY, 0444)
91+
if err != nil {
92+
return nil, err
93+
}
8494

95+
lines, err := readLines(f)
96+
if err != nil {
8597
f.Close()
98+
return nil, err
8699
}
100+
f.Close()
87101

88102
var (
89103
rp = Proto{
90-
Messages: []ProtoMessage{},
91104
Services: []ProtoService{},
105+
Messages: []ProtoMessage{},
92106
}
93107
mode = reading
94108

parse/parse_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package parse
1717
import "testing"
1818

1919
func TestReadDir(t *testing.T) {
20-
proto, err := ReadDir("testdata")
20+
proto, err := ReadDir("testdata", "")
2121
if err != nil {
2222
t.Fatal(err)
2323
}

parse/proto.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,41 @@ import "sort"
1818

1919
// Proto represents sets of 'ProtoMessage' and 'ProtoService'.
2020
type Proto struct {
21-
Messages []ProtoMessage
2221
Services []ProtoService
22+
Messages []ProtoMessage
2323
}
2424

25-
type messages []ProtoMessage
26-
27-
func (s messages) Len() int { return len(s) }
28-
func (s messages) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
29-
func (s messages) Less(i, j int) bool { return s[i].Name < s[j].Name }
30-
3125
type services []ProtoService
3226

3327
func (s services) Len() int { return len(s) }
3428
func (s services) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
3529
func (s services) Less(i, j int) bool { return s[i].Name < s[j].Name }
3630

31+
type messages []ProtoMessage
32+
33+
func (s messages) Len() int { return len(s) }
34+
func (s messages) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
35+
func (s messages) Less(i, j int) bool { return s[i].Name < s[j].Name }
36+
3737
func (p *Proto) Sort() {
38-
sort.Sort(messages(p.Messages))
3938
sort.Sort(services(p.Services))
39+
sort.Sort(messages(p.Messages))
40+
}
41+
42+
// ProtoService represents the 'service' type in Protocol Buffer.
43+
// (https://developers.google.com/protocol-buffers/docs/proto3#services)
44+
type ProtoService struct {
45+
Name string
46+
Description string
47+
Methods []ProtoMethod
48+
}
49+
50+
// ProtoMethod represents methods in ProtoService.
51+
type ProtoMethod struct {
52+
Name string
53+
Description string
54+
RequestType string
55+
ResponseType string
4056
}
4157

4258
// ProtoMessage represents the 'message' type in Protocol Buffer.
@@ -55,19 +71,3 @@ type ProtoField struct {
5571
ProtoType ProtoType
5672
UserDefinedProtoType string
5773
}
58-
59-
// ProtoService represents the 'service' type in Protocol Buffer.
60-
// (https://developers.google.com/protocol-buffers/docs/proto3#services)
61-
type ProtoService struct {
62-
Name string
63-
Description string
64-
Methods []ProtoMethod
65-
}
66-
67-
// ProtoMethod represents methods in ProtoService.
68-
type ProtoMethod struct {
69-
Name string
70-
Description string
71-
RequestType string
72-
ResponseType string
73-
}

0 commit comments

Comments
 (0)