+
Skip to content

Improve stringify test coverage #2320

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

Merged
merged 2 commits into from
Mar 25, 2022
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
53 changes: 52 additions & 1 deletion github/gen-stringify-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ var (
return "github.Timestamp{0001-01-01 00:00:00 +0000 UTC}"
case "nil":
return "map[]"
case `[]int{0}`:
return `[0]`
case `[]string{""}`:
return `[""]`
case "[]Scope{ScopeNone}":
return `["(no scope)"]`
}
log.Fatalf("Unhandled zero value: %q", v)
return ""
Expand Down Expand Up @@ -144,7 +150,18 @@ func (t *templateData) processAST(f *ast.File) error {
logf("Got FuncDecl: Name=%q, id.Name=%#v", fn.Name.Name, id.Name)
t.StringFuncs[id.Name] = true
} else {
logf("Ignoring FuncDecl: Name=%q, Type=%T", fn.Name.Name, fn.Recv.List[0].Type)
star, ok := fn.Recv.List[0].Type.(*ast.StarExpr)
if ok && fn.Name.Name == "String" {
id, ok := star.X.(*ast.Ident)
if ok {
logf("Got FuncDecl: Name=%q, id.Name=%#v", fn.Name.Name, id.Name)
t.StringFuncs[id.Name] = true
} else {
logf("Ignoring FuncDecl: Name=%q, Type=%T", fn.Name.Name, fn.Recv.List[0].Type)
}
} else {
logf("Ignoring FuncDecl: Name=%q, Type=%T", fn.Name.Name, fn.Recv.List[0].Type)
}
}
} else {
logf("Ignoring FuncDecl: Name=%q, fn=%#v", fn.Name.Name, fn)
Expand All @@ -157,6 +174,7 @@ func (t *templateData) processAST(f *ast.File) error {
logf("Ignoring AST decl type %T", decl)
continue
}

for _, spec := range gd.Specs {
ts, ok := spec.(*ast.TypeSpec)
if !ok {
Expand Down Expand Up @@ -188,6 +206,13 @@ func (t *templateData) processAST(f *ast.File) error {
continue
}

if at, ok := field.Type.(*ast.ArrayType); ok {
if id, ok := at.Elt.(*ast.Ident); ok {
t.addIdentSlice(id, ts.Name.String(), fieldName.String())
continue
}
}

se, ok := field.Type.(*ast.StarExpr)
if !ok {
logf("Ignoring type %T for Name=%q, FieldName=%q", field.Type, ts.Name.String(), fieldName.String())
Expand Down Expand Up @@ -272,6 +297,32 @@ func (t *templateData) addIdentPtr(x *ast.Ident, receiverType, fieldName string)
t.StructFields[receiverType] = append(t.StructFields[receiverType], newStructField(receiverType, fieldName, x.String(), zeroValue, namedStruct))
}

func (t *templateData) addIdentSlice(x *ast.Ident, receiverType, fieldName string) {
var zeroValue string
var namedStruct = false
switch x.String() {
case "int":
zeroValue = "[]int{0}"
case "int64":
zeroValue = "[]int64{0}"
case "float64":
zeroValue = "[]float64{0}"
case "string":
zeroValue = `[]string{""}`
case "bool":
zeroValue = "[]bool{false}"
case "Scope":
zeroValue = "[]Scope{ScopeNone}"
// case "Timestamp":
// zeroValue = "&Timestamp{}"
default:
zeroValue = "nil"
namedStruct = true
}

t.StructFields[receiverType] = append(t.StructFields[receiverType], newStructField(receiverType, fieldName, x.String(), zeroValue, namedStruct))
}

func (t *templateData) dump() error {
if len(t.StructFields) == 0 {
logf("No StructFields for %v; skipping.", t.filename)
Expand Down
96 changes: 81 additions & 15 deletions github/github-stringify_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载