Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code optimized #382

Merged
merged 4 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions tools/goctl/api/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ func GoFormatApi(c *cli.Context) error {
})
be.Add(err)
}

if be.NotNil() {
scanner.PrintError(os.Stderr, be.Err())
os.Exit(1)
}

return be.Err()
}

Expand All @@ -73,10 +75,7 @@ func ApiFormatByStdin() error {
}

_, err = fmt.Print(result)
if err != nil {
return err
}
return nil
return err
}

func ApiFormatByPath(apiFilePath string) error {
Expand All @@ -90,10 +89,12 @@ func ApiFormatByPath(apiFilePath string) error {
return err
}

if err := ioutil.WriteFile(apiFilePath, []byte(result), os.ModePerm); err != nil {
_, err = parser.ParseContent(result)
if err != nil {
return err
}
return nil

return ioutil.WriteFile(apiFilePath, []byte(result), os.ModePerm)
}

func apiFormat(data string) (string, error) {
Expand Down Expand Up @@ -150,6 +151,7 @@ func apiFormat(data string) (string, error) {
}
preLine = line
}

return strings.TrimSpace(builder.String()), nil
}

Expand Down Expand Up @@ -178,9 +180,9 @@ func formatGoTypeDef(line string, scanner *bufio.Scanner, builder *strings.Build
break
}
}

return true, nil
}

return false, nil
}

Expand Down Expand Up @@ -212,5 +214,6 @@ func mayInsertStructKeyword(line string, token *int) string {
if strings.Contains(noCommentLine, "`") {
return util.UpperFirst(strings.TrimSpace(line))
}

return line
}
5 changes: 5 additions & 0 deletions tools/goctl/api/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ func (p parser) fieldToMember(field *ast.TypeField) spec.Member {
var tag = ""
if !field.IsAnonymous {
name = field.Name.Text()
if field.Tag == nil {
panic(fmt.Sprintf("error: line %d:%d field %s has no tag", field.Name.Line(), field.Name.Column(),
field.Name.Text()))
}

tag = field.Tag.Text()
}
return spec.Member{
Expand Down