Skip to content

Commit

Permalink
Merge pull request #7 from Dokiys/feat/gmfs
Browse files Browse the repository at this point in the history
feat: optimize gmfs
  • Loading branch information
Dokiys authored Mar 28, 2023
2 parents 51bf863 + 5d4166b commit 790d68e
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 182 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
# vendor/

*local_test.go

**/cover.out
.DS_Store
91 changes: 85 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,92 @@
# codemates
[![Go Reference](https://pkg.go.dev/badge/github.com/Dokiys/codemates.svg)](https://pkg.go.dev/github.com/Dokiys/codemates)
[![Go Report Card](https://goreportcard.com/badge/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
}
```
84 changes: 84 additions & 0 deletions cmd/gmfs/gmfs.go
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)
}
70 changes: 0 additions & 70 deletions cmd/gmfs/main.go

This file was deleted.

5 changes: 3 additions & 2 deletions doc.go
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
37 changes: 14 additions & 23 deletions gmfs/example_gmfs_test.go → example_gmfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"os"
"regexp"

"github.com/Dokiys/codemates/gmfs"
"github.com/Dokiys/gmfs"
)

func ExampleGenMsg() {
src := `
package example
import (
"fmt"
"strings"
"time"
)
Expand All @@ -40,24 +40,21 @@ type TemplateData struct {
Map1 map[string]*Item
// Unsupported
StrArr [][]string
MapArr map[string][]Item
aaa func() bool
Condition
*a.Condition
function func() bool
strings.Reader
}
`
r := bytes.NewReader([]byte(src))

exp, _ := regexp.Compile(".*")
_ = gmfs.GenMsg(r, os.Stdout, *exp)
// Output:
//// Item Comment 1
///*
// // Item Comment 1
// /*
// Item Comment 1
//*/
//// Item Comment 1
//message Item {
// */
// // Item Comment 1
// message Item {
// // Item ItemId Comment 1
// int64 item_id = 1;
//
Expand All @@ -66,24 +63,18 @@ type TemplateData struct {
// Duration duration = 3;
//
// google.protobuf.Timestamp created_at = 4;
//}
// }
//
//message TemplateData {
// message TemplateData {
//
// repeated string arr = 1;
//
// repeated Item items = 2;
//
// map<string,Item> map1 = 3;
// // Unsupported
// // Unsupported field: StrArr
// // Unsupported field: function
//
// // Unsupported field: MapArr
//
// // Unsupported field: aaa
//
// // Unsupported field: Condition
//
// // Unsupported field: *a.Condition
//}
// // Unsupported field: strings.Reader
// }
}
4 changes: 2 additions & 2 deletions gmfs/gmfs.go → gmfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const specTab = "\t"
const specEnter = "\n"
const commentPrefix = "//"

var TypInt = fmt.Sprintf("int%d", 32<<(^uint(0)>>63))
var IntType = fmt.Sprintf("int%d", 32<<(^uint(0)>>63))

func GenMsg(r io.Reader, w io.Writer, exp regexp.Regexp) error {
src, err := io.ReadAll(r)
Expand Down Expand Up @@ -161,7 +161,7 @@ func getSelectorExprName(expr *ast.SelectorExpr) (name string) {
func getIdentName(ident *ast.Ident) (name string) {
switch ident.Name {
case "int":
name = TypInt
name = IntType
case "float64":
name = "double"
case "float32":
Expand Down
Loading

0 comments on commit 790d68e

Please sign in to comment.