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

Commit

Permalink
Renamed struct tag msg to msgp to be consistent with package name
Browse files Browse the repository at this point in the history
  • Loading branch information
dchenk committed Mar 11, 2018
1 parent 6dbf2b1 commit 6a49464
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 112 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ Field names can be set in much the same way as with the `encoding/json` package.

```go
type Person struct {
Name string `msg:"name"`
Address string `msg:"address"`
Age int `msg:"age"`
Hidden string `msg:"-"` // this field is ignored
Name string `msgp:"name"`
Address string `msgp:"address"`
Age int `msgp:"age"`
Hidden string `msgp:"-"` // this field is ignored
unexported bool // this field is also ignored
}
```
Expand Down Expand Up @@ -63,9 +63,9 @@ type MyInt int
type Data []byte

type Struct struct {
Which map[string]*MyInt `msg:"which"`
Other Data `msg:"other"`
Nums [Eight]float64 `msg:"nums"`
Which map[string]*MyInt `msgp:"which"`
Other Data `msgp:"other"`
Nums [Eight]float64 `msgp:"nums"`
}
```
As long as the declarations of `MyInt` and `Data` are in the same file as `Struct`, the parser will determine that the type information for `MyInt` and `Data` can be passed into the definition of `Struct` before its methods are generated.
Expand Down
8 changes: 4 additions & 4 deletions gen/elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func randIdent() string {
// Consider the following:
//
// type Marshaler struct {
// Thing1 *float64 `msg:"thing1"`
// Body []byte `msg:"body"`
// Thing1 *float64 `msgp:"thing1"`
// Body []byte `msgp:"body"`
// }
//
// A parser should parse the above into:
Expand Down Expand Up @@ -438,8 +438,8 @@ func (s *Struct) Complexity() int {
}

type structField struct {
fieldTag string // the string inside the `msg:""` tag
rawTag string // the full tag (in case there are non-msg keys)
fieldTag string // the string inside the `msgp:""` tag
rawTag string // the full tag (in case there are non-msgp keys)
fieldName string // the name of the struct field
fieldElem Elem // the field type
}
Expand Down
45 changes: 19 additions & 26 deletions gen/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
"strings"
)

// A source is the in-memory representation of a either a single parsed source code
// file or a concatenation of source code files.
// A source represents either a single parsed source code file or a concatenation of files.
type source struct {
pkg string // package name
specs map[string]ast.Expr // type specs found in the code
Expand All @@ -23,7 +22,7 @@ type source struct {

// newSource parses a file at the path provided and produces a new *source.
// If srcPath is the path to a directory, the entire directory will be parsed.
// If unexported is false, only exported identifiers are included in the source.
// If unexported is true, the unexported identifiers in source will be included.
// If the resulting source would be empty, an error is returned.
func newSource(srcPath string, unexported bool) (*source, error) {

Expand All @@ -45,9 +44,9 @@ func newSource(srcPath string, unexported bool) (*source, error) {
return nil, err
}
if len(pkgs) != 1 {
return nil, fmt.Errorf("multiple packages in directory: %s", srcPath)
return nil, fmt.Errorf("multiple packages in directory %s", srcPath)
}
// Extract the Package from the pkgs map.
// Extract the package from the pkgs map.
var pkg *ast.Package
for n := range pkgs {
s.pkg = n
Expand Down Expand Up @@ -131,13 +130,11 @@ func (s *source) applyDirectives() {

// A linkset is a graph of unresolved identities.
//
// Since Ident can only represent one level of type
// indirection (e.g. Foo -> uint8), type declarations like `type Foo Bar`
// aren't resolve-able until we've processed everything else.
// Since Ident can only represent one level of type indirection (e.g. Foo -> uint8), type
// declarations like `type Foo Bar` aren't resolve-able until we've processed everything else.
//
// The goal of this dependency resolution is to distill the type
// declaration into just one level of indirection.
// In other words, if we have:
// The goal of this dependency resolution is to distill the type declaration into just one
// level of indirection. So if we have:
//
// type A uint64
// type B A
Expand Down Expand Up @@ -174,8 +171,7 @@ func (s *source) resolve(ls linkset) {

}

// process takes the contents of f.Specs and
// uses them to populate f.Identities
// process takes the contents of f.Specs and uses them to populate f.Identities
func (s *source) process() {

deferred := make(linkset)
Expand Down Expand Up @@ -225,11 +221,8 @@ func strToMethod(s string) Method {
}
}

// applyDirs applies directives of the form: //msgp:encode ignore {{TypeName}}
func (s *source) applyDirs(p generatorSet) {
// apply directives of the form
//
// //msgp:encode ignore {{TypeName}}
//
loop:
for _, d := range s.directives {
chunks := strings.Split(d, " ")
Expand Down Expand Up @@ -324,14 +317,14 @@ func (s *source) getField(f *ast.Field) []structField {

fields := make([]structField, 1)
var extension bool
// parse tag; otherwise field name is field tag
// Parse the tag; otherwise the field name is field tag.
if f.Tag != nil {
body := reflect.StructTag(strings.Trim(f.Tag.Value, "`")).Get("msg")
body := reflect.StructTag(strings.Trim(f.Tag.Value, "`")).Get("msgp")
tags := strings.Split(body, ",")
if len(tags) == 2 && tags[1] == "extension" {
extension = true
}
// ignore "-" fields
// Ignore "-" fields.
if tags[0] == "-" {
return nil
}
Expand All @@ -344,7 +337,7 @@ func (s *source) getField(f *ast.Field) []structField {
return nil
}

