• Stars
    star
    27
  • Rank 905,827 (Top 18 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 5 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Helpers for making the use of reflection easier

go-xray

Go Report Card Documentation license GitHub version GitHub issues

This is a Golang 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:

package main

import (
    "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.

package main

import (
    "fmt"

    "github.com/pieterclaerhout/go-xray"
)

func main() {

    v := &test{}

    fmt.Println(xray.Name(v))
    // Ouptut: test

    n := nil

    fmt.Println(xray.Name(n))
    // Ouptut: <nil>
    
}

Properties

The function xray.Properties returns the a list with the property names defined in a struct:

package main

import (
    "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:

package main

import (
    "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:

package main

import (
    "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:

package main

import (
    "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",
    // }

}

More Repositories

1

go-james

James is your butler and helps you to create, build, debug, test and run your Go projects
Go
61
star
2

go-waitgroup

A sync.WaitGroup with error handling and concurrency control
Go
43
star
3

export-komoot

Proof-of-concept to export your planned tours from Komoot
Go
24
star
4

go-finance

Finance related Go functions (e.g. exchange rates, VAT number checking, …)
Go
23
star
5

go-log

A logging library with strack traces, object dumping and optional timestamps
Go
10
star
6

go-geoip

A module to make it easier to use the MaxMind GeoIP database in a microservice environment
Go
5
star
7

example-command-output

An example for https://www.yellowduck.be/posts/reading-command-output-line-by-line/
Go
4
star
8

kubeboard

A very basic way to run the Kubernetes Dashboard as a separate app
Go
2
star
9

example-json-unixtimestamp

An example project for https://www.yellowduck.be/posts/handling-unix-timestamps-in-json/
Go
2
star
10

kotlin-yellowduck-gpx

A GPX library written in Kotlin
Kotlin
2
star
11

go-formatter

A package which allows you to format JSON, SQL, …
Go
2
star
12

jono-indesign-scripts

A couple of scripts used to automate tedious InDesign tasks
JavaScript
2
star
13

detectapplesilicon

Detecting Apple Silicon using Go
Go
2
star
14

pytest-as-a-github-action

Running pytest as a GitHub action
Python
2
star
15

go-html

A Go way of generating HTML without writing templates
Go
2
star
16

vscode-snippets

Snippets I'm using for Visual Studio Code
1
star
17

example-apiclient

An example project for https://www.yellowduck.be/posts/mocking-a-http-server/
Go
1
star
18

go-date

A Go library with date related functions
Go
1
star
19

laravel-sanctum-api

PHP
1
star
20

go-jobqueue

Background job queue based on MySQL (for now)
Go
1
star
21

example-jwt

Example with Labstack Echo and JWT authentication
Go
1
star
22

pieterclaerhout

My GitHub profile page.
1
star
23

add-jira-ticket-number

Add Jira ticket number
TypeScript
1
star