structure -> structs renaming

This commit is contained in:
Fatih Arslan 2014-08-13 22:53:15 +03:00
parent 548248a457
commit f3d3802cda
8 changed files with 57 additions and 57 deletions

View File

@ -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 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 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 package based on primitives from the reflect package. Feel free to add new
@ -9,7 +9,7 @@ functions or improve the existing code.
## Install ## Install
```bash ```bash
go get github.com/fatih/structure go get github.com/fatih/structs
``` ```
## Usage and Examples ## Usage and Examples
@ -38,7 +38,7 @@ Let's create a new `Struct` type.
```go ```go
// Create a new struct type: // Create a new struct type:
s := structure.New(server) s := structs.New(server)
// Convert a struct to a map[string]interface{} // Convert a struct to a map[string]interface{}
// => {"Name":"gopher", "ID":123456, "Enabled":true} // => {"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: for a `New()` constructor:
```go ```go
m := structure.Map(s) // Get a map[string]interface{} m := structs.Map(s) // Get a map[string]interface{}
v := structure.Values(s) // Get a []interface{} v := structs.Values(s) // Get a []interface{}
f := structure.Fields(s) // Get a []*Field f := structs.Fields(s) // Get a []*Field
n := structure.Name(s) // Get the struct name n := structs.Name(s) // Get the struct name
h := structure.HasZero(s) // Check if any field is initialized h := structs.HasZero(s) // Check if any field is initialized
z := structure.IsZero(s) // Check if all fields are initialized z := structs.IsZero(s) // Check if all fields are initialized
i := structure.IsStruct(s) // Check if s is a struct or a pointer to struct i := structs.IsStruct(s) // Check if s is a struct or a pointer to struct
``` ```
### Field methods ### Field methods
@ -87,7 +87,7 @@ get and interact with various field methods:
```go ```go
s := structure.New(server) s := structs.New(server)
// Get the Field struct for the "Name" field // Get the Field struct for the "Name" field
name := s.Field("Name") name := s.Field("Name")

View File

@ -1,4 +1,4 @@
package structure package structs
import "reflect" import "reflect"

View File