// parse field name
// Parse the field name.
switch len(f.Names) {
case 0:
fields[0].fieldName = embedded(f.Type)
Expand Down Expand Up @@ -412,7 +405,7 @@ func embedded(f ast.Expr) string {
}
}

// stringify a field type name
// stringify a field type name.
func stringify(e ast.Expr) string {
switch e := e.(type) {
case *ast.Ident:
Expand Down Expand Up @@ -468,7 +461,7 @@ func (s *source) parseExpr(e ast.Expr) Elem {

case *ast.ArrayType:

// special case for []byte
// Special case for []byte
if e.Len == nil {
if i, ok := e.Elt.(*ast.Ident); ok && i.Name == "byte" {
return &BaseElem{Value: Bytes}
Expand All @@ -481,7 +474,7 @@ func (s *source) parseExpr(e ast.Expr) Elem {
return nil
}

// array and not a slice
// Check if array and not a slice
if e.Len != nil {
switch lt := e.Len.(type) {
case *ast.BasicLit:
Expand Down Expand Up @@ -521,13 +514,13 @@ func (s *source) parseExpr(e ast.Expr) Elem {
return Ident(stringify(e))

case *ast.InterfaceType:
// support `interface{}`
// Support `interface{}`
if len(e.Methods.List) == 0 {
return &BaseElem{Value: Intf}
}
return nil

default: // other types not supported
default: // Other types are not supported.
return nil
}
}
36 changes: 18 additions & 18 deletions gen/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,24 @@ const (
marshaltest = Marshal | Unmarshal | Test // tests for Marshaler and Unmarshaler
)

func strtoMeth(s string) Method {
switch s {
case "encode":
return Encode
case "decode":
return Decode
case "marshal":
return Marshal
case "unmarshal":
return Unmarshal
case "size":
return Size
case "test":
return Test
default:
return 0
}
}
//func strtoMeth(s string) Method {
// switch s {
// case "encode":
// return Encode
// case "decode":
// return Decode
// case "marshal":
// return Marshal
// case "unmarshal":
// return Unmarshal
// case "size":
// return Size
// case "test":
// return Test
// default:
// return 0
// }
//}

// A generator has all the methods needed to generate code.
type generator interface {
Expand Down
8 changes: 4 additions & 4 deletions msgp/defs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package msgp_test
type Blobs []Blob

type Blob struct {
Name string `msg:"name"`
Float float64 `msg:"float"`
Bytes []byte `msg:"bytes"`
Amount int64 `msg:"amount"`
Name string `msgp:"name"`
Float float64 `msgp:"float"`
Bytes []byte `msgp:"bytes"`
Amount int64 `msgp:"amount"`
}
16 changes: 5 additions & 11 deletions msgp/elsize.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package msgp

// size of every object on the wire,
// plus type information. gives us
// constant-time type information
// for traversing composite objects.
//
// sizes gives the size of every object on the wire and type information. This gives us
// constant-time type information for traversing composite objects.
var sizes = [256]bytespec{
mnil: {size: 1, extra: constsize, typ: NilType},
mfalse: {size: 1, extra: constsize, typ: BoolType},
Expand Down Expand Up @@ -40,7 +37,6 @@ var sizes = [256]bytespec{
}

func init() {
// set up fixed fields

// fixint
for i := mfixint; i < 0x80; i++ {
Expand All @@ -52,8 +48,7 @@ func init() {
sizes[uint8(i)] = bytespec{size: 1, extra: constsize, typ: IntType}
}

// fixstr gets constsize,
// since the prefix yields the size
// fixstr gets constsize since the prefix yields the size
for i := mfixstr; i < 0xc0; i++ {
sizes[i] = bytespec{size: 1 + rfixstr(i), extra: constsize, typ: StrType}
}
Expand All @@ -67,11 +62,10 @@ func init() {
for i := mfixarray; i < 0xa0; i++ {
sizes[i] = bytespec{size: 1, extra: varmode(rfixarray(i)), typ: ArrayType}
}

}

// a valid bytespsec has
// non-zero 'size' and
// non-zero 'typ'
// A valid bytespsec has non-zero size and non-zero type.
type bytespec struct {
size uint8 // prefix size information
extra varmode // extra size information
Expand Down
23 changes: 9 additions & 14 deletions msgp/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,18 @@ const (
// our extensions live here
var extensionReg = make(map[int8]func() Extension)

// RegisterExtension registers extensions so that they
// can be initialized and returned by methods that
// decode `interface{}` values. This should only
// be called during initialization. f() should return
// a newly-initialized zero value of the extension. Keep in
// mind that extensions 3, 4, and 5 are reserved for
// complex64, complex128, and time.Time, respectively,
// and that MessagePack reserves extension types from -127 to -1.
// RegisterExtension registers extensions so that they can be initialized and returned
// by methods that decode `interface{}` values. This should only be called during
// initialization. Func f should return a newly-initialized zero value of the extension.
// Keep in mind that extensions 3, 4, and 5 are reserved for complex64, complex128, and
// time.Time, respectively, and that MessagePack reserves extension types from -127 to -1.
//
// For example, if you wanted to register a user-defined struct:
//
// msgp.RegisterExtension(10, func() msgp.Extension { &MyExtension{} })
//
// RegisterExtension will panic if you call it multiple times
// with the same 'typ' argument, or if you use a reserved
// type (3, 4, or 5).
// RegisterExtension will panic if you call it multiple times with the same 'typ' argument
// or if you use a reserved type (3, 4, or 5).
func RegisterExtension(typ int8, f func() Extension) {
switch typ {
case Complex64Extension, Complex128Extension, TimeExtension:
Expand All @@ -46,9 +42,8 @@ func RegisterExtension(typ int8, f func() Extension) {
extensionReg[typ] = f
}

// ExtensionTypeError is an error type returned
// when there is a mis-match between an extension type
// and the type encoded on the wire
// ExtensionTypeError is an error type returned when there is a mis-match between an extension
// type and the type encoded on the wire
type ExtensionTypeError struct {
Got int8
Want int8
Expand Down
Loading

0 comments on commit 6a49464

Please sign in to comment.