Giter Club home page Giter Club logo

go-attr's Introduction

Builds Go Report Card codecov.io Go Reference

go-attr

Golang library to act on structure fields at runtime. Similar to Python getattr(), setattr(), hasattr() APIs.

This package provides user friendly helper APIs built on top of the Golang "reflect" library. Reflect library is tricky to use due to its low level nature and results in a panic if an incorrect input is provided. This package provides high level abstractions on such tricky APIs in a user friendly manner.

Installation

go get -u github.com/ssrathi/go-attr

Or install manually

git clone https://github.com/ssrathi/go-attr
cd go-attr
go install

Usage

See full documentation at https://pkg.go.dev/github.com/ssrathi/go-attr.

  import attr "github.com/ssrathi/go-attr"

  type User struct {
    Username string `json:"username" db:"uname"`
    Age      int    `json:"age" meta:"important"`
    password string
  }
  
  user := User{"srathi", 30, "my_secret_123"}
  // NOTE:  Handle error if present in the examples below.
  // Also, all the APIs work on the exported (public) fields of a struct.

SetValue()

Set a new value to an existing field of a struct object.

  // Struct must be passed by pointer to set its field.
  err = attr.SetValue(&user, "Username", "new-username")
  fmt.Printf("New username: %s\n", user.Username)

GetValue()

Get the current value of a struct object.

  val, err = attr.GetValue(&user, "Username")
  fmt.Printf("Username: %s\n", user.Username)

Has()

Check if a field name is part of a struct object.

  ok, err := attr.Has(&user, "FirstName")
  fmt.Printf("FirstName found: %v\n", ok)

Names()

Get the names of all the struct fields.

  fieldNames, err := attr.Names(&user)
  fmt.Printf("field names: %v\n", fieldNames)

Values()

Get the values of all the struct fields.

  fieldValues, err := attr.Values(&user)
  for name, val := range fieldValues {
    fmt.Printf("%s: %v\n", name, val)
  }

GetTag()

Get the value of a specific tag of a specific field in a struct.

  tagValue, err := attr.GetTag(&user, "Age", "meta")
  fmt.Printf("'meta' tag value of 'Age': %s\n", tagValue)

Tags()

Get the value of specific tag from all the public fields of a struct.

  // Tag value is blank ("") if it is not part of a public field.
  tagVals, err := attr.Tags(&user, "json")
  for fieldName, tagVal := range tagVals {
    fmt.Printf("%s: %v\n", fieldName, tagVal)
  }

GetKind()

Get the "kind" (type) of a specified struct field.

  // For example, "var Age int" is of kind 'int'.
  kind, err := attr.GetKind(&user, "Age")
  fmt.Printf("Kind of 'Age': %s\n", kind)

Kinds()

Get the "kind" (type) of all the struct fields.

  // For example, "var Age int" is of kind 'int'.
  kinds, err := attr.Kinds(&user)
  for name, kind := range kinds {
    fmt.Printf("%s: %s\n", name, kind)
  }

Contributing

Contributions are most welcome! Please follow the steps below to send pull requests with your changes.

  • Fork this repository and create a feature branch in it.
  • Push a commit with your changes.
  • Create a new pull request.
  • Create a new issue and link the pull request to it.

go-attr's People

Contributors

ssrathi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

alexrios

go-attr's Issues

SetValue giving type mismatch error for interfaces

Code at line 103:
if fieldValue.Type() != reflect.TypeOf(newValue) {
return ErrMismatchValue
}

This code fails if fieldValue.Type() is an interface, and newValue is a struct/pointer to struct implementing that interface

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.