Skip to content

Commit

Permalink
fix bug and add update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
momaek committed Apr 19, 2021
1 parent 2133904 commit 69ebf87
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,17 @@ formattag -file /path/to/your/golang/file
```

This command will change your go file.

#### Vim
Add this to your ~/.vimrc:
```
set rtp+=$GOPATH/src/github.com/momaek/formattag/vim
```
If you have multiple entries in your GOPATH, replace $GOPATH with the right value.

Running `:PrettyTag` will run formattag on the current file.

Optionally, add this to your `~/.vimrc` to automatically run `formattag` on :w
```
autocmd BufWritePost,FileWritePost *.go execute 'PrettyTag' | checktime
```
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func main() {
func (c *config) write(node ast.Node) error {
for _, grp := range c.groups {
for _, line := range grp.lines {
if len(line.result) == 0 {
line.result = fmt.Sprintf("`%s`", strings.Join(line.tags, " "))
}

line.field.Tag.Value = line.result
}
}
Expand Down Expand Up @@ -180,15 +184,14 @@ func (c *config) preProcessStruct(st *ast.StructType, inline ...bool) {
ln.lens = lens

lineNum := c.fset.Position(field.Pos()).Line

if lineNum-lastLineNum >= 2 {
lastLineNum = lineNum
c.groups = append(c.groups, grp)
grp = group{}
continue
}

lastLineNum = lineNum

grp.lines = append(grp.lines, ln)
}

Expand All @@ -199,7 +202,7 @@ func (c *config) preProcessStruct(st *ast.StructType, inline ...bool) {

func (c *config) process() {
for _, grp := range c.groups {
if len(grp.lines) == 1 {
if len(grp.lines) <= 1 {
continue
}

Expand Down
26 changes: 4 additions & 22 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package main

//import "time"
import "time"

// TestStruct this is a test struct
/*
type TestStruct struct {
ID string `json:"id" xml:"id" bson:"id"`
IfNotModified string `json:"if_not_modified" xml:"if_not_modified"`
Expand All @@ -16,26 +15,9 @@ type TestStruct struct {

T time.Time `json:"t" xml:"t" bson:"t"`
Fset Fset `json:"fset" xml:"fset" bson:"fset"`

CreatedAt int64 `json:"created_at,omitempty" xml:"created_at" bson:"created_at,omitempty"`
UpdatedAt int64 `json:"updated_at,omitempty" xml:"updated_at,omitempty" bson:"created_at"`
}

type Fset struct{}
*/

// Location 位置,分部,办公室
type Location struct {
ID uint `json:"id,omitempty" gorm:"primaryKey"` // 主建
OrganizationID uint `json:"organization_id" gorm:"uniqueIndex:uix_org_name"` // 组织 ID
Name string `json:"name" gorm:"size:255;uniqueIndex:uix_org_name" binding:"required"` // 名称
Description string `json:"description" gorm:"type:text"` // 描述
Avatar string `json:"avatar"` // LOGO
Address string `json:"address"` // 地址
ZipCode string `json:"zip_code"` // 邮政编码
Phone string `json:"phone"` // 电话号码
Fax string `json:"fax"` // 传真
Position float64 `json:"position"` // 排序位置

CreatedAt int64 `json:"created_at,omitempty"` // 创建时间
UpdatedAt int64 `json:"updated_at,omitempty"` // 更新时间

CountMembers int `json:"count_members" sql:"-"`
}

0 comments on commit 69ebf87

Please sign in to comment.