Skip to content
This repository was archived by the owner on May 8, 2019. It is now read-only.

Commit c26bd37

Browse files
committed
Documentation and test improvements
1 parent 6a49464 commit c26bd37

File tree

6 files changed

+37
-29
lines changed

6 files changed

+37
-29
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# NOTE: This Makefile is only necessary if you would
2-
# like to help develop the msgp tool or library.
1+
# This Makefile is necessary only if you want to help
2+
# develop the msgp tool or library.
33
# You can still install msgp with `go get` or `go install`.
44

55
GGEN = ./tests/def_gen.go ./tests/def_gen_test.go

gen/run.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// This package is the tool msgp uses to generate Go code for the types in your program that you
2-
// want to serialize to and from the MessagePack format. The package is designed to be usable by
1+
// Package gen is the tool msgp uses to generate Go code for the types in your program that you
2+
// want to serialize to and from the MessagePack format. This package is designed to be usable by
33
// both the main.go file at the root of this repository (installed as a command line tool that is
44
// called by the `go generate` command) and by external programs that import the package.
55
//
@@ -11,7 +11,7 @@
1111
//
1212
// import "github.com/dchenk/msgp/gen"
1313
//
14-
// err := gen.Run("path/to/myfile.go", gen.Size|gen.Marshal|gen.Unmarshal|gen.Test, false)
14+
// err := gen.Run("path/to/my_file.go", gen.Size|gen.Marshal|gen.Unmarshal|gen.Test, false)
1515
//
1616
package gen
1717

@@ -90,7 +90,7 @@ func RunData(srcPath string, mode Method, unexported bool) (mainBuf *bytes.Buffe
9090
}
9191

9292
fmt.Println(chalk.Magenta.Color("======= MessagePack Code Generating ======="))
93-
fmt.Printf(chalk.Magenta.Color(">>> Input: %q\n"), srcPath)
93+
fmt.Printf(chalk.Magenta.Color(" Input: %s\n"), srcPath)
9494

