Skip to content

Commit

Permalink
Fix go vet 11 issue with debugAsJSON func
Browse files Browse the repository at this point in the history
* contributes go-swagger#1724

Signed-off-by: Frederic BIDON <[email protected]>
  • Loading branch information
fredbi committed Nov 16, 2018
1 parent 5ef69e2 commit 837c26c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
15 changes: 8 additions & 7 deletions docs/guidelines/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ go install github.com/go-swagger/go-swagger/cmd/swagger
Running standard unit tests:
```bash
go test ./...
```
```

More advanced tests are run by CI. See [below](#continuous-integration).

Expand All @@ -49,7 +49,7 @@ stable `go-openapi` repos. We manage this with build tags. Notice the very
important blank line after your build tag comment line.

Example (from `go-openapi/swag`):
```go
```go
// +build !go1.8

package swag
Expand All @@ -61,7 +61,7 @@ func pathUnescape(path string) (string, error) {
}
```

All repos should remain go-gettable (i.e. available with the `go get ...` command)
All repos should remain go-gettable (i.e. available with the `go get ...` command)
and testable with `go test ./...`

### Continuous integration
Expand Down Expand Up @@ -107,7 +107,7 @@ Deprecated engines:
The script `./hack/codegen-nonreg.sh` runs on CI with a single generation option.
You may run it manually to explore more generation options (expand spec, flatten, etc...).

CircleCI has a separate CI workflow to build releases, baking and checking newly released docker
CircleCI has a separate CI workflow to build releases, baking and checking newly released docker
images.

##### go-openapi repos
Expand Down Expand Up @@ -152,13 +152,14 @@ With `go-swagger` (vendored):
to get the proper version of the required dependency from the unmerged branch (e.g. from your fork)

With `go-openapi` (non vendored):
- for your "WIP" PR, temporarily alter the CI config script (e.g. `.travis.yml`) and
- for your "WIP" PR, temporarily alter the CI config script (e.g. `.travis.yml`) and
replace the `go get` requirements to build your CI with the adequate `git clone`
pointing to the required branches

### Update templates

`go-swagger` is built with an in-memory image of templates.

Binary encoded assets are auto-generated from the `generator/templates` directory using `bindata`
(the result is `generator/bindata.go`).

Expand All @@ -172,13 +173,13 @@ For `bindata` please use the fork found at: `github.com/kevinburke/go-bindata`.

> **NOTE**: we are carrying out unit tests on codegen mostly by asserting lines in generated code.
> There is a bunch of test utility functions for this. See `generator/*_test.go`.
> If you want to bring in more advanced testing go programs with your fixtures, please tag
> If you want to bring in more advanced testing go programs with your fixtures, please tag
> those so they don't affect the `go ./...` command (e.g. with `// +build +integration`).
### Updating examples

Whenever code generation rules change, we feel it is important to maintain
consistency with the generated code provided as examples.
consistency with the generated code provided as examples.

The script `./hack/regen-samples.sh` does just that.

Expand Down
12 changes: 8 additions & 4 deletions generator/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ func debugLog(frmt string, args ...interface{}) {
// debugLogAsJSON unmarshals its last arg as pretty JSON
func debugLogAsJSON(frmt string, args ...interface{}) {
if Debug {
var dfrmt string
_, file, pos, _ := runtime.Caller(1)
dargs := make([]interface{}, 0, len(args)+2)
dargs = append(dargs, filepath.Base(file), pos)
if len(args) > 0 {
dfrmt = "%s:%d: " + frmt + "\n%s"
bbb, _ := json.MarshalIndent(args[len(args)-1], "", " ")
generatorLogger.Printf("%s:%d: %s\n%s", filepath.Base(file), pos,
fmt.Sprintf(frmt, args[0:len(args)-1]...), string(bbb))
dargs = append(dargs, args[0:len(args)-1]...)
dargs = append(dargs, string(bbb))
} else {
generatorLogger.Printf("%s:%d: %s", filepath.Base(file), pos,
fmt.Sprintf(frmt, args...))
dfrmt = "%s:%d: " + frmt
}
generatorLogger.Printf(dfrmt, dargs...)
}
}
13 changes: 13 additions & 0 deletions generator/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,16 @@ func TestDebug(t *testing.T) {
assert.Contains(t, string(buf2), `"fieldOne":`)
assert.Contains(t, string(buf2), `"content"`)
}

func TestDebugAsJSON(t *testing.T) {
t.SkipNow()
var body struct {
A string `json:"a"`
B int `json:"b"`
}
Debug = true
body.A = "abc"
body.B = 123
debugLogAsJSON("No arg")
debugLogAsJSON("With arg:%t", true, body)
}
6 changes: 3 additions & 3 deletions generator/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ func (sg *schemaGenContext) buildProperties() error {
debugLog("building properties %s (parent: %s)", sg.Name, sg.Container)

for k, v := range sg.Schema.Properties {
debugLogAsJSON("building property %s[%q] (tup: %t) (BaseType: %t) %s",
debugLogAsJSON("building property %s[%q] (tup: %t) (BaseType: %t)",
sg.Name, k, sg.IsTuple, sg.GenSchema.IsBaseType, sg.Schema)
debugLog("property %s[%q] (tup: %t) HasValidations: %t)",
sg.Name, k, sg.IsTuple, sg.GenSchema.HasValidations)
Expand Down Expand Up @@ -1185,7 +1185,7 @@ func (sg *schemaGenContext) buildAdditionalProperties() error {
}
sg.MergeResult(cp, false)
sg.GenSchema.AdditionalProperties = &cp.GenSchema
debugLog("added interface{} schema for additionalProperties[allows == true]", cp.GenSchema.IsInterface)
debugLog("added interface{} schema for additionalProperties[allows == true], IsInterface=%t", cp.GenSchema.IsInterface)
}
return nil
}
Expand Down Expand Up @@ -1315,7 +1315,7 @@ func (sg *schemaGenContext) buildAdditionalProperties() error {
}

func (sg *schemaGenContext) makeNewStruct(name string, schema spec.Schema) *schemaGenContext {
debugLog("making new struct", name, sg.Container)
debugLog("making new struct: name: %s, container: %s", name, sg.Container)
sp := sg.TypeResolver.Doc.Spec()
name = swag.ToGoName(name)
if sg.TypeResolver.ModelName != sg.Name {
Expand Down
2 changes: 1 addition & 1 deletion generator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func knownDefGoType(def string, schema spec.Schema, clear func(string) string) (
} else {
alias = filepath.Base(pkg)
}
debugLog("known def type %s no clear: %q", xGoType, alias+"."+t, pkg, alias)
debugLog("known def type %s no clear: %q: pkg=%s, alias=%s", xGoType, alias+"."+t, pkg, alias)
return alias + "." + t, pkg, alias
}

Expand Down

0 comments on commit 837c26c

Please sign in to comment.