Comments (5)
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.
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.
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.
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.
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)
- Custom Map function HOT 1
- Report fields of anonymous fields HOT 2
- stack overflow on structs.Map() HOT 1
- Add Addr method for Field HOT 1
- structs.Map return empty map HOT 3
- Range over nested slice of structs (using fields) HOT 3
- structs.Values() does not parse time.Time field HOT 2
- Why don't cache type information that has been reflected ? HOT 1
- Custom how nested struct marshals HOT 2
- Nested reflection? HOT 1
- How about add a api for convert map to structs? HOT 3
- Changing case of field name(key) in the generated map HOT 3
- Versions and tags HOT 4
- Proposal: Create a map and omit nil values HOT 3
- Nested structs HOT 1
- Get tag-value without omitempty HOT 2
- New() with options HOT 2
- I get empty return for this HOT 1
- panic when using both flatten and omitempty HOT 1
- Values that are structs lose their tags HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from structs.