add: message only from this file

This commit is contained in:
Gyu-Ho Lee 2016-04-21 16:36:58 -07:00
parent adad2168d1
commit fd023d24d8
5 changed files with 71 additions and 51 deletions

14
main.go
View File

@ -18,9 +18,13 @@
// protodoc [flags]
//
// Flags:
// --directories value comma separated map of target directory to parse options (e.g. 'dirA=message,dirB=message_service')
// -d, --directory string target directory where Protocol Buffer files are.
// -h, --help help for protodoc
// -o, --languages value language options in field descriptions (default [Go,C++,Java,Python])
// -p, --target-path string file path to save the documentation
// -l, --languages value language options in field descriptions (Go, C++, Java, Python, Ruby, C#) (default [])
// --message-only-from-this-file string if specified, it parses only the messages in this file within the directory
// -o, --output string output file path to save documentation
// -p, --parse value Protocol Buffer types to parse (message, service) (default [service,message])
// -t, --title string title of documentation
//
package main
@ -49,6 +53,7 @@ var (
outputPath string
targetDirectories mapString
messageOnlyFromThisFile string
)
type mapString map[string][]parse.ParseOption
@ -94,13 +99,14 @@ func init() {
rootCommand.PersistentFlags().StringVarP(&outputPath, "output", "o", "", "output file path to save documentation")
rootCommand.PersistentFlags().Var(&targetDirectories, "directories", "comma separated map of target directory to parse options (e.g. 'dirA=message,dirB=message_service')")
rootCommand.PersistentFlags().StringVar(&messageOnlyFromThisFile, "message-only-from-this-file", "", "if specified, it parses only the messages in this file within the directory")
}
func CommandFunc(cmd *cobra.Command, args []string) error {
var rs string
if len(targetDirectories) == 0 {
log.Println("opening", targetDirectory)
proto, err := parse.ReadDir(targetDirectory)
proto, err := parse.ReadDir(targetDirectory, "")
if err != nil {
return err
}
@ -121,7 +127,7 @@ func CommandFunc(cmd *cobra.Command, args []string) error {
} else {
for k, opts := range targetDirectories {
log.Println("opening", k)
proto, err := parse.ReadDir(k)
proto, err := parse.ReadDir(k, messageOnlyFromThisFile)
if err != nil {
return err
}

View File

@ -17,7 +17,7 @@ package parse
import "testing"
func TestMarkdown(t *testing.T) {
proto, err := ReadDir("testdata")
proto, err := ReadDir("testdata", "")
if err != nil {
t.Fatal(err)
}

View File

@ -62,33 +62,47 @@ const (
parsingRPC
)
func ReadDir(targetDir string) (*Proto, error) {
func ReadDir(targetDir, messageOnlyFromThisFile string) (*Proto, error) {
rm, err := walkDirExt(targetDir, ".proto")
if err != nil {
return nil, err
}
var lines []string
pr := &Proto{
Services: []ProtoService{},
Messages: []ProtoMessage{},
}
for _, fpath := range rm {
p, err := ReadFile(fpath)
if err != nil {
return nil, err
}
pr.Services = append(pr.Services, p.Services...)
if messageOnlyFromThisFile == "" ||
(messageOnlyFromThisFile != "" && strings.HasSuffix(fpath, messageOnlyFromThisFile)) {
pr.Messages = append(pr.Messages, p.Messages...)
}
}
return pr, nil
}
func ReadFile(fpath string) (*Proto, error) {
f, err := os.OpenFile(fpath, os.O_RDONLY, 0444)
if err != nil {
return nil, err
}
ls, err := readLines(f)
lines, err := readLines(f)
if err != nil {
f.Close()
return nil, err
}
lines = append(lines, ls...)
f.Close()
}
var (
rp = Proto{
Messages: []ProtoMessage{},
Services: []ProtoService{},
Messages: []ProtoMessage{},
}
mode = reading

View File

@ -17,7 +17,7 @@ package parse
import "testing"
func TestReadDir(t *testing.T) {
proto, err := ReadDir("testdata")
proto, err := ReadDir("testdata", "")
if err != nil {
t.Fatal(err)
}

View File

@ -18,25 +18,41 @@ import "sort"
// Proto represents sets of 'ProtoMessage' and 'ProtoService'.
type Proto struct {
Messages []ProtoMessage
Services []ProtoService
Messages []ProtoMessage
}
type messages []ProtoMessage
func (s messages) Len() int { return len(s) }
func (s messages) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s messages) Less(i, j int) bool { return s[i].Name < s[j].Name }
type services []ProtoService
func (s services) Len() int { return len(s) }
func (s services) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s services) Less(i, j int) bool { return s[i].Name < s[j].Name }
type messages []ProtoMessage
func (s messages) Len() int { return len(s) }
func (s messages) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s messages) Less(i, j int) bool { return s[i].Name < s[j].Name }
func (p *Proto) Sort() {
sort.Sort(messages(p.Messages))
sort.Sort(services(p.Services))
sort.Sort(messages(p.Messages))
}
// ProtoService represents the 'service' type in Protocol Buffer.
// (https://developers.google.com/protocol-buffers/docs/proto3#services)
type ProtoService struct {
Name string
Description string
Methods []ProtoMethod
}
// ProtoMethod represents methods in ProtoService.
type ProtoMethod struct {
Name string
Description string
RequestType string
ResponseType string
}
// ProtoMessage represents the 'message' type in Protocol Buffer.
@ -55,19 +71,3 @@ type ProtoField struct {
ProtoType ProtoType
UserDefinedProtoType string
}
// ProtoService represents the 'service' type in Protocol Buffer.
// (https://developers.google.com/protocol-buffers/docs/proto3#services)
type ProtoService struct {
Name string
Description string
Methods []ProtoMethod
}
// ProtoMethod represents methods in ProtoService.
type ProtoMethod struct {
Name string
Description string
RequestType string
ResponseType string
}