https://github.com/things-go/structs
Go library for encoding native Go structures into generic map values. go struct to map[string]interface{}
https://github.com/things-go/structs
Last synced: 5 months ago
JSON representation
Go library for encoding native Go structures into generic map values. go struct to map[string]interface{}
- Host: GitHub
- URL: https://github.com/things-go/structs
- Owner: things-go
- License: mit
- Created: 2021-11-03T00:56:05.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-06-13T10:24:04.000Z (about 2 years ago)
- Last Synced: 2024-06-21T13:21:31.037Z (almost 2 years ago)
- Language: Go
- Homepage:
- Size: 83 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# structs
Go library for encoding native Go structures into generic map values.
[](https://godoc.org/github.com/things-go/structs)
[](https://pkg.go.dev/github.com/things-go/structs?tab=doc)
[](https://github.com/things-go/structs/actions/workflows/ci.yml)
[](https://goreportcard.com/report/github.com/things-go/structs)
[](https://raw.githubusercontent.com/things-go/structs/main/LICENSE)
[](https://github.com/things-go/structs/tags)
### Installation
Use go get.
```bash
go get -u github.com/things-go/structs
```
Then import the structs package into your own code.
```go
import "github.com/things-go/structs"
```
### Usage && Example
#### API
Just like the standard lib strings, bytes and co packages, structs has many global functions to manipulate or organize your struct data. Lets define and declare a struct:
```go
type Server struct {
Name string `json:"name,omitempty"`
ID int
Enabled bool
users []string // not exported
http.Server // embedded
}
server := &Server{
Name: "gopher",
ID: 123456,
Enabled: true,
}
```
Here is an example:
```go
// Convert a struct to a map[string]interface{}
// => {"Name":"gopher", "ID":123456, "Enabled":true}
m := structs.Map(server)
// Convert the values of a struct to a []interface{}
// => ["gopher", 123456, true]
v := structs.Values(server)
// Convert the names of a struct to a []string
// (see "Names methods" for more info about fields)
n := structs.Names(server)
// Convert the values of a struct to a []*Field
// (see "Field methods" for more info about fields)
f := structs.Fields(server)
// Return the struct name => "Server"
n := structs.Name(server)
// Check if any field of a struct is initialized or not.
h := structs.HasZero(server)
// Check if all fields of a struct is initialized or not.
z := structs.IsZero(server)
// Check if server is a struct or a pointer to struct
i := structs.IsStruct(server)
```
Only [public fields](https://golang.org/doc/effective_go.html#names) will be processed. So **fields
starting with lowercase will be ignored**.
#### Name Tags
```go
type AA struct {
Id int64 `map:"id"`
Name string `map:"name"`
}
```
We can give the field a tag to specify another name to be used as the key.
#### Ignore Field
```go
type AA struct {
Ignore string `map:"-"`
}
```
If we give the special tag "-" to a field, it will be ignored.
#### Omit Empty
```go
type AA struct {
Desc string `map:"desc,omitempty"`
}
```
If tag option is "omitempty", this field will not appear in the map if the value is empty.
Empty values are 0, false, "", nil, empty array and empty map.
#### Omit Nested
```go
type AA struct {
// Field is not processed further by this package.
Field *http.Request `map:",omitnested"`
}
```
A value with the option of "omitnested" stops iterating further if the type
is a struct. it do not converted value if a value are no exported fields, ie: time.Time
#### To String
```go
type AA struct {
Id int64 `map:"id,string"`
Price float32 `map:"price,string"`
}
```
If tag option is "string", this field will be converted to string type. Encode will put the
original value to the map if the conversion is failed.
## References
- [mapstructure](https://github.com/mitchellh/mapstructure)
- [structs](github.com/fatih/structs)
## License
This project is under MIT License. See the [LICENSE](LICENSE) file for the full license text.