@ -1,11 +1,11 @@
package structure package structs
import "testing" import "testing"
// A test struct that defines all cases // A test struct that defines all cases
type Foo struct { type Foo struct {
A string A string
B int `structure:"y"` B int `structs:"y"`
C bool `json:"c"` C bool `json:"c"`
d string // not exported d string // not exported
x string `xml:"x"` // not exported, with tag x string `xml:"x"` // not exported, with tag

View File

@ -1,5 +1,5 @@
// Package structure contains various utilities functions to work with structs. // Package structs contains various utilities functions to work with structs.
package structure package structs
import "reflect" import "reflect"
@ -7,11 +7,11 @@ var (
// DefaultTagName is the default tag name for struct fields which provides // DefaultTagName is the default tag name for struct fields which provides
// a more granular to tweak certain structs. Lookup the necessary functions // a more granular to tweak certain structs. Lookup the necessary functions
// for more info. // 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 // Struct encapsulates a struct type to provide several high level functions
// around the structure. // around the struct.
type Struct struct { type Struct struct {
raw interface{} raw interface{}
value reflect.Value 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 // 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 // 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 // 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: // struct's field tag value is the key name. Example:
// //
// // Field appears in map as key "myName". // // 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: // A value with the content of "-" ignores that particular field. Example:
// //
// // Field is ignored by this package. // // 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 // A value with the option of "omitnested" stops iterating further if the type
// is a struct. Example: // is a struct. Example:
// //
// // Field is not processed further by this package. // // Field is not processed further by this package.
// Field time.Time `structure:"myName,omitnested"` // Field time.Time `structs:"myName,omitnested"`
// Field *http.Request `structure:",omitnested"` // Field *http.Request `structs:",omitnested"`
// //
// Note that only exported fields of a struct can be accessed, non exported // Note that only exported fields of a struct can be accessed, non exported
// fields will be neglected. // fields will be neglected.
@ -85,14 +85,14 @@ func (s *Struct) Map() map[string]interface{} {
// Example: // Example:
// //
// // Field is ignored by this package. // // 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 // A value with the option of "omitnested" stops iterating further if the type
// is a struct. Example: // is a struct. Example:
// //
// // Field is not processed further by this package. // // Field is not processed further by this package.
// Field time.Time `structure:"myName,omitnested"` // Field time.Time `structs:"myName,omitnested"`
// Field *http.Request `structure:",omitnested"` // Field *http.Request `structs:",omitnested"`
// //
// Note that only exported fields of a struct can be accessed, non exported // Note that only exported fields of a struct can be accessed, non exported
// fields will be neglected. // fields will be neglected.
@ -124,7 +124,7 @@ func (s *Struct) Values() []interface{} {
// ignores the checking of that particular field. Example: // ignores the checking of that particular field. Example:
// //
// // Field is ignored by this package. // // Field is ignored by this package.
// Field bool `structure:"-"` // Field bool `structs:"-"`
// //
// It panics if s's kind is not struct. // It panics if s's kind is not struct.
func (s *Struct) Fields() []*Field { func (s *Struct) Fields() []*Field {
@ -184,14 +184,14 @@ func (s *Struct) FieldOk(name string) (*Field, bool) {
// that particular field. Example: // that particular field. Example:
// //
// // Field is ignored by this package. // // 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 // A value with the option of "omitnested" stops iterating further if the type
// is a struct. Example: // is a struct. Example:
// //
// // Field is not processed further by this package. // // Field is not processed further by this package.
// Field time.Time `structure:"myName,omitnested"` // Field time.Time `structs:"myName,omitnested"`
// Field *http.Request `structure:",omitnested"` // Field *http.Request `structs:",omitnested"`
// //
// Note that only exported fields of a struct can be accessed, non exported // 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. // 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. Example:
// //
// // Field is ignored by this package. // // 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 // A value with the option of "omitnested" stops iterating further if the type
// is a struct. Example: // is a struct. Example:
// //
// // Field is not processed further by this package. // // Field is not processed further by this package.
// Field time.Time `structure:"myName,omitnested"` // Field time.Time `structs:"myName,omitnested"`
// Field *http.Request `structure:",omitnested"` // Field *http.Request `structs:",omitnested"`
// //
// Note that only exported fields of a struct can be accessed, non exported // 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. // fields will be neglected. It panics if s's kind is not struct.

View File

@ -1,4 +1,4 @@
package structure package structs
import ( import (
"fmt" "fmt"
@ -58,9 +58,9 @@ func ExampleMap() {
func ExampleMap_tags() { func ExampleMap_tags() {
// Custom tags can change the map keys instead of using the fields name // Custom tags can change the map keys instead of using the fields name
type Server struct { type Server struct {
Name string `structure:"server_name"` Name string `structs:"server_name"`
ID int32 `structure:"server_id"` ID int32 `structs:"server_id"`
Enabled bool `structure:"enabled"` Enabled bool `structs:"enabled"`
} }
s := &Server{ s := &Server{
@ -85,9 +85,9 @@ func ExampleMap_nested() {
// By default field with struct types are processed too. We can stop // By default field with struct types are processed too. We can stop
// processing them via "omitnested" tag option. // processing them via "omitnested" tag option.
type Server struct { type Server struct {
Name string `structure:"server_name"` Name string `structs:"server_name"`
ID int32 `structure:"server_id"` ID int32 `structs:"server_id"`
Time time.Time `structure:"time,omitnested"` // do not convert to map[string]interface{} Time time.Time `structs:"time,omitnested"` // do not convert to map[string]interface{}
} }
const shortForm = "2006-Jan-02" const shortForm = "2006-Jan-02"
@ -141,7 +141,7 @@ func ExampleValues_tags() {
Name string Name string
ID int32 ID int32
Enabled bool Enabled bool
Location Location `structure:"-"` // values from location are not included anymore Location Location `structs:"-"` // values from location are not included anymore
} }
s := &Server{ s := &Server{
@ -277,12 +277,12 @@ func ExampleIsZero() {
func ExampleHasZero() { func ExampleHasZero() {
// Let's define an Access struct. Note that the "Enabled" field is not // 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 { type Access struct {
Name string Name string
LastAccessed time.Time LastAccessed time.Time
Number int Number int
Enabled bool `structure:"-"` Enabled bool `structs:"-"`
} }
// Name and Number is not initialized. // Name and Number is not initialized.

View File

@ -1,4 +1,4 @@
package structure package structs
import ( import (
"fmt" "fmt"
@ -85,9 +85,9 @@ func TestMap(t *testing.T) {
func TestMap_Tag(t *testing.T) { func TestMap_Tag(t *testing.T) {
var T = struct { var T = struct {
A string `structure:"x"` A string `structs:"x"`
B int `structure:"y"` B int `structs:"y"`
C bool `structure:"z"` C bool `structs:"z"`
}{ }{
A: "a-value", A: "a-value",
B: 2, B: 2,
@ -152,7 +152,7 @@ func TestMap_OmitNested(t *testing.T) {
type A struct { type A struct {
Name string Name string
Value string Value string
Time time.Time `structure:",omitnested"` Time time.Time `structs:",omitnested"`
} }
a := A{Time: time.Now()} a := A{Time: time.Now()}
@ -292,7 +292,7 @@ func TestValues_OmitNested(t *testing.T) {
} }
type B struct { type B struct {
A A `structure:",omitnested"` A A `structs:",omitnested"`
C int C int
} }
b := &B{A: a, C: 123} b := &B{A: a, C: 123}
@ -423,7 +423,7 @@ func TestFields_OmitNested(t *testing.T) {
type B struct { type B struct {
A A A A
C int C int
Value string `structure:"-"` Value string `structs:"-"`
Number int Number int
} }
b := &B{A: a, C: 123} b := &B{A: a, C: 123}
@ -485,7 +485,7 @@ func TestIsZero(t *testing.T) {
var T = struct { var T = struct {
A string A string
B int B int
C bool `structure:"-"` C bool `structs:"-"`
D []string D []string
}{} }{}
@ -528,7 +528,7 @@ func TestIsZero_OmitNested(t *testing.T) {
a := A{Name: "example"} a := A{Name: "example"}
type B struct { type B struct {
A A `structure:",omitnested"` A A `structs:",omitnested"`
C int C int
} }
b := &B{A: a, C: 123} b := &B{A: a, C: 123}
@ -609,7 +609,7 @@ func TestHasZero(t *testing.T) {
var T = struct { var T = struct {
A string A string
B int B int
C bool `structure:"-"` C bool `structs:"-"`
D []string D []string
}{ }{
A: "a-value", A: "a-value",
@ -655,7 +655,7 @@ func TestHasZero_OmitNested(t *testing.T) {
a := A{Name: "example"} a := A{Name: "example"}
type B struct { type B struct {
A A `structure:",omitnested"` A A `structs:",omitnested"`
C int C int
} }
b := &B{A: a, C: 123} b := &B{A: a, C: 123}

View File

@ -1,4 +1,4 @@
package structure package structs
import "strings" import "strings"

View File

@ -1,4 +1,4 @@
package structure package structs
import "testing" import "testing"