9595
mainBuf = bytes.NewBuffer(make([]byte, 0, 4096))
9696
writePkgHeader(mainBuf, s.pkg)
@@ -101,7 +101,7 @@ func RunData(srcPath string, mode Method, unexported bool) (mainBuf *bytes.Buffe
101101
// If the import has an alias, include it (imp.Path.Value is a quoted string).
102102
// But do not include the import if its alias is the blank identifier.
103103
if imp.Name.Name == "_" {
104-
fmt.Printf(chalk.Blue.Color("Not including import %s with blank identifier as alias\n"), imp.Path.Value)
104+
fmt.Printf(chalk.Blue.Color("Not including import %s with blank identifier as alias.\n"), imp.Path.Value)
105105
} else {
106106
mainImports = append(mainImports, imp.Name.Name+" "+imp.Path.Value)
107107
}
@@ -123,7 +123,7 @@ func RunData(srcPath string, mode Method, unexported bool) (mainBuf *bytes.Buffe
123123

124124
writeImportHeader(mainBuf, mainImports)
125125

126-
// Write the test file if it's needed.
126+
// Write the test file if it's desired.
127127
if mode&Test == Test {
128128
testsBuf = bytes.NewBuffer(make([]byte, 0, 4096))
129129
writePkgHeader(testsBuf, s.pkg)
@@ -147,7 +147,7 @@ func formatWrite(fileName string, data []byte) error {
147147
if err != nil {
148148
return err
149149
}
150-
fmt.Printf(chalk.Magenta.Color(">>> Writing file \"%s\"\n"), fileName)
150+
fmt.Printf(chalk.Magenta.Color(" Writing file: %s\n"), fileName)
151151
return ioutil.WriteFile(fileName, out, 0600)
152152
}
153153

msgp/defs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// Package msgp is the support library for the msgp code generator (http://github.com/dchenk/msgp).
1+
// Package msgp is the runtime support library for the msgp code generator (http://github.com/dchenk/msgp).
22
//
33
// This package defines the utilities used by the msgp code generator for encoding and decoding MessagePack
4-
// from []byte and io.Reader/io.Writer types. Much of this package is devoted to helping the msgp code
5-
// generator implement the Marshaler/Unmarshaler and Encoder/Decoder interfaces to avoid runtime reflection.
4+
// from []byte and io.Reader/io.Writer types. Most things here are intended to be used only in programs that
5+
// use the msgp code generator, the point being to avoid runtime reflection.
66
//
77
// This package defines four families of functions:
88
// - AppendXxxx() appends an object to a []byte in MessagePack encoding.

msgp/defs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package msgp_test
22

33
//go:generate msgp -o=defs_gen_test.go -tests=false
44

5-
type Blobs []Blob
5+
type Blobs []Blob // needed separately for Msgsize()
66

77
type Blob struct {
88
Name string `msgp:"name"`

msgp/extension.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const (
1616
TimeExtension = 5
1717
)
1818

19-
// our extensions live here
19+
// extensionReg contains registered extensions.
2020
var extensionReg = make(map[int8]func() Extension)
2121

2222
// RegisterExtension registers extensions so that they can be initialized and returned

msgp/file_test.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build linux darwin dragonfly freebsd netbsd openbsd
2-
31
package msgp_test
42

53
import (
@@ -32,32 +30,40 @@ func TestReadWriteFile(t *testing.T) {
3230

3331
t.Parallel()
3432

35-
f, err := os.Create("tmpfile")
33+
fname := "tmpfile"
34+
f, err := os.Create(fname)
3635
if err != nil {
3736
t.Fatal(err)
3837
}
3938
defer func() {
4039
f.Close()
41-
os.Remove("tmpfile")
40+
os.Remove(fname)
4241
}()
4342

4443
data := make([]byte, 1024*1024)
45-
rand.Read(data)
44+
if _, err = rand.Read(data); err != nil {
45+
t.Fatalf("rand reader: %v", err)
46+
}
4647

47-
err = msgp.WriteFile(rawBytes(data), f)
48+
if err = msgp.WriteFile(rawBytes(data), f); err != nil {
49+
t.Fatalf("writing file: %v", err)
50+
}
51+
if err = f.Close(); err != nil {
52+
t.Fatalf("could not close file; %v", err)
53+
}
54+
55+
f, err = os.Open(fname)
4856
if err != nil {
49-
t.Fatal(err)
57+
t.Fatalf("could not open written file; %v", err)
5058
}
5159

5260
var out rawBytes
53-
f.Seek(0, os.SEEK_SET) // TODO: SEEK_SET is deprecated
54-
err = msgp.ReadFile(&out, f)
55-
if err != nil {
56-
t.Fatal(err)
61+
if err = msgp.ReadFile(&out, f); err != nil {
62+
t.Fatalf("reading file: %v", err)
5763
}
5864

5965
if !bytes.Equal([]byte(out), data) {
60-
t.Fatal("Input and output not equal.")
66+
t.Fatal("Input not equal to output.")
6167
}
6268

6369
}
@@ -79,10 +85,10 @@ func BenchmarkWriteReadFile(b *testing.B) {
7985
if err != nil {
8086
b.Fatal(err)
8187
}
82-
defer func(f *os.File, name string) {
88+
defer func() {
8389
f.Close()
84-
os.Remove(name)
85-
}(f, fname)
90+
os.Remove(fname)
91+
}()
8692

8793
data := make(Blobs, b.N)
8894

@@ -95,6 +101,7 @@ func BenchmarkWriteReadFile(b *testing.B) {
95101

96102
b.SetBytes(int64(data.Msgsize() / b.N))
97103
b.ResetTimer()
104+
98105
err = msgp.WriteFile(data, f)
99106
if err != nil {
100107
b.Fatal(err)
@@ -103,4 +110,5 @@ func BenchmarkWriteReadFile(b *testing.B) {
103110
if err != nil {
104111
b.Fatal(err)
105112
}
113+
106114
}

0 commit comments

Comments
 (0)