summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile3
-rw-r--r--addMutex.go56
-rw-r--r--example/Makefile6
-rw-r--r--example/file.proto93
-rw-r--r--file.proto8
5 files changed, 79 insertions, 87 deletions
diff --git a/Makefile b/Makefile
index 26dfb7a..ffdafa2 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
VERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
-full: clean auto goimports vet build test
+full: install clean auto goimports vet build test
@echo everything worked and the example ran
test: goimports build test
@@ -20,6 +20,7 @@ recover:
build:
GO111MODULE=off go build \
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
+ # autogen uses autogen to build. keep a working copy somewhere
cp -f autogenpb autogenpb.${BUILDTIME}
bak:
diff --git a/addMutex.go b/addMutex.go
index 5e374f8..12ea1d0 100644
--- a/addMutex.go
+++ b/addMutex.go
@@ -21,6 +21,14 @@ func (pb *Files) addMutex(f *File) error {
return err
}
+ // check if autogenpb has already looked at this file
+ for _, line := range strings.Split(string(data), "\n") {
+ if strings.Contains(line, "autogenpb DO NOT EDIT") {
+ log.Info("autogenpb has already been run")
+ return nil
+ }
+ }
+
w, _ := os.OpenFile(f.Pbfilename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
pbHeaderComment(w)
@@ -34,30 +42,20 @@ func (pb *Files) addMutex(f *File) error {
// fmt.Fprintln(w, "package "+"main")
continue
}
- var found bool
- for _, msg := range f.MsgNames {
- start := "type " + msg.Name + " struct {"
- // marshalThing(w, msg.Name)
- // log.Info("line:", line)
- if strings.HasPrefix(line, start) {
- msg.MutexFound = true
- found = true
- if argv.Mutex {
- log.Info("Adding Mutex to line:", line)
- fmt.Fprintln(w, line)
- fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
- fmt.Fprintln(w, "")
- } else {
- log.Info("Skipping. Mutex = false for", msg.Name)
- fmt.Fprintln(w, line)
- fmt.Fprintln(w, "\t// Lock sync.RWMutex // autogenpb skipped this. needs --mutex command line arg")
- fmt.Fprintln(w, "")
- }
- // important to exit here. somehow this matched twice at one point. notsure how
- break
+
+ if f.structMatch(line) {
+ if argv.Mutex {
+ log.Info("Adding Mutex to:", line)
+ fmt.Fprintln(w, line)
+ fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
+ fmt.Fprintln(w, "")
+ } else {
+ log.Info("Skipping. Mutex = false for:", line)
+ fmt.Fprintln(w, line)
+ fmt.Fprintln(w, "\t// Lock sync.RWMutex // autogenpb skipped this. needs --mutex command line arg")
+ fmt.Fprintln(w, "")
}
- }
- if !found {
+ } else {
fmt.Fprintln(w, line)
}
}
@@ -68,3 +66,15 @@ func (pb *Files) addMutex(f *File) error {
}
return nil
}
+
+// is this struct supposed to have a Mutex added?
+func (pf *File) structMatch(line string) bool {
+ for _, msg := range pf.MsgNames {
+ start := "type " + msg.Name + " struct {"
+ if strings.HasPrefix(line, start) {
+ msg.MutexFound = true
+ return true
+ }
+ }
+ return false
+}
diff --git a/example/Makefile b/example/Makefile
index bc7b48d..1f3d285 100644
--- a/example/Makefile
+++ b/example/Makefile
@@ -17,10 +17,12 @@ build:
GO111MODULE=off go build -v
withMutex:
- ../autogenpb --proto fruit.proto --package main --mutex
+ ../autogenpb --proto fruit.proto --package main
+ ../autogenpb --proto file.proto --package main
withoutMutex:
- ../autogenpb --proto fruit.proto --package main
+ ../autogenpb --proto fruit.proto --package main --mutex=false
+ ../autogenpb --proto file.proto --package main --mutex=false
goimports:
goimports -w *.go
diff --git a/example/file.proto b/example/file.proto
index d65ccd0..18b096a 100644
--- a/example/file.proto
+++ b/example/file.proto
@@ -1,5 +1,9 @@
syntax = "proto3";
+// Look at "example/fruit.proto" not this file
+
+// this file is actually used by autogenpb
+
// here are some docs, but probably it's just easier to run
// autogenpb on this file and see what gets autogenerated
// in this directory. All autogenerated files are named *.pb.go
@@ -15,76 +19,43 @@ syntax = "proto3";
package main;
-message Apple { // `autogenpb:marshal`
- string name = 1; // `autogenpb:unique` // generates SortByxxx() and AppendUnique() functions
- string genus = 2; // `autogenpb:unique` // generates same thing here but SortByGenus()
-}
-
-message Apples { // `autogenpb:marshal` `autogenpb:mutex`
- string uuid = 1; // `autogenpb:default:b2a2de35-07b6-443b-8188-709e27bee8a7`
- string version = 2; // `autogenpb:default:2`
- repeated Apple apples = 3; // `autogenpb:sort` `autogenpb:unique`
- repeated Pear pears = 4; // `autogenpb:sort` `autogenpb:unique`
- repeated Pear more = 5; // `autogenpb:sort` `autogenpb:unique` // not supported. 'More' can only be the string 'Pears'
- repeated string color = 6; // `autogenpb:sort` `autogenpb:unique`
-}
-
-message Pear {
- string name = 1; //
- string favorite = 2; // `autogenpb:sort`
-}
-
-// above is an example
-
//
// below are the actual structs autogen uses
// autogen parses the .proto file and then store the information
// it needs in these protobuf files, then it processes the
// protobuf files to write out *.sort.pb.go and *.marshal.pb.go files
//
-message MsgName {
- // If you have:
- //
- // "Shelf" for msgname
- // "Books" for name
- //
- // Then in the proto file, that would mean it would look like:
- //
- // message Shelf {
- // and then
- // repeated string Books = 42;
- //
- // autogenpb will then generate sort functions for each 'name'
- // things like:
- //
- // for _, b := range all.Book {
- //
- // and sort functions like:
- //
- // func (a ShelfBook) Less(i, j int) bool { return a[i].Book < a[j].Book }
- //
-
- string name = 1; // the name of the message aka struct. for this example: "Shelf"
- bool marshal = 2; // if "Shelf" should have Marshal & Unmarshal functions
- bool mutex = 3; // an experiment to insert a mutex into the protoc generated msg struct (bad idea?)
- repeated string sort = 4; // "Book", "Picture", etc
- repeated string aq = 5; // if the fields should have AppendUnique() functions
- repeated string uniq = 6; // the non-repeating fields that should be unique
+message MsgVar {
+ string varName = 1; // the variable name
+ string varType = 2; // the variable type
+ bool isRepeated = 3; // does the variable repeate
}
-message Unique {
- string name = 1; // the variable name of the repeatable struct that must be unique
- string msgName = 2; // the struct that is repeated
- repeated string keys = 3; // the variables in that structure to check are unique
+message MsgName {
+ string name = 1; // the name of the message aka struct. for this example: "Shelf"
+ string lockname = 2; // name of the lockfile. ends in Mu
+ bool doMarshal = 3; // if msg struct should have Marshal & Unmarshal functions
+ bool doMutex = 4; // true if a mutex is needed for the message struct
+ bool doProtocMutex = 5; // an experiment to insert a mutex into the protoc generated msg struct (bad idea?)
+ bool mutexFound = 6; // true if the mutex was added to the protoc pb.go file
+ repeated string sort = 7; // keys to sort on
+ repeated string unique = 8; // if the fields should have AppendUnique() functions
+ repeated MsgVar vars = 9; // store all the vars in the message
}
-message File { // `autogenpb:nomarshal`
- string name = 1; // for this one: autogen.proto
- string uuid = 2; // the uuid to use in a func NewMsgName()
- int64 version = 3; // the version to use in a func NewMsgName()
+message File {
+ string Package = 1; // whatever the package name is at the top of the .go file
+ string filename = 2; // yellow.proto
+ string pbfilename = 3; // yellow.pb.go
+ string filebase = 4; // yellow
+ string uuid = 5; // the uuid to use in a func NewMsgName()
+ string version = 6; // the version to use in a func NewMsgName()
+ MsgName bases = 7; // the message in "plural" form
+ MsgName base = 8; // the primary repeated message for the master struct
- // in this proto file, this would have "Apple", "Apples", ... "File", etc...
- repeated MsgName msgNames = 4; // `autogenpb:unique` // in this file
+ // every struct in this proto file, this file has: "Apple", "Apples", ... "File", etc...
+ repeated MsgName msgNames = 9;
+ repeated MsgName sortNames = 10; // variables that are repeated can have the standard functions generated (Sort(), etc)
}
// I know, I know, the whole point of using protobuf
@@ -94,8 +65,8 @@ message File { // `autogenpb:nomarshal`
// also, this could be used to modify /usr/bin/file /usr/share/magic to identify the files
// maybe this is already been done and is pointless, but it seems like a good idea
message Files { // `autogenpb:marshal`
- string uuid = 1; // `autogenpb:uuid:fakeuuid`
- string version = 2; // `autogenpb:id:42`
+ string uuid = 1; // `autogenpb:uuid:6c9ae4dd-648d-4b51-9738-bd59fb8fafd5`
+ string version = 2; // `autogenpb:version:v0.0.38`
repeated File Files = 3; // an array of each .proto file in the working directory
}
diff --git a/file.proto b/file.proto
index 740761d..18b096a 100644
--- a/file.proto
+++ b/file.proto
@@ -25,6 +25,12 @@ package main;
// it needs in these protobuf files, then it processes the
// protobuf files to write out *.sort.pb.go and *.marshal.pb.go files
//
+message MsgVar {
+ string varName = 1; // the variable name
+ string varType = 2; // the variable type
+ bool isRepeated = 3; // does the variable repeate
+}
+
message MsgName {
string name = 1; // the name of the message aka struct. for this example: "Shelf"
string lockname = 2; // name of the lockfile. ends in Mu
@@ -34,6 +40,7 @@ message MsgName {
bool mutexFound = 6; // true if the mutex was added to the protoc pb.go file
repeated string sort = 7; // keys to sort on
repeated string unique = 8; // if the fields should have AppendUnique() functions
+ repeated MsgVar vars = 9; // store all the vars in the message
}
message File {
@@ -48,6 +55,7 @@ message File {
// every struct in this proto file, this file has: "Apple", "Apples", ... "File", etc...
repeated MsgName msgNames = 9;
+ repeated MsgName sortNames = 10; // variables that are repeated can have the standard functions generated (Sort(), etc)
}
// I know, I know, the whole point of using protobuf