Skip to content

Commit

Permalink
fix a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
momaek committed Apr 18, 2021
1 parent da20fa1 commit 953f8af
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
32 changes: 10 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,10 @@ import (
"os"
"strconv"
"strings"
"time"

"github.com/fatih/structtag"
)

// TestStruct this is a test struct
type TestStruct struct {
ID string `json:"id" xml:"id"`
IfNotModified string `json:"if_not_modified" xml:"if_not_modified"`
Name string `json:"name" xml:"name"`

ThisIsAStructWodeTianNa struct {
FieldFromThisIsAStructWodeTianNa string `json:"field_from_this_is_a_struct_wode_tian_na" xml:"field_from_this_is_a_struct_wode_tian_na"`
TianName string `json:"tian_name" xml:"tian_name"`
} `json:"this_is_a_struct_wode_tian_na" xml:"this_is_a_struct_wode_tian_na"`

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

type Fset struct{}

type config struct {
fset *token.FileSet
file string
Expand Down Expand Up @@ -125,6 +107,8 @@ type line struct {
result string
}

var runID = 0

func (c *config) rewrite(node ast.Node) bool {
st, ok := node.(*ast.StructType)
if !ok {
Expand Down Expand Up @@ -173,6 +157,7 @@ func (c *config) preProcessStruct(st *ast.StructType, inline ...bool) {
lastLineNum = c.fset.Position(st.Fields.List[idx+1].Pos()).Line
}

c.groups = append(c.groups, grp)
grp = group{}
continue
}
Expand All @@ -195,6 +180,7 @@ 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)
Expand Down Expand Up @@ -243,10 +229,12 @@ func process(lines []*line, idx int) int {

func updateResult(lines []*line, max, idx int) {
for _, line := range lines {
if l := len(line.lens); l > idx && idx < l-1 {
line.result += line.tags[idx] + strings.Repeat(" ", max-line.lens[idx]+1)
} else {
line.result += line.tags[idx]
if len(line.tags) > idx {
if l := len(line.lens); l > idx && idx < l-1 {
line.result += line.tags[idx] + strings.Repeat(" ", max-line.lens[idx]+1)
} else {
line.result += line.tags[idx]
}
}
}
}
20 changes: 20 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

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"`
Name string `json:"name" xml:"name" bson:"name"`

ThisIsAStructWodeTianNa struct {
FieldFromThisIsAStructWodeTianNa string `json:"field_from_this_is_a_struct_wode_tian_na" xml:"field_from_this_is_a_struct_wode_tian_na"`
TianName string `json:"tian_name" xml:"tian_name"`
} `json:"this_is_a_struct_wode_tian_na" xml:"this_is_a_struct_wode_tian_na"`

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

type Fset struct{}

0 comments on commit 953f8af

Please sign in to comment.