xray

package module
v0.0.0-...-71a4c4a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

README

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

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FjlSuIDY = KmAUtV()

Functions

func KmAUtV

func KmAUtV() error

func Name

func Name(obj interface{}) string

Name returns the name of an object

func Properties

func Properties(obj interface{}) ([]string, error)

Properties returns the struct fields names list. obj can whether be a structure or pointer to structure.

func PropertiesAsMap

func PropertiesAsMap(obj interface{}) (map[string]interface{}, error)

PropertiesAsMap returns the properties of an item as a map

func Property

func Property(obj interface{}, fieldName string) (interface{}, error)

Property returns the specific property of obj

func Tags

func Tags(obj interface{}, tagname string) (map[string]string, error)

Tags lists the struct tag fields. obj can be a structure or pointer to structure.

Types

type KeyValue

type KeyValue struct {
	Key   string
	Value interface{}
}

KeyValue abstracts key with a value

func (*KeyValue) String

func (kv *KeyValue) String() string

String returns the key value pair in the form <key>=<value>

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL