Skip to content

Commit

Permalink
gofmt for current versions of go and remove stray whitespace
Browse files Browse the repository at this point in the history
gofmt the code for current versions of go:

- adds updated build-tag comments as used by current versions, but keeping
  the old format lines to remain compatible with old go versions.
- format some godoc comments to fix rendeing on pkg.go.dev
- uses the (now preferred) `0o` notation for octal values.

also remove stray whitespace to make more linters happy.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah authored and philhofer committed Dec 31, 2022
1 parent f75b7bc commit 0fc7365
Show file tree
Hide file tree
Showing 47 changed files with 204 additions and 212 deletions.
2 changes: 1 addition & 1 deletion _generated/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ type ArrayConstants struct {
ConstantUint32 [ConstantUint32]string
ConstantUint64 [ConstantUint64]string
ConstantHex [0x16]string
ConstantOctal [07]string
ConstantOctal [0o7]string
}

// Ensure non-msg struct tags work:
Expand Down
14 changes: 7 additions & 7 deletions _generated/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package _generated

import (
"bytes"
"github.com/tinylib/msgp/msgp"
"reflect"
"testing"
"time"

"github.com/tinylib/msgp/msgp"
)

// benchmark encoding a small, "fast" type.
Expand Down Expand Up @@ -76,12 +77,11 @@ func (a *TestType) Equal(b *TestType) bool {
}

