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

fix: staticcheck and govet fixes #406

Merged
merged 3 commits into from
Apr 28, 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
73 changes: 73 additions & 0 deletions .github/workflows/go-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# File managed by web3-bot. DO NOT EDIT.
# See https://github.com/protocol/.github/ for details.

on: [push, pull_request]
name: Go Checks

jobs:
unit:
runs-on: ubuntu-latest
name: All
env:
RUNGOGENERATE: false
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- uses: actions/setup-go@v2
with:
go-version: "1.18.x"
- name: Run repo-specific setup
uses: ./.github/actions/go-check-setup
if: hashFiles('./.github/actions/go-check-setup') != ''
- name: Read config
if: hashFiles('./.github/workflows/go-check-config.json') != ''
run: |
if jq -re .gogenerate ./.github/workflows/go-check-config.json; then
echo "RUNGOGENERATE=true" >> $GITHUB_ENV
fi
- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@d7e217c1ff411395475b2971c0824e1e7cc1af98 # 2022.1 (v0.3.0)
- name: Check that go.mod is tidy
uses: protocol/multiple-go-modules@v1.2
with:
run: |
go mod tidy
if [[ -n $(git ls-files --other --exclude-standard --directory -- go.sum) ]]; then
echo "go.sum was added by go mod tidy"
exit 1
fi
git diff --exit-code -- go.sum go.mod
- name: gofmt
if: ${{ success() || failure() }} # run this step even if the previous one failed
run: |
out=$(gofmt -s -l .)
if [[ -n "$out" ]]; then
echo $out | awk '{print "::error file=" $0 ",line=0,col=0::File is not gofmt-ed."}'
exit 1
fi
- name: go vet
if: ${{ success() || failure() }} # run this step even if the previous one failed
uses: protocol/multiple-go-modules@v1.2
with:
run: go vet ./...
- name: staticcheck
if: ${{ success() || failure() }} # run this step even if the previous one failed
uses: protocol/multiple-go-modules@v1.2
with:
run: |
set -o pipefail
staticcheck ./... | sed -e 's@\(.*\)\.go@./\1.go@g'
- name: go generate
uses: protocol/multiple-go-modules@v1.2
if: (success() || failure()) && env.RUNGOGENERATE == 'true'
with:
run: |
git clean -fd # make sure there aren't untracked files / directories
go generate ./...
# check if go generate modified or added any files
if ! $(git add . && git diff-index HEAD --exit-code --quiet); then
echo "go generated caused changes to the repository:"
git status --short
exit 1
fi
4 changes: 2 additions & 2 deletions codecHelpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/ipld/go-ipld-prime/schema"
)

