code optimized (#382)

master
kingxt 4 years ago committed by GitHub
parent 9cd2015661
commit 7f49bd8a31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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()
}
@ -73,11 +75,8 @@ func ApiFormatByStdin() error {
}
_, err = fmt.Print(result)
if err != nil {
return err
}
return nil
}
func ApiFormatByPath(apiFilePath string) error {
data, err := ioutil.ReadFile(apiFilePath)
@ -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) {
@ -150,6 +151,7 @@ func apiFormat(data string) (string, error) {
}
preLine = line
}
return strings.TrimSpace(builder.String()), nil
}
@ -178,9 +180,9 @@ func formatGoTypeDef(line string, scanner *bufio.Scanner, builder *strings.Build
break
}
}
return true, nil
}
return false, nil
}
@ -212,5 +214,6 @@ func mayInsertStructKeyword(line string, token *int) string {
if strings.Contains(noCommentLine, "`") {
return util.UpperFirst(strings.TrimSpace(line))
}
return line
}

@ -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{

Loading…
Cancel
Save