From f3d3802cdaf6f53d52ec64b5b7725872021e967f Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Wed, 13 Aug 2014 22:53:15 +0300 Subject: [PATCH] structure -> structs renaming --- README.md | 24 ++++++------ field.go | 2 +- field_test.go | 4 +- structure.go => structs.go | 38 +++++++++---------- ...example_test.go => structs_example_test.go | 20 +++++----- structure_test.go => structs_test.go | 22 +++++------ tags.go | 2 +- tags_test.go | 2 +- 8 files changed, 57 insertions(+), 57 deletions(-) rename structure.go => structs.go (90%) rename structure_example_test.go => structs_example_test.go (90%) rename structure_test.go => structs_test.go (97%) diff --git a/README.md b/README.md index b5ecbfa..e4f4f61 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# Structure [![GoDoc](https://godoc.org/github.com/fatih/structure?status.svg)](http://godoc.org/github.com/fatih/structure) [![Build Status](https://travis-ci.org/fatih/structure.svg)](https://travis-ci.org/fatih/structure) [![Coverage Status](https://img.shields.io/coveralls/fatih/structure.svg)](https://coveralls.io/r/fatih/structure) +# Structs [![GoDoc](https://godoc.org/github.com/fatih/structs?status.svg)](http://godoc.org/github.com/fatih/structs) [![Build Status](https://travis-ci.org/fatih/structs.svg)](https://travis-ci.org/fatih/structs) [![Coverage Status](https://img.shields.io/coveralls/fatih/structs.svg)](https://coveralls.io/r/fatih/structs) -Structure contains various utilities to work with Go (Golang) structs. It was +Structs contains various utilities to work with Go (Golang) structs. It was initially used by me to convert a struct into a `map[string]interface{}`. With time I've added other utilities for structs. It's basically a high level package based on primitives from the reflect package. Feel free to add new @@ -9,7 +9,7 @@ functions or improve the existing code. ## Install ```bash -go get github.com/fatih/structure +go get github.com/fatih/structs ``` ## Usage and Examples @@ -38,7 +38,7 @@ Let's create a new `Struct` type. ```go // Create a new struct type: -s := structure.New(server) +s := structs.New(server) // Convert a struct to a map[string]interface{} // => {"Name":"gopher", "ID":123456, "Enabled":true} @@ -71,13 +71,13 @@ Most of the struct methods are available as global functions without the need for a `New()` constructor: ```go -m := structure.Map(s) // Get a map[string]interface{} -v := structure.Values(s) // Get a []interface{} -f := structure.Fields(s) // Get a []*Field -n := structure.Name(s) // Get the struct name -h := structure.HasZero(s) // Check if any field is initialized -z := structure.IsZero(s) // Check if all fields are initialized -i := structure.IsStruct(s) // Check if s is a struct or a pointer to struct +m := structs.Map(s) // Get a map[string]interface{} +v := structs.Values(s) // Get a []interface{} +f := structs.Fields(s) // Get a []*Field +n := structs.Name(s) // Get the struct name +h := structs.HasZero(s) // Check if any field is initialized +z := structs.IsZero(s) // Check if all fields are initialized +i := structs.IsStruct(s) // Check if s is a struct or a pointer to struct ``` ### Field methods @@ -87,7 +87,7 @@ get and interact with various field methods: ```go -s := structure.New(server) +s := structs.New(server) // Get the Field struct for the "Name" field name := s.Field("Name") diff --git a/field.go b/field.go index 4022310..f916925 100644 --- a/field.go +++ b/field.go @@ -1,4 +1,4 @@ -package structure +package structs import "reflect" diff --git a/field_test.go b/field_test.go index beb892d..090e8f2 100644 --- a/field_test.go +++ b/field_test.go @@ -1,11 +1,11 @@ -package structure +package structs import "testing" // A test struct that defines all cases type Foo struct { A string - B int `structure:"y"` + B int `structs:"y"` C bool `json:"c"` d string // not exported x string `xml:"x"` // not exported, with tag diff --git a/structure.go b/structs.go similarity index 90% rename from structure.go rename to structs.go index 617a2df..a15f7a0 100644 --- a/structure.go +++ b/structs.go @@ -1,5 +1,5 @@ -// Package structure contains various utilities functions to work with structs. -package structure +// Package structs contains various utilities functions to work with structs. +package structs import "reflect" @@ -7,11 +7,11 @@ var ( // DefaultTagName is the default tag name for struct fields which provides // a more granular to tweak certain structs. Lookup the necessary functions // for more info. - DefaultTagName = "structure" // struct's field default tag name + DefaultTagName = "structs" // struct's field default tag name ) // Struct encapsulates a struct type to provide several high level functions -// around the structure. +// around the struct. type Struct struct { raw interface{} value reflect.Value @@ -29,23 +29,23 @@ func New(s interface{}) *Struct { // Map converts the given struct to a map[string]interface{}, where the keys // of the map are the field names and the values of the map the associated // values of the fields. The default key string is the struct field name but -// can be changed in the struct field's tag value. The "structure" key in the +// can be changed in the struct field's tag value. The "structs" key in the // struct's field tag value is the key name. Example: // // // Field appears in map as key "myName". -// Name string `structure:"myName"` +// Name string `structs:"myName"` // // A value with the content of "-" ignores that particular field. Example: // // // Field is ignored by this package. -// Field bool `structure:"-"` +// Field bool `structs:"-"` // // A value with the option of "omitnested" stops iterating further if the type // is a struct. Example: // // // Field is not processed further by this package. -// Field time.Time `structure:"myName,omitnested"` -// Field *http.Request `structure:",omitnested"` +// Field time.Time `structs:"myName,omitnested"` +// Field *http.Request `structs:",omitnested"` // // Note that only exported fields of a struct can be accessed, non exported // fields will be neglected. @@ -85,14 +85,14 @@ func (s *Struct) Map() map[string]interface{} { // Example: // // // Field is ignored by this package. -// Field int `structure:"-"` +// Field int `structs:"-"` // // A value with the option of "omitnested" stops iterating further if the type // is a struct. Example: // // // Field is not processed further by this package. -// Field time.Time `structure:"myName,omitnested"` -// Field *http.Request `structure:",omitnested"` +// Field time.Time `structs:"myName,omitnested"` +// Field *http.Request `structs:",omitnested"` // // Note that only exported fields of a struct can be accessed, non exported // fields will be neglected. @@ -124,7 +124,7 @@ func (s *Struct) Values() []interface{} { // ignores the checking of that particular field. Example: // // // Field is ignored by this package. -// Field bool `structure:"-"` +// Field bool `structs:"-"` // // It panics if s's kind is not struct. func (s *Struct) Fields() []*Field { @@ -184,14 +184,14 @@ func (s *Struct) FieldOk(name string) (*Field, bool) { // that particular field. Example: // // // Field is ignored by this package. -// Field bool `structure:"-"` +// Field bool `structs:"-"` // // A value with the option of "omitnested" stops iterating further if the type // is a struct. Example: // // // Field is not processed further by this package. -// Field time.Time `structure:"myName,omitnested"` -// Field *http.Request `structure:",omitnested"` +// Field time.Time `structs:"myName,omitnested"` +// Field *http.Request `structs:",omitnested"` // // Note that only exported fields of a struct can be accessed, non exported // fields will be neglected. It panics if s's kind is not struct. @@ -231,14 +231,14 @@ func (s *Struct) IsZero() bool { // field. Example: // // // Field is ignored by this package. -// Field bool `structure:"-"` +// Field bool `structs:"-"` // // A value with the option of "omitnested" stops iterating further if the type // is a struct. Example: // // // Field is not processed further by this package. -// Field time.Time `structure:"myName,omitnested"` -// Field *http.Request `structure:",omitnested"` +// Field time.Time `structs:"myName,omitnested"` +// Field *http.Request `structs:",omitnested"` // // Note that only exported fields of a struct can be accessed, non exported // fields will be neglected. It panics if s's kind is not struct. diff --git a/structure_example_test.go b/structs_example_test.go similarity index 90% rename from structure_example_test.go rename to structs_example_test.go index 18d29cc..45299e8 100644 --- a/structure_example_test.go +++ b/structs_example_test.go @@ -1,4 +1,4 @@ -package structure +package structs import ( "fmt" @@ -58,9 +58,9 @@ func ExampleMap() { func ExampleMap_tags() { // Custom tags can change the map keys instead of using the fields name type Server struct { - Name string `structure:"server_name"` - ID int32 `structure:"server_id"` - Enabled bool `structure:"enabled"` + Name string `structs:"server_name"` + ID int32 `structs:"server_id"` + Enabled bool `structs:"enabled"` } s := &Server{ @@ -85,9 +85,9 @@ func ExampleMap_nested() { // By default field with struct types are processed too. We can stop // processing them via "omitnested" tag option. type Server struct { - Name string `structure:"server_name"` - ID int32 `structure:"server_id"` - Time time.Time `structure:"time,omitnested"` // do not convert to map[string]interface{} + Name string `structs:"server_name"` + ID int32 `structs:"server_id"` + Time time.Time `structs:"time,omitnested"` // do not convert to map[string]interface{} } const shortForm = "2006-Jan-02" @@ -141,7 +141,7 @@ func ExampleValues_tags() { Name string ID int32 Enabled bool - Location Location `structure:"-"` // values from location are not included anymore + Location Location `structs:"-"` // values from location are not included anymore } s := &Server{ @@ -277,12 +277,12 @@ func ExampleIsZero() { func ExampleHasZero() { // Let's define an Access struct. Note that the "Enabled" field is not - // going to be checked because we added the "structure" tag to the field. + // going to be checked because we added the "structs" tag to the field. type Access struct { Name string LastAccessed time.Time Number int - Enabled bool `structure:"-"` + Enabled bool `structs:"-"` } // Name and Number is not initialized. diff --git a/structure_test.go b/structs_test.go similarity index 97% rename from structure_test.go rename to structs_test.go index 342cf71..5d7f04d 100644 --- a/structure_test.go +++ b/structs_test.go @@ -1,4 +1,4 @@ -package structure +package structs import ( "fmt" @@ -85,9 +85,9 @@ func TestMap(t *testing.T) { func TestMap_Tag(t *testing.T) { var T = struct { - A string `structure:"x"` - B int `structure:"y"` - C bool `structure:"z"` + A string `structs:"x"` + B int `structs:"y"` + C bool `structs:"z"` }{ A: "a-value", B: 2, @@ -152,7 +152,7 @@ func TestMap_OmitNested(t *testing.T) { type A struct { Name string Value string - Time time.Time `structure:",omitnested"` + Time time.Time `structs:",omitnested"` } a := A{Time: time.Now()} @@ -292,7 +292,7 @@ func TestValues_OmitNested(t *testing.T) { } type B struct { - A A `structure:",omitnested"` + A A `structs:",omitnested"` C int } b := &B{A: a, C: 123} @@ -423,7 +423,7 @@ func TestFields_OmitNested(t *testing.T) { type B struct { A A C int - Value string `structure:"-"` + Value string `structs:"-"` Number int } b := &B{A: a, C: 123} @@ -485,7 +485,7 @@ func TestIsZero(t *testing.T) { var T = struct { A string B int - C bool `structure:"-"` + C bool `structs:"-"` D []string }{} @@ -528,7 +528,7 @@ func TestIsZero_OmitNested(t *testing.T) { a := A{Name: "example"} type B struct { - A A `structure:",omitnested"` + A A `structs:",omitnested"` C int } b := &B{A: a, C: 123} @@ -609,7 +609,7 @@ func TestHasZero(t *testing.T) { var T = struct { A string B int - C bool `structure:"-"` + C bool `structs:"-"` D []string }{ A: "a-value", @@ -655,7 +655,7 @@ func TestHasZero_OmitNested(t *testing.T) { a := A{Name: "example"} type B struct { - A A `structure:",omitnested"` + A A `structs:",omitnested"` C int } b := &B{A: a, C: 123} diff --git a/tags.go b/tags.go index 103c824..8859341 100644 --- a/tags.go +++ b/tags.go @@ -1,4 +1,4 @@ -package structure +package structs import "strings" diff --git a/tags_test.go b/tags_test.go index 87725b2..5d12724 100644 --- a/tags_test.go +++ b/tags_test.go @@ -1,4 +1,4 @@ -package structure +package structs import "testing"