-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Dokiys/feat/gmfs
feat: optimize gmfs
- Loading branch information
Showing
16 changed files
with
196 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,5 @@ | |
# vendor/ | ||
|
||
*local_test.go | ||
|
||
**/cover.out | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,92 @@ | ||
# codemates | ||
[](https://pkg.go.dev/github.com/Dokiys/codemates) | ||
[](https://goreportcard.com/report/github.com/Dokiys/codemates) | ||
# gmfs | ||
|
||
Some tools help us to generate codes. | ||
Generate protobuf message from go struct. | ||
|
||
## Installing | ||
|
||
Install any cmd such as: | ||
```bash | ||
go install github.com/Dokiys/gmfs/cmd/gmfs@latest | ||
``` | ||
|
||
## Usage | ||
|
||
```bash | ||
$ gmfs -h | ||
usage: gmfs [OPTION] [GO_FILES] | ||
-i int | ||
Set int convert type, only allow [32,64]. (default 64) | ||
-r string | ||
Regexp match struct name. (default ".*") | ||
-v,--version Show version info and exit. | ||
``` | ||
If under macOS copy struct name and run `gmfs -s=$(pbpaste) $(ls | grep ".go") | pbcopy` at go file path will copy the result to clipboard. | ||
|
||
Example: | ||
```bash | ||
go install github.com/Dokiys/codemates/cmd/gmfs@latest | ||
$ cat example.go | ||
package example | ||
|
||
import ( | ||
"strings" | ||
"time" | ||
) | ||
|
||
func (i *Item) P() {} | ||
|
||
// Item Comment 1 | ||
/* | ||
Item Comment 1 | ||
*/ | ||
// Item Comment 1 | ||
type Item struct { | ||
// Item ItemId Comment 3 | ||
|
||
// Item ItemId Comment 2 | ||
ItemId int // Item ItemId Comment 1 | ||
Name string | ||
Duration time.Duration | ||
CreatedAt time.Time | ||
} | ||
|
||
type TemplateData struct { | ||
Arr []string | ||
Items []*Item | ||
Map1 map[string]*Item | ||
|
||
// Unsupported | ||
function func() bool | ||
strings.Reader | ||
} | ||
``` | ||
```bash | ||
$ gmfs example.go | ||
|
||
// Item Comment 1 | ||
/* | ||
Item Comment 1 | ||
*/ | ||
// Item Comment 1 | ||
message Item { | ||
// Item ItemId Comment 1 | ||
int64 item_id = 1; | ||
|
||
string name = 2; | ||
|
||
Duration duration = 3; | ||
|
||
google.protobuf.Timestamp created_at = 4; | ||
} | ||
|
||
message TemplateData { | ||
|
||
repeated string arr = 1; | ||
|
||
repeated Item items = 2; | ||
|
||
map<string,Item> map1 = 3; | ||
// Unsupported | ||
// Unsupported field: function | ||
|
||
// Unsupported field: strings.Reader | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"syscall" | ||
|
||
"github.com/Dokiys/gmfs" | ||
) | ||
|
||
const VERSION = "v0.1.0" | ||
|
||
var r string | ||
var intType int | ||
|
||
func main() { | ||
if len(os.Args) == 2 && (os.Args[1] == "--version" || os.Args[1] == "-v") { | ||
fmt.Println(VERSION) | ||
return | ||
} | ||
|
||
flag.Usage = usage | ||
flag.IntVar(&intType, "i", 64, "Set int convert type, only allow [32,64].") | ||
flag.StringVar(&r, "r", ".*", "Regexp match struct name.") | ||
flag.Parse() | ||
|
||
checkArgs() | ||
|
||
exp, _ := regexp.Compile(r) | ||
gmfs.IntType = fmt.Sprintf("int%d", intType) | ||
|
||
for _, src := range flag.Args() { | ||
f, err := os.Open(src) | ||
if err != nil { | ||
if errors.Is(err, syscall.ENOENT) { | ||
continue | ||
} | ||
|
||
errExit(err) | ||
} | ||
|
||
if err := gmfs.GenMsg(f, os.Stdout, *exp); err != nil { | ||
errExit(err) | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
func usage() { | ||
_, _ = fmt.Fprintf(os.Stderr, `usage: gmfs [OPTION] [GO_FILES] | ||
-i int | ||
Set int convert type, only allow [32,64]. (default 64) | ||
-r string | ||
Regexp match struct name. (default ".*") | ||
-v,--version Show version info and exit. | ||
`) | ||
os.Exit(2) | ||
} | ||
|
||
func checkArgs() { | ||
_, err := regexp.Compile(r) | ||
if err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "-r invalid: %r\n", err) | ||
usage() | ||
} | ||
|
||
// int32 int64 | ||
if intType != 32 && intType != 64 { | ||
usage() | ||
} | ||
|
||
if len(flag.Args()) <= 0 { | ||
usage() | ||
} | ||
} | ||
|
||
func errExit(err error) { | ||
_, _ = fmt.Fprintln(os.Stderr, err) | ||
os.Exit(2) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/* | ||
Package codemates provides cmds to help us generate code. | ||
Package gmfs provides a method to convert specified struct | ||
from reader and write to given writer. | ||
*/ | ||
package codemates | ||
package gmfs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.