Coder Social home page Coder Social logo

Comments (5)

fatih avatar fatih commented on August 19, 2024

Hi @dominicbarnes

To get the tags you also need to define the tag key name. Such as structs, json, etc.. You can do it already with the following (for your example):

package main

import (
	"fmt"

	"github.com/fatih/structs"
)

type Person struct {
	FirstName string `structs:"first_name"`
	LastName  string `structs:"last_name"`
}

func main() {
	var tagNames []string
	for _, f := range structs.New(Person{}).Fields() {
		tagNames = append(tagNames, f.Tag("structs"))
	}

	fmt.Println(tagNames)
}

As you see it's just three lines to collect all tag names. I don't want to further expand this library as I believe it already provides the necessary API. Thanks for you feedback.

from structs.

dominicbarnes avatar dominicbarnes commented on August 19, 2024

The example you have misses quite a few details, such as additional parts in the tag like omitempty or omitnested and using - or empty string to ignore a field. Plus, it's inconvenient to establish my own for loop to accomplish this.

Would you at least be open to a PR that adds this feature? (rather than needing to write it yourself?)

from structs.

fatih avatar fatih commented on August 19, 2024

I'm not sure those are important. The struct pkg will take care of it already. Can you share an example what you mean?

from structs.

dominicbarnes avatar dominicbarnes commented on August 19, 2024

To elaborate on my first example:

type Person struct {
  FirstName string    `structs:"first_name"`
  LastName  string    `structs:"last_name"`
  Birthdate time.Time `structs:"birthdate,omitnested"`
  CacheKey  string    `structs:"-"`
}

Using the code you have above, I'd end up with:

[]string{"first_name", "last_name", "birthdate,omitnested", "-"}

But what I really want is:

[]string{"first_name", "last_name", "birthdate"}

from structs.

fatih avatar fatih commented on August 19, 2024

Hi @dominicbarnes

I see what you mean. I worked on a separate, improved and powerful tags package that deals with this any many other things. Let keep this open, I'll add the necessary feature once I have time (just need to replace the internal tags.go file with the package I wrote and expose it with some well defined API's)

from structs.

Related Issues (20)

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.