func Example_Marshal() {
func Example_marshal() {
type Foobar struct {
Foo string
Bar string
Expand All @@ -28,7 +28,7 @@ func Example_Marshal() {

// TODO: Example_Unmarshal, which uses nil and infers a typesystem. However, to match Example_Unmarshal_withSchema, that appears to need more features in bindnode.

func Example_Unmarshal_withSchema() {
func Example_unmarshal_withSchema() {
typesys := schema.MustTypeSystem(
schema.SpawnStruct("Foobar",
[]schema.StructField{
Expand Down
2 changes: 1 addition & 1 deletion fluent/qp/qp.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (f floatParam) Assemble(na datamodel.NodeAssembler) {
}

func Float(f float64) Assemble {
return intParam(f).Assemble
return floatParam(f).Assemble
rvagg marked this conversation as resolved.
Show resolved Hide resolved
}

type stringParam string
Expand Down
2 changes: 1 addition & 1 deletion linking/cid/cidLink.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (lp LinkPrototype) BuildLink(hashsum []byte) datamodel.Link {
p := lp.Prefix

length := p.MhLength
if p.MhType == multihash.ID {
if p.MhType == multihash.IDENTITY {
length = -1
}
if p.Version == 0 && (p.MhType != multihash.SHA2_256 ||
Expand Down
8 changes: 3 additions & 5 deletions node/bindnode/infer.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func inferGoType(typ schema.Type, status map[schema.TypeName]inferredStatus, lev

// from IPLD Schema field names like "foo" to Go field names like "Foo".
func fieldNameFromSchema(name string) string {
fieldName := strings.Title(name)
fieldName := strings.Title(name) //lint:ignore SA1019 cases.Title doesn't work for this
if !token.IsIdentifier(fieldName) {
panic(fmt.Sprintf("bindnode: inferred field name %q is not a valid Go identifier", fieldName))
}
Expand Down Expand Up @@ -418,10 +418,8 @@ func inferSchema(typ reflect.Type, level int) schema.Type {
return schemaTypeBytes
}

etyp := typ.Elem()
nullable := false
if etyp.Kind() == reflect.Ptr {
etyp = etyp.Elem()
if typ.Elem().Kind() == reflect.Ptr {
nullable = true
}
etypSchema := inferSchema(typ.Elem(), level+1)
Expand All @@ -433,7 +431,7 @@ func inferSchema(typ reflect.Type, level int) schema.Type {
defaultTypeSystem.Accumulate(typSchema)
return typSchema
}
panic(fmt.Sprintf("%s", typ.Kind()))
panic(typ.Kind().String())
}

// There are currently 27 reflect.Kind iota values,
Expand Down
4 changes: 0 additions & 4 deletions node/bindnode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,6 @@ type _mapAssembler struct {
// TODO: more state checks

curKey _assembler

nextIndex int // only used by repr.go
rvagg marked this conversation as resolved.
Show resolved Hide resolved
}

func (w *_mapAssembler) AssembleKey() datamodel.NodeAssembler {
Expand Down Expand Up @@ -1000,8 +998,6 @@ type _unionAssembler struct {
// TODO: more state checks

curKey _assembler

nextIndex int // only used by repr.go
}

func (w *_unionAssembler) AssembleKey() datamodel.NodeAssembler {
Expand Down
2 changes: 1 addition & 1 deletion node/tests/schemaLinks.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
var store = memstore.Store{}

func encode(n datamodel.Node) (datamodel.Node, datamodel.Link) {
lp := cidlink.LinkPrototype{cid.Prefix{
lp := cidlink.LinkPrototype{Prefix: cid.Prefix{
Version: 1,
Codec: 0x0129,
MhType: 0x13,
Expand Down
35 changes: 3 additions & 32 deletions node/tests/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,9 @@ import (
// they'd probably be much less fragile and give better error messages that way.
// On the other hand, the functions for condensing two-arg returns wouldn't go away anyway.

func plz(n datamodel.Node, e error) datamodel.Node {
if e != nil {
panic(e)
}
return n
}

func plzStr(n datamodel.Node, e error) string {
if e != nil {
panic(e)
}
if s, ok := n.AsString(); ok == nil {
return s
} else {
panic(ok)
}
}

func str(n datamodel.Node) string {
if s, ok := n.AsString(); ok == nil {
return s
} else {
panic(ok)
}
}

func erp(n datamodel.Node, e error) interface{} {
if e != nil {
return e
}
return n
}
// various benchmarks assign their final result here,
// in order to defuse the possibility of their work being elided.
var sink interface{} //lint:ignore U1000 used by benchmarks

// purely to syntactically flip large inline closures so we can see the argument at the top rather than the bottom of the block.
func withNode(n datamodel.Node, cb func(n datamodel.Node)) {
Expand Down
4 changes: 0 additions & 4 deletions node/tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
"github.com/ipld/go-ipld-prime/traversal/selector"
)

// various benchmarks assign their final result here,
// in order to defuse the possibility of their work being elided.
var sink interface{}

func mustNodeFromJsonString(np datamodel.NodePrototype, str string) datamodel.Node {
nb := np.NewBuilder()
must.NotError(json.Decode(nb, strings.NewReader(str)))
Expand Down
2 changes: 2 additions & 0 deletions printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,15 @@ func (cfg Config) oneline(typ schema.Type, isInKey bool) bool {
return isInKey // Future: this could become customizable, with some kind of Always|OnlyInKeys|Never option enum per type.
}

/* TODO: not implemented or used
// useRepr decides if a value should be printed using its representation.
// Sometimes configuring this to be true for structs or unions with stringy representations
// will cause map printouts using them as keys to become drastically more readable
// (if with some loss of informativeness, or at least loss of explicitness).
func (cfg Config) useRepr(typ schema.Type, isInKey bool) bool {
return false
}
*/

// useCmplxKeys decides if a map should print itself using a multi-line and extra-indented style for keys.
func (cfg Config) useCmplxKeys(mapn datamodel.Node) bool {
Expand Down
4 changes: 2 additions & 2 deletions schema/gen/go/adjunctCfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (cfg *AdjunctCfg) FieldSymbolUpper(f schema.StructField) string {
if x, ok := cfg.fieldSymbolUpperOverrides[FieldTuple{f.Type().Name(), f.Name()}]; ok {
return x
}
return strings.Title(f.Name())
return strings.Title(f.Name()) //lint:ignore SA1019 cases.Title doesn't work for this
}

// Comments returns a bool for whether comments should be included in gen output or not.
Expand Down Expand Up @@ -171,7 +171,7 @@ func sizeOfSchemaType(t schema.Type) uintptr {
// and validated more aggressively up front when adjcfg is loaded.
func (cfg *AdjunctCfg) UnionMemlayout(t schema.Type) string {
if t.TypeKind() != schema.TypeKind_Union {
panic(fmt.Errorf("%s is not a union!", t.Name()))
panic(fmt.Errorf("%s is not a union", t.Name()))
}
v, ok := cfg.CfgUnionMemlayout[t.Name()]
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion schema/gen/go/genBoolReprBool.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (g boolReprBoolReprGenerator) EmitNodePrototypeType(w io.Writer) {
`, w, g.AdjCfg, g)
}
func (g boolReprBoolReprGenerator) GetNodeBuilderGenerator() NodeBuilderGenerator {
return boolReprBoolReprBuilderGenerator{g.AdjCfg, g.Type}
rvagg marked this conversation as resolved.
Show resolved Hide resolved
return boolReprBoolReprBuilderGenerator(g)
}

type boolReprBoolReprBuilderGenerator struct {
Expand Down
2 changes: 1 addition & 1 deletion schema/gen/go/genBytesReprBytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (g bytesReprBytesReprGenerator) EmitNodePrototypeType(w io.Writer) {
`, w, g.AdjCfg, g)
}
func (g bytesReprBytesReprGenerator) GetNodeBuilderGenerator() NodeBuilderGenerator {
return bytesReprBytesReprBuilderGenerator{g.AdjCfg, g.Type}
return bytesReprBytesReprBuilderGenerator(g)
}

type bytesReprBytesReprBuilderGenerator struct {
Expand Down
2 changes: 1 addition & 1 deletion schema/gen/go/genFloatReprFloat.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (g float64ReprFloatReprGenerator) EmitNodePrototypeType(w io.Writer) {
`, w, g.AdjCfg, g)
}
func (g float64ReprFloatReprGenerator) GetNodeBuilderGenerator() NodeBuilderGenerator {
return float64ReprFloatReprBuilderGenerator{g.AdjCfg, g.Type}
return float64ReprFloatReprBuilderGenerator(g)
}

type float64ReprFloatReprBuilderGenerator struct {
Expand Down
2 changes: 1 addition & 1 deletion schema/gen/go/genIntReprInt.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (g intReprIntReprGenerator) EmitNodePrototypeType(w io.Writer) {
`, w, g.AdjCfg, g)
}
func (g intReprIntReprGenerator) GetNodeBuilderGenerator() NodeBuilderGenerator {
return intReprIntReprBuilderGenerator{g.AdjCfg, g.Type}
return intReprIntReprBuilderGenerator(g)
}

type intReprIntReprBuilderGenerator struct {
Expand Down
2 changes: 1 addition & 1 deletion schema/gen/go/genLinkReprLink.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (g linkReprLinkReprGenerator) EmitNodePrototypeType(w io.Writer) {
`, w, g.AdjCfg, g)
}
func (g linkReprLinkReprGenerator) GetNodeBuilderGenerator() NodeBuilderGenerator {
return linkReprLinkReprBuilderGenerator{g.AdjCfg, g.Type}
return linkReprLinkReprBuilderGenerator(g)
}

type linkReprLinkReprBuilderGenerator struct {
Expand Down
2 changes: 1 addition & 1 deletion schema/gen/go/genStringReprString.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (g stringReprStringReprGenerator) EmitNodePrototypeType(w io.Writer) {
`, w, g.AdjCfg, g)
}
func (g stringReprStringReprGenerator) GetNodeBuilderGenerator() NodeBuilderGenerator {
return stringReprStringReprBuilderGenerator{g.AdjCfg, g.Type}
return stringReprStringReprBuilderGenerator(g)
}

type stringReprStringReprBuilderGenerator struct {
Expand Down
6 changes: 1 addition & 5 deletions schema/gen/go/genUnionReprKinded.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,7 @@ func (g unionKindedReprGenerator) EmitNodePrototypeType(w io.Writer) {
// --- NodeBuilder and NodeAssembler --->

func (g unionKindedReprGenerator) GetNodeBuilderGenerator() NodeBuilderGenerator {
return unionKindedReprBuilderGenerator{
g.AdjCfg,
g.PkgName,
g.Type,
}
return unionKindedReprBuilderGenerator(g)
}

type unionKindedReprBuilderGenerator struct {
Expand Down
2 changes: 1 addition & 1 deletion schema/gen/go/mixins/templateUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func doTemplate(tmplstr string, w io.Writer, data interface{}) {
tmpl := template.Must(template.New("").
Funcs(template.FuncMap{
"title": func(s string) string { return strings.Title(s) },
"title": func(s string) string { return strings.Title(s) }, //lint:ignore SA1019 cases.Title doesn't work for this
}).
Parse(testutil.Dedent(tmplstr)))
if err := tmpl.Execute(w, data); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion schema/gen/go/templateUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func doTemplate(tmplstr string, w io.Writer, adjCfg *AdjunctCfg, data interface{
}
},
"add": func(a, b int) int { return a + b },
"title": func(s string) string { return strings.Title(s) },
"title": func(s string) string { return strings.Title(s) }, //lint:ignore SA1019 cases.Title doesn't work for this
}).
Parse(testutil.Dedent(tmplstr)))
if err := tmpl.Execute(w, data); err != nil {
Expand Down
46 changes: 0 additions & 46 deletions schema/gen/go/testutil_test.go

This file was deleted.

Loading