// This covers the following cases:
// - Recursive types
// - Non-builtin identifiers (and recursive types)
// - time.Time
// - map[string]string
// - anonymous structs
//
// - Recursive types
// - Non-builtin identifiers (and recursive types)
// - time.Time
// - map[string]string
// - anonymous structs
func Test1EncodeDecode(t *testing.T) {
f := 32.00
tt := &TestType{
Expand Down
11 changes: 0 additions & 11 deletions _generated/omitempty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func mustEncodeToJSON(o msgp.Encodable) string {
}

func TestOmitEmpty0(t *testing.T) {

var s string

var oe0a OmitEmpty0
Expand Down Expand Up @@ -92,12 +91,10 @@ func TestOmitEmpty0(t *testing.T) {
if oe0c.AInt64 != oe0d.AInt64 {
t.Fail()
}

}

// TestOmitEmptyHalfFull tests mixed omitempty and not
func TestOmitEmptyHalfFull(t *testing.T) {

var s string

var oeA OmitEmptyHalfFull
Expand All @@ -124,7 +121,6 @@ func TestOmitEmptyHalfFull(t *testing.T) {

// TestOmitEmptyLotsOFields tests the case of > 64 fields (triggers the bitmask needing to be an array instead of a single value)
func TestOmitEmptyLotsOFields(t *testing.T) {

var s string

var oeLotsA OmitEmptyLotsOFields
Expand All @@ -147,11 +143,9 @@ func TestOmitEmptyLotsOFields(t *testing.T) {
if s != `{"field64":"val64"}` {
t.Errorf("wrong result: %s", s)
}

}

func BenchmarkOmitEmpty10AllEmpty(b *testing.B) {

en := msgp.NewWriter(ioutil.Discard)
var s OmitEmpty10

Expand All @@ -163,11 +157,9 @@ func BenchmarkOmitEmpty10AllEmpty(b *testing.B) {
b.Fatal(err)
}
}

}

func BenchmarkOmitEmpty10AllFull(b *testing.B) {

en := msgp.NewWriter(ioutil.Discard)
var s OmitEmpty10
s.Field00 = "this is the value of field00"
Expand All @@ -189,11 +181,9 @@ func BenchmarkOmitEmpty10AllFull(b *testing.B) {
b.Fatal(err)
}
}

}

func BenchmarkNotOmitEmpty10AllEmpty(b *testing.B) {

en := msgp.NewWriter(ioutil.Discard)
var s NotOmitEmpty10

Expand All @@ -208,7 +198,6 @@ func BenchmarkNotOmitEmpty10AllEmpty(b *testing.B) {
}

func BenchmarkNotOmitEmpty10AllFull(b *testing.B) {

en := msgp.NewWriter(ioutil.Discard)
var s NotOmitEmpty10
s.Field00 = "this is the value of field00"
Expand Down
6 changes: 2 additions & 4 deletions gen/elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ var primitives = map[string]Primitive{
// that satisfy all of the
// interfaces.
var builtins = map[string]struct{}{
"msgp.Raw": struct{}{},
"msgp.Number": struct{}{},
"msgp.Raw": {},
"msgp.Number": {},
}

// common data/methods for every Elem
Expand Down Expand Up @@ -644,7 +644,6 @@ func (s *BaseElem) Resolved() bool {

// ZeroExpr returns the zero/empty expression or empty string if not supported.
func (s *BaseElem) ZeroExpr() string {

switch s.Value {
case Bytes:
return "nil"
Expand Down Expand Up @@ -754,7 +753,6 @@ func writeStructFields(s []StructField, name string) {
// ArrayHeader implementation in this library using uint32. On the Go side, we
// can declare array lengths as any constant integer width, which breaks when
// attempting a direct comparison to an array header's uint32.
//
func coerceArraySize(asz string) string {
return fmt.Sprintf("uint32(%s)", asz)
}
1 change: 0 additions & 1 deletion gen/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func (e *encodeGen) appendraw(bts []byte) {
}

func (e *encodeGen) structmap(s *Struct) {

oeIdentPrefix := randIdent()

var data []byte
Expand Down
1 change: 0 additions & 1 deletion gen/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ func (m *marshalGen) tuple(s *Struct) {
}

func (m *marshalGen) mapstruct(s *Struct) {

oeIdentPrefix := randIdent()

var data []byte
Expand Down
22 changes: 8 additions & 14 deletions gen/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,12 @@ func (p *printer) declare(name string, typ string) {

// does:
//
// if m == nil {
// m = make(type, size)
// } else if len(m) > 0 {
// for key := range m { delete(m, key) }
// }
// if m == nil {
// m = make(type, size)
// } else if len(m) > 0 {
//
// for key := range m { delete(m, key) }
// }
func (p *printer) resizeMap(size string, m *Map) {
vn := m.Varname()
if !p.ok() {
Expand Down Expand Up @@ -368,10 +368,9 @@ func (p *printer) closeblock() { p.print("\n}") }

// does:
//
// for idx := range iter {
// {{generate inner}}
// }
//
// for idx := range iter {
// {{generate inner}}
// }
func (p *printer) rangeBlock(ctx *Context, idx string, iter string, t traversal, inner Elem) {
ctx.PushVar(idx)
p.printf("\n for %s := range %s {", idx, iter)
Expand Down Expand Up @@ -444,7 +443,6 @@ func (b *bmask) typeDecl() string {

// typeName returns the type, e.g. "uint8" or "[2]uint64"
func (b *bmask) typeName() string {

if b.bitlen <= 8 {
return "uint8"
}
Expand All @@ -464,7 +462,6 @@ func (b *bmask) typeName() string {
// readExpr returns the expression to read from a position in the bitmask.
// Compare ==0 for false or !=0 for true.
func (b *bmask) readExpr(bitoffset int) string {

if bitoffset < 0 || bitoffset >= b.bitlen {
panic(fmt.Errorf("bitoffset %d out of range for bitlen %d", bitoffset, b.bitlen))
}
Expand All @@ -481,12 +478,10 @@ func (b *bmask) readExpr(bitoffset int) string {
buf.WriteByte(')')

return buf.String()

}

// setStmt returns the statement to set the specified bit in the bitmask.
func (b *bmask) setStmt(bitoffset int) string {

var buf bytes.Buffer
buf.Grow(len(b.varname) + 16)
buf.WriteString(b.varname)
Expand All @@ -496,5 +491,4 @@ func (b *bmask) setStmt(bitoffset int) string {
fmt.Fprintf(&buf, " |= 0x%X", (uint64(1) << (uint64(bitoffset) % 64)))

return buf.String()

}
1 change: 0 additions & 1 deletion gen/testgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,4 @@ func BenchmarkDecode{{.TypeName}}(b *testing.B) {
}
`))

}
1 change: 0 additions & 1 deletion gen/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func (u *unmarshalGen) gStruct(s *Struct) {
}

func (u *unmarshalGen) tuple(s *Struct) {

// open block
sz := randIdent()
u.p.declare(sz, u32)
Expand Down
3 changes: 1 addition & 2 deletions issue185_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const debugTemp = false
//
// structs are currently processed alphabetically by msgp. this test relies on
// that property.
//
func TestIssue185Idents(t *testing.T) {
var identCases = []struct {
tpl *template.Template
Expand Down Expand Up @@ -225,7 +224,7 @@ func extractVars(file string) (extractedVars, error) {
}

func goGenerateTpl(cwd, tfile string, tpl *template.Template, tplData interface{}) error {
outf, err := os.OpenFile(tfile, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600)
outf, err := os.OpenFile(tfile, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o600)
if err != nil {
return err
}
Expand Down
14 changes: 6 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@
// To use it, include the following directive in a
// go source file with types requiring source generation:
//
// //go:generate msgp
// //go:generate msgp
//
// The go generate tool should set the proper environment variables for
// the generator to execute without any command-line flags. However, the
// following options are supported, if you need them:
//
// -o = output file name (default is {input}_gen.go)
// -file = input file name (or directory; default is $GOFILE, which is set by the `go generate` command)
// -io = satisfy the `msgp.Decodable` and `msgp.Encodable` interfaces (default is true)
// -marshal = satisfy the `msgp.Marshaler` and `msgp.Unmarshaler` interfaces (default is true)
// -tests = generate tests and benchmarks (default is true)
// -o = output file name (default is {input}_gen.go)
// -file = input file name (or directory; default is $GOFILE, which is set by the `go generate` command)
// -io = satisfy the `msgp.Decodable` and `msgp.Encodable` interfaces (default is true)
// -marshal = satisfy the `msgp.Marshaler` and `msgp.Unmarshaler` interfaces (default is true)
// -tests = generate tests and benchmarks (default is true)
//
// For more information, please read README.md, and the wiki at github.com/tinylib/msgp
//
package main

import (
Expand Down Expand Up @@ -98,7 +97,6 @@ func main() {
// Run writes all methods using the associated file or path, e.g.
//
// err := msgp.Run("path/to/myfile.go", gen.Size|gen.Marshal|gen.Unmarshal|gen.Test, false)
//
func Run(gofile string, mode gen.Method, unexported bool) error {
if mode&^gen.Test == 0 {
return nil
Expand Down
1 change: 1 addition & 0 deletions msgp/advise_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux && !appengine && !tinygo
// +build linux,!appengine,!tinygo

package msgp
Expand Down
1 change: 1 addition & 0 deletions msgp/advise_other.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build (!linux && !tinygo) || appengine
// +build !linux,!tinygo appengine

package msgp
Expand Down
15 changes: 9 additions & 6 deletions msgp/defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
// generator implement the Marshaler/Unmarshaler and Encodable/Decodable interfaces.
//
// This package defines four "families" of functions:
// - AppendXxxx() appends an object to a []byte in MessagePack encoding.
// - ReadXxxxBytes() reads an object from a []byte and returns the remaining bytes.
// - (*Writer).WriteXxxx() writes an object to the buffered *Writer type.
// - (*Reader).ReadXxxx() reads an object from a buffered *Reader type.
// - AppendXxxx() appends an object to a []byte in MessagePack encoding.
// - ReadXxxxBytes() reads an object from a []byte and returns the remaining bytes.
// - (*Writer).WriteXxxx() writes an object to the buffered *Writer type.
// - (*Reader).ReadXxxx() reads an object from a buffered *Reader type.
//
// Once a type has satisfied the `Encodable` and `Decodable` interfaces,
// it can be written and read from arbitrary `io.Writer`s and `io.Reader`s using
// msgp.Encode(io.Writer, msgp.Encodable)
//
// msgp.Encode(io.Writer, msgp.Encodable)
//
// and
// msgp.Decode(io.Reader, msgp.Decodable)
//
// msgp.Decode(io.Reader, msgp.Decodable)
//
// There are also methods for converting MessagePack to JSON without
// an explicit de-serialization step.
Expand Down
1 change: 0 additions & 1 deletion msgp/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func TestLocate(t *testing.T) {
if len(field) != 0 {
t.Fatalf("wanted a zero-length returned slice")
}

}

func TestReplace(t *testing.T) {
Expand Down
2 changes: 0 additions & 2 deletions msgp/elsize.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package msgp

func calcBytespec(v byte) bytespec {

// single byte values
switch v {

Expand Down Expand Up @@ -97,7 +96,6 @@ func calcBytespec(v byte) bytespec {
// everything else is covered above

return bytespec{}

}

func getType(v byte) Type {
Expand Down
2 changes: 1 addition & 1 deletion msgp/elsize_default.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !tinygo
// +build !tinygo

package msgp
Expand All @@ -6,7 +7,6 @@ package msgp
// plus type information. gives us
// constant-time type information
// for traversing composite objects.
//
var sizes [256]bytespec

func init() {
Expand Down
6 changes: 0 additions & 6 deletions msgp/elsize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package msgp
import "testing"

func TestBytespec(t *testing.T) {

// verify that bytespec refactor for TinyGo behaves the same as the old code

// previous sizes array setup verbatim:
Expand Down Expand Up @@ -47,7 +46,6 @@ func TestBytespec(t *testing.T) {
mmap32: {size: 5, extra: map32v, typ: MapType},
}

//func init() {
// set up fixed fields

// fixint
Expand Down Expand Up @@ -75,11 +73,9 @@ func TestBytespec(t *testing.T) {
for i := mfixarray; i < 0xa0; i++ {
sizes[i] = bytespec{size: 1, extra: varmode(rfixarray(i)), typ: ArrayType}
}
//}

// compare all values to calcBytespec
for i := 0; i < 256; i++ {

sizeb := sizes[byte(i)]
cb := calcBytespec(byte(i))
if sizeb != cb {
Expand All @@ -90,7 +86,5 @@ func TestBytespec(t *testing.T) {
if i != 0xC1 && sizeb.size == 0 {
t.Errorf("unexpected zero size for index 0x%x", i)
}

}

}
Loading

0 comments on commit 0fc7365

Please sign in to comment.