Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pieterclaerhout/go-xray
Helpers for making the use of reflection easier
https://github.com/pieterclaerhout/go-xray
Last synced: about 1 month ago
JSON representation
Helpers for making the use of reflection easier
- Host: GitHub
- URL: https://github.com/pieterclaerhout/go-xray
- Owner: pieterclaerhout
- License: apache-2.0
- Created: 2019-10-01T08:40:51.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-24T06:14:17.000Z (6 months ago)
- Last Synced: 2024-08-03T19:10:03.626Z (5 months ago)
- Language: Go
- Size: 13.7 KB
- Stars: 27
- Watchers: 5
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-extra - go-xray - 10-01T08:40:51Z|2019-11-20T17:31:59Z| (Generators / Utility/Miscellaneous)
README
## go-xray
[![Go Report Card](https://goreportcard.com/badge/github.com/pieterclaerhout/go-xray)](https://goreportcard.com/report/github.com/pieterclaerhout/go-xray)
[![Documentation](https://godoc.org/github.com/pieterclaerhout/go-xray?status.svg)](http://godoc.org/github.com/pieterclaerhout/go-xray)
[![license](https://img.shields.io/badge/license-Apache%20v2-orange.svg)](https://github.com/pieterclaerhout/go-xray/raw/master/LICENSE)
[![GitHub version](https://badge.fury.io/gh/pieterclaerhout%2Fgo-xray.svg)](https://badge.fury.io/gh/pieterclaerhout%2Fgo-xray)
[![GitHub issues](https://img.shields.io/github/issues/pieterclaerhout/go-xray.svg)](https://github.com/pieterclaerhout/go-xray/issues)This is a [Golang](https://golang.org) library with reflection related functions which I use in my different projects.
## `KeyValue`
This type is used to construct a key-value pair. You can use it as follows:
```go
package mainimport (
"fmt""github.com/pieterclaerhout/go-xray"
)func main() {
pair := &xray.KeyValue{
Key: "key",
Value: "value",
}fmt.Println(pair.String())
// Ouptut: key=value}
```If both `Key` and `Value` are empty, an empty string is returned.
## `Name`
Name allows you to get the name of an object.
```go
package mainimport (
"fmt""github.com/pieterclaerhout/go-xray"
)func main() {
v := &test{}
fmt.Println(xray.Name(v))
// Ouptut: testn := nil
fmt.Println(xray.Name(n))
// Ouptut:
}
```## `Properties`
The function `xray.Properties` returns the a list with the property names defined in a struct:
```go
package mainimport (
"fmt""github.com/pieterclaerhout/go-xray"
)func main() {
type sampleStruct struct {
Name string `form:"name" json:"name"`
Title string `form:"title" json:"title"`
}v, _ := xray.Properties(sampleStruct{})
// v now contains:
// []string{
// "Name",
// "Title",
// }}
```The function `xray.PropertiesAsMap` does the same, but also includes the values and returns a `map[string]interface{}` instance:
```go
package mainimport (
"fmt""github.com/pieterclaerhout/go-xray"
)func main() {
type sampleStruct struct {
Name string `form:"name" json:"name"`
Title string `form:"title" json:"title"`
}v, _ := xray.PropertiesAsMap(sampleStruct{})
// v now contains:
// map[string]interface{}{
// "Name": "name",
// "Title": "title",
// }}
```The function `xray.Property` retrieves a single value by it's name:
```go
package mainimport (
"fmt""github.com/pieterclaerhout/go-xray"
)func main() {
type sampleStruct struct {
Name string `form:"name" json:"name"`
Title string `form:"title" json:"title"`
}v, _ := xray.Property(sampleStruct{}, "Name")
// v now contains:
// "name"}
```## `Tags`
The `xray.Tags` function allows you to easily extract tags from a struct:
```go
package mainimport (
"fmt""github.com/pieterclaerhout/go-xray"
)func main() {
type sampleStruct struct {
Name string `form:"name" json:"name"`
Title string `form:"title" json:"title"`
}v, _ := xray.Tags(sampleStruct{}, "form")
// v now contains:
// map[string]string{
// "Name": "name",
// "Title": "title",
// }}
```