structure -> structs renaming
This commit is contained in:
parent
548248a457
commit
f3d3802cda
24
README.md
24
README.md
@ -1,6 +1,6 @@
|
||||
# Structure [](http://godoc.org/github.com/fatih/structure) [](https://travis-ci.org/fatih/structure) [](https://coveralls.io/r/fatih/structure)
|
||||
# Structs [](http://godoc.org/github.com/fatih/structs) [](https://travis-ci.org/fatih/structs) [](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")
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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.
|
||||
@ -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.
|
||||
@ -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}
|
||||
@ -1,4 +1,4 @@
|
||||
package structure
|
||||
package structs
|
||||
|
||||
import "testing"
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user