Categorygithub.com/roninzo/structs
modulepackage
1.0.6
Repository: https://github.com/roninzo/structs.git
Documentation: pkg.go.dev

# README

Golang structs Go Reference license build coverage

Package structs implements a generic interface for manipulating Go structs. The related API is powered and inspired from the Go reflection package.

Installation

go get github.com/roninzo/structs

Usage

Example

main.go:

package main

import (
	"fmt"

	"github.com/roninzo/structs"
)

func main() {
	type T struct {
		String string
		Uint   uint
		Bool   bool
		Int    int32
	}

	t := T{
		String: "Roninzo",
		Uint:   123456,
		Bool:   true,
		Int:    5,
	}

	s, err := structs.New(&t)
	if err != nil {
		fmt.Printf("New[Error]: %v.\n", err)
		return
	}

	fmt.Printf("Name               : %v.\n", s.Name())
	fmt.Printf("Value of 1st field : %v.\n", s.Field(0).Value())
	fmt.Printf("Value of Uint      : %v.\n", s.Field("Uint").Value())
	fmt.Printf("Value of Int       : %v.\n", s.Field("Int").Value())
	fmt.Printf("Sprint: %s.\n", s.Sprint())

	err = s.Field("Uint").Set(uint(654321))
	if err != nil {
		fmt.Printf("Set[Error]: %v.\n", err)
	}

	err = s.Field("Int").Set(6)
	if err != nil {
		fmt.Printf("Set[Error]: %v.\n", err)
	}

	err = s.Field("Bool").Set(6)
	if err != nil {
		fmt.Printf("Set[Error]: %v.\n", err)
	}

	fmt.Printf("Value of String    : %s.\n", s.Field("String").String()) // syntax for %s verb
	fmt.Printf("Value of Uint      : %d.\n", s.Field("Uint").Uint())     // syntax for %d verb
	fmt.Printf("Value of Int       : %d.\n", s.Field("Int").Int())       // syntax for %d verb
	fmt.Printf("Sprint: %s.\n", s.Sprint())
	fmt.Printf("\nVerification       :\n")
	fmt.Printf("t.String           : %s.\n", t.String)
	fmt.Printf("t.Uint             : %d.\n", t.Uint)
	fmt.Printf("t.Int              : %d.\n", t.Int)
}

Execute

go run main.go

Output

Name               : T.
Value of 1st field : Roninzo.
Value of Uint      : 123456.
Value of Int       : 5.
Sprint: {
	"String": "Roninzo",
	"Uint": 123456,
	"Bool": true,
	"Int": 5
	}.
Set[Error]: wrong kind of value for field T.Bool. got: 'int' want: 'bool'.
Value of String    : Roninzo.
Value of Uint      : 654321.
Value of Int       : 6.
Sprint: {
	"String": "Roninzo",
	"Uint": 654321,
	"Bool": true,
	"Int": 6
	}.

Verification       :
t.String           : Roninzo.
t.Uint             : 654321.
t.Int              : 6.

Caveat

Package API is not final yet.

Documentation

Dependencies

Inspired/forked from

To Do

  • Extend support for Pointer to struct fields
  • Extend support for Slice of any Type as struct fields
  • Extend support for Map of any Type as struct fields
  • Extend support for Method of struct
  • Extend support for complex64/128 field types
  • Implement Map
  • Improve performance
  • Improve coverage

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

Clone returns a copy from a struct out of nothing.
Compare returns dest boolean comparing two structs.
Copy makes a copy, for instance, of a struct pointer.
Defaults ...
Diff returns differences between two structs.
Fields returns a slice of *StructField.
Forward copies only non-zero values between two structs, i.e.
IndirectStruct finds the struct or structs in the interface dest.
MapFunc returns a copy of the StructValue s with all its fields modified according to the mapping function handler.
Name returns the structs's type name within its package.
Names returns a slice of field names.
New returns a new StructValue initialized to the struct concrete value stored in the interface dest.
Replace returns a copy of the struct dest with the first n non-overlapping instance of old replaced by new.
ScanFromMap trusted source maps of string to interface{} row into Go struct dest.
Sprint returns a MarshalIndent string.
Sprint returns a Marshal one-line string (without indenting).
Transpose loops through target fields and set value of its related source field.
Unmarshal parses the Go struct and stores the result in the value pointed to by dest.

# Constants

No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
could not replace value in struct.
No description provided by the author
No description provided by the author

# Structs

StructField represents a single struct field that encapsulates high level functions around the field.
StructRows represents a single row of a struct from a StructValue containing a slice of structs.
StructValue is the representation of a Go struct powered by the Go reflection package.

# Type aliases

StructFields represents all struct fields that encapsulates high level functions around the struct fields.