-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: spec: introduce structured tags #23637
Comments
Related to #20165, which was recently declined. But this version is better, because it proposes an alternative. |
I don't see any special need for a new On the other hand, something this proposal doesn't clearly address is that those values must be entirely computable at compile time. That is not a notion that the language currently defines for anything other than constants, and it would have to be carefully spelled out to decide what is permitted and what is not. For example, can a tag, under either the original definition or this new one, have a field of interface type? |
@ianlancetaylor
vs what I would consider an invalid usage:
For your second point, I assumed that it would be clear that any value for any field of a tag has to be a constant. Such a "restriction" makes it clear what can and cannot be a field type, and will rule out having a struct as a field type (or an interface, in your example). |
I wonder if we could solve this without having to change the language, and even better, in Go 1.X rather than waiting for Go 2. As such, I've tried to understand the problem as well as the proposed solution and came up with a different approach to the problem, please see below. First, the problem.
There can totally be a tool that understands how these flags work and allow users to define custom tags and have them validated. This would of course have the advantage of not having to force the change in the toolchain, with the downside that you'd need to have the tool to validate this instead of the compiler. The values should be json, for example: package mypackage
import "json"
import "sqlx"
type MyStruct struct {
Value string `json:"{\"Name\":\"value\"}" sqlx:"{\"Name\":\"value\"}"`
PrivateKey []byte `json:"{\"Ignore\":true}"`
} package json
// +tag
type Tag struct {
Name string
OmitEmpty bool
Ignore bool
} package sqlx
// +tag
type SQLXTag struct {
Name string
} More details can be put into this on how this should be a single tag per package, the struct must be exportable, and so on (which the current proposal also does not address).
This sounds like a problem one could be able to fix with a CL / PR to the documentation of the package which specifically improves it by documenting the tag available or how to use these struct tags.
Should the above proposal with "annotating" a struct in a package work, this means that the tools could also fix the problem of navigating to the definition of tag. Furthermore, the original proposal adds the problem that The downside of my proposal is that this requires people to use non-compiler tools. But given how govet is now partially integrated in go test, this check could also be added to that list. Tools that offer completion to users can be adapted to fulfill the requirement of assisting the user in writing the tag, all without having the language changed with an extra keyword added. And should the compiler ever want to validate these tags, the code would already be there in govet, |
You can always make compiler a bit smarter to understand context and when a keyword is a keyword. As for your proposal, for me adding another set of magic comments further establishes opinion that there's something wrong with the design of the language. Every time I look at these comments they look out of place. Like someone forgot to add some feature and, in order to not break things, shoved everything into comments. There's plenty of magic comments already. I think we should stop and implement proper Go features and not continue developing another language on top of Go. |
As I said above, though, I see no advantage at all to using This proposal still needs more clarity on precisely what is permitted in a tag type, whatever we call it. It's not enough to say "it has to be a constant." We need to spell out precisely what that means. Can it be a constant expression? What types are the fields permitted to have? |
This seems to make the problem worse, to be honest. Instead of making tags easier to write and use, you are now introducing more magic comments. I'm sure I'm not the only one opposed to such solutions, as such comments are very confusing to users (plenty of questions on stackoverflow). Also, what happens when someone puts // +tag before multiple types?
This not only ignores a major part of the problem, illustrated by the proposal (syntax), but also makes it harder to write.
Should we address the obvious? Honest question, I skipped some things as I thought they were too obvious to write.
It's still in the reflect package. Why would an average user ever go and read the reflect package. It's index alone is larger that the documentation of some packages.
I edited my original proposal to remove the inclusion of a new type. This was discussed in the initial discussion with @ianlancetaylor, and I was hoping further discussion would include that as well. |
I've edited the proposal to add more information as to what types are permitted as tags. |
i like the idea of the proposal. I would love to be able to write Metadata that is then checked at compile time, and that i do not need to parse the metadata at runtime. The updated proposal makes sense to me. A Metadata is simply a struct. 1. initial proposal type MyStruct struct {
Value string [
json.Rules{Name: "value"},
sqlx.Name{"value"}
]
PrivateKey []byte [
json.Rules{Ignore: true}
]
} That appears even to be readable (at least to me). Fields and annotations are clearly distinguishable, even without syntax highlighting. Now, i know this is about tags only but if we would want to add metadata to other things than struct fields, i would suggest to put the metadata above the thing that you annotate instead of having it behind. 2 examples that arise to me, are C# Attributes and Java Annotations. Lets see how they would look like on go struct fields 2. C# like type MyStruct struct {
[json.Rules{Name: "value"}]
[sqlx.Name{"value"}]
Value string
[json.Rules{Ignore: true}]
PrivateKey []byte
} That is less readable than 1. 3. Java like type MyStruct struct {
@json.Rules{Name: "value"}
@sqlx.Name{"value"}
Value string
@json.Rules{Ignore: true}
PrivateKey []byte
} That is less readable than 1. but way cleaner than 2. Now in go we already have a syntax for multiple imports and multiple constants. Lets try that 4. Go like type MyStruct struct {
meta (
json.Rules{Name: "value"}
sqlx.Name{"value"}
)
Value string
meta (json.Rules{Ignore: true})
PrivateKey []byte
} That is less compact. Removing the 5. with square brackets type MyStruct struct {
[
json.Rules{Name: "value"}
sqlx.Name{"value"}
]
Value string
[json.Rules{Ignore: true}]
PrivateKey []byte
} The single statement looks like 2. C# like and the multiline statement is still not as compact as i would like it to be. So far i still like the 3. Java like style best. However, if metadata should not be applied to anything other than struct fields (ever) then i prefer the 1. initial proposal style. Now if there are some legal issues with stealing a syntax from another language (i am not a lawyer) then i could think of following 6. hash type MyStruct struct {
# json.Rules{Name: "value"}
# sqlx.Name{"value"}
Value string
# json.Rules{Ignore: true}
PrivateKey []byte
} |
Having the field tags before the field declaration is not very readable, compared to having them afterwards. For the same reason that it is more readable to have the type of a variable after the variable. When you start reading the struct definition, you come upon some meta information about something you haven't yet read. Currently, you know that a PrimaryKey is a byte array, and it is ignored for json marshaling. With your suggestioun, You know that something will be ignored for json marshaling, and afterwards you learn that what is ignored will be a primary key, which is a byte array. |
I understand your point and i partially agree on that. Having metadata on tags only, your suggestion looks best (i might want to omit the comma in multiline though) My intention is to suggest a syntax that might work on structs and methods. Those elements have their own body. Adding metadata behind the body might push it out of sight if the implementation is lengthy. I think metadata should live somewhere near to the name of the element that it annotates and my natural choice is above that name, since below is the body. Syntax highlighting helps to spot the parts of the code you are interested in. So if you are interested in reading the struct definition, your eye will skip the metadata syntax. |
I don't think tags are that important to care about them being out of sight. For the most part, I only care about actual fields and look at tags only in very specific cases. It's actually a good thing that they're out of the way because most of the time you don't need to look at them. Your examples with |
Just wanted to add another quick anecdote. I recently saw this committed by a colleague of mine, a seasoned developer:
Obviously this wasn't tested at runtime, but its clear that even the best of us can overlook the syntax, especially since were are used to catching 'syntax' errors at compile time. |
I like this proposal for property tags. Would it be too much to ask for a similar feature for struct-level tags? |
@kprav33n I'm not sure what you mean by struct-level tags, but I'm guessing it's something that the language does not support today. It sounds like an orthogonal idea that should be discussed separately from this issue. |
@ianlancetaylor Thanks for the comment. Yes, this feature doesn't exist in the language today. Will discuss as a separate issue. |
I really like this proposal since I encountered problems with the current implementation of tags type MyStruct struct {
field string `tag:"This is an extremely long tag and it's hard to view on smaller screens since it is so incredibly long, but it can't be broken using a line break because that would prevent correct parsing of the tag."`
} Adding line breaks will prevent the tag from being parsed correctly: package main
import (
"fmt"
"reflect"
)
type MyStruct struct {
field string `tag:"This is an extremely long tag and it's hard to view on smaller
screens since it is so incredibly long, but it can't be broken using a
line break because that would prevent correct parsing of the tag."`
}
func main() {
v, ok := reflect.ValueOf(MyStruct{}).Type().Field(0).Tag.Lookup("tag")
fmt.Printf("%q, %t", v, ok)
// Output: "", false
} This is kind of documented at https://golang.org/pkg/reflect/#StructTag which clearly forbids line breaks between the "tag-pairs" and says that the quoted string part is in "Go string literal syntax". Which means "interpreted string literal" per specification. |
I'm not sure if this is worth the increase in complexity to the language, etc., etc. But the potential benefits to tooling and improvement to developer experience do seem quite nice, though. I think it is worth exploring the idea. I'm not concerned about the particulars of the syntax, so I'll stick with the convention in the first post. (To give the There's discussion about extending what kinds of types can be constants—which, currently, seems to be mostly taking place in #21130. Let's assume, for the moment that, it's extended to allow a struct whose fields are all types that can be constants. While I agree that a tag should be a defined type, I don't think that should be enforced by the compiler—that can be left to linters. With the above, the proposal reduces to: any vector of constants is a valid tag. This also allows an old-style tag to be easily converted to a new-style tag. For example, say we have some struct with a field like
Given a tool with a complete understanding of the tags defined in the stdlib but no third party libraries, this could be automatically rewritten to
Then, the third party tags could be manually rewritten or rewritten further by tools provided by the third party packages. It would also be possible for the reflect API to work with both old- and new-style tags: I'd also note that most of the benefits of this proposal are for tooling and increased compile time checking. There's little benefit for the program at runtime, but there are some:
|
Some points brought up in #24889 by myself, @ianlancetaylor, @balasanjay, and @bcmills If the tags are any allowed constant they could also be named constants and not just constant literals, for example:
which allows both reuse and documentation on Tags, being ordinary types, can use versioning which in turn allows them to be standardized and collected in central locations. |
I dislike the idea of binding tags and external packages. While field tags are very widely used by those external packages, binding them is very hindering. Having an I think field tags have problems need to be addressed, like OP said: It needs syntax check and tools helping that. But binding them to dependency feels overdoing. |
@leafbebop Why would importing the |
Say I am writing a website, and since Go is supporting Wasm, I am going fullstack. Because of that, I isolate my code of data models for re-usabilty. Now that there is not a way to add field tags to struct, for my server code to be able to co-operate with sql, I add tag fields for And then I decide to re-use the code of data models for my Wasm-based frontend, to trully enjoy the benefit of full-stacking. But here is the problem. I imported Sure, I can just copy my code, delete all tags and build it again. But why should I? The code suddenly becomes non-cross-platform-able (now JS is a GOOS) just because something trivial. It does not make sence. Alternatively, I think it can remains in a keyword style - instead of package, use a string-typed keyword. |
@leafbebop Such code would be in the extreme minority. Not only would it target wasm, but would also have to import cgo modules. Not a lot of people will have to deal with that. And why should everyone else have to suffer the current error-prone structureless mess that you have to triple check when you write, because no tool will be able to help you with that, and then pray that down the line you won't get any runtime errors because a field didn't match? |
By meaning keeping both, I am not referring This will cause code that reads on string field of This might be very rare and impractical case, but I think unofficial implementation of json package would suffer that. Not it is hard to fix, or I'm sure that is going to have an impact, but another concern. |
Since tags are essentially a One unanswered question is... what should happen if a struct field tries to use structured and unstructured tags at the same time? Surely this will happen as libraries slowly migrate to structured tags. Should conflicting options in the structured tag override the string-based tags? Should the library make this decision? Should there be some sort of convention? If so, what? |
@bminer |
@urandom - I agree, and it's probably not enforceable. Just pointing out the corner case that might warrant a new guideline / convention -- perhaps even something that is checked by a linter. Should a library completely ignore the string / unstructured tags when both structured and unstructured tags are present? Example: type Person struct {
FirstName string `json:"firstName" bson:"first"` [json.Name("fName")]
} EDIT: Fixed invalid string tag. :)
I think it would help to address these questions in the proposal -- even if it's just a convention that isn't enforceable. Thoughts? |
I think building this idea of stringification in would mostly defeat the point of the proposal. If you stringify the typed tags, that mapping has to be injective, so you don't get collisions. But if you have an injective mapping, why not just use that mapping for your string-tags in the first place? Or to put another way: To be well-defined, the stringification would likely need to include the import path of the type defining the tag - and if the package using it knows that it needs to specify the import path, why not move it over to the structured tagging API while you're at it? Do any of y'all actually know a case where you'd need to support both simultaneously? ISTM that usually whoever adds the struct-tags will have a certain amount of control (and/or expectations) over what packages that type is used with - and at the end of the day, the syntax is still bound to the package defining the tags, so if a third-party library wants to drop-in replace that package (say, a third-party json encoder) it seems reasonable to expect it to conform to that new API in a timely manner (after all, support for structured tags in At the end of the day, I don't really think the goal here should be to prescribe how packages actually end up performing that move in all eventualities. It should really just be about making a reasonable path possible. |
@bminer |
@urandom - Ha! Nice catch. I didn't realize the tag was invalid. :) Anyway, I think that I agree with your convention: ignore string-based tags entirely if structured tags exist. This can lead to strange situations, though... Suppose the BSON library does not support structured tags yet. So we write: type Person struct {
FirstName string `bson:"firstName"` [json.Name("firstName")]
} Then, suddenly the author adds support for structured tags and pushes a minor version release. The code above might silently break because the string-based tag will be ignored (by convention) by the BSON library. Anyway, maybe this is worth discussing further? Sadly, since Go does not support structured tags and unstructured tags won't be going anywhere, I think it's really important to understand how we migrate between the two. @Merovius - you are right that stringification can be weird, but current libraries have their own opinions about the namespace already. For example, for the sake of brevity, one writes |
As a follow-up to my last comment, perhaps the convention should be: If a pertinent structured tag is used, all unstructured tags should be ignored. In the example above, there are no BSON-specific structured tags, so unstructured tags would get processed. If there were 1 or more BSON-specific structured tags, the BSON encoder library would ignore all unstructured tags. I also like this approach because linters can warn users when both tag types are being used (at least for commonly-used libraries like encoding/json). Still, using the word "pertinent" makes for rather loose language, and as a computer scientist, I don't like it much. |
@bminer, with your bson example, your code would have to contain a single bson.Tag() for the bson library to start ignoring string tags. It's not enough that it contains any tags at all. So you, as a writer, will have to make the change yourself as well. |
@bminer my point was exactly that you are diving too far into the weeds. What to do if there are both is exactly relevant, if a) there have to be both and b) if they have differences. IMO a) won't be the case in >99% of use-cases and thus for b) it's fine to rely on "don't do that then". IMO you are complicating the design enormously for very little benefit - trying to address something that I believe to be entirely uncommon. And not just that, IMO the case you worry about even gets worse. If there are some rules about the compiler sometimes rewriting tags or them sometimes being ignored, then if someone sees that their encoding-process (or whatever) doesn't work as expected, they need to worry about and try to understand those rules too. If there are simply two APIs, one for typed and one for string tags, then thinking through how to move from one to the other is a pretty simple matrix - does the declaration contain typed/string tags yes/no and does the consumer site use the typed tag API yes/no. If a package wants to move to typed packages, it just has to look at this matrix and decide which cases are relevant to its particular use-case and decide if it's happy with what will happen. If, however, there is some magical system in place to translate from one system to the other, that will have to be understood and taken into account, which will make the decision much harder, what to do. And if some user wonders why their encoding (or whatever) is broken, they also have to understand this system - and users already have problems understanding the current string-tag syntax on its own. IMO, if you just treat them as separate systems, it is pretty clear what happens when you add typed tags to a struct - code will simply continue to work as before, until it explicitly adopts the typed tag API. And it's pretty clear what happens when you remove string tags from a struct - code that is still relying on the string tag API will break. That's clear, it's simple and it gives a good migration path. Note, that without any magic there is a pretty clear and obvious strategy for tag-consumers to adopt typed tags that makes the problem you identified vanish: Look for typed tags you are interested in, if you can't find any, use the string-tag. When |
I think we have all reached the same conclusion: a library should ignore all string tags if a relevant structured tag exists. @urandom Perhaps this should be included in the original proposal for clarity? |
Any progress? This issue has been silent for three years. |
One thing that I don't think I have seen addressed in this so far is that of multiple tags in the same package. The current string tags allow for using different tags in the same way. The specific example I know of is the go-playground/validator package. I use this feature because I have some cases where I need to validate parts of a struct in one way, but other times need to validate it differently. (specifically the creation path vs the update path have different requirements.) |
@deefdragon |
type User struct {
//Other assorted variables above and below
PaymentStatus string `validateCreate:"oneof=GoodGraces" validateUpdate:"oneof=GoodGraces LatePayment ChargeBack"`
} This is just a one field example, but in this example, the User struct can only be created if the PaymentStatus is set to GoodGraces (preventing forgetting it or using the incorrect value for a new user), while it can be updated to one of the three items listed. This is what a create might look like. //create the validator somewhere (likely in program init, but also possible here
vc := validator.New()
//set the tag to the expected tag
vc.SetTagName("validateCreate")
//... other setup etc.
//... create user object, set defaults, update data based on provided inputs etc.
u := User{}
//check for errors in user creation somewhere down the line
err := vc.StructCtx(ctx, u)
if err != nil {
//handle error
}
Giving it some thought, I suspect that a Name/Key/Tag field in the structured tag would be fine to differentiate, and could be set by any library that needs it, but I thought it was important to acknowledge the current lack of discussion around custom tag names. |
You could probably do something like type User struct {
PaymentStatus string validator.Create("oneof=GoodGraces"), validator.Update("oneof=GoodGraces LatePayment ChargeBack")
} Then, instead of setting the tag name to check, you could do something like vc := validator.New()
vc.SetMode(validator.ModeCreate) // ModeCreate could be a function that gets the correct tag.
// ... |
We could also allow multiple typed tags per type. Allowing you to do type User struct {
PaymentStatus string validator.Create("GoodGraces"), validator.Update("GoodGraces"), validator.Update("LatePayment"), validator.Update("ChargeBack")
} The tags would be exposed by More complex structures are not possible to express (so you couldn't also have a concept of "and", only of "or", for example). But I also don't think we'd want that anyways. TBQH in my opinion what is already done via struct tags is too "magic" anyways. |
not sure what the correct final solution should be, but my library uses structs to validate struct tags, so something to consider or at least a workaround for now. https://github.com/matt1484/spectagular/ |
I've recently gotten into doing some custom marshaling/unmarshaling using struct tags in Go1, and noticed that there is a lot of commonality between the two sides when it comes to parsing the structure. Perhaps the solution here is to define a format for the string and then provide additional capabilities in Go itself so folks don't need to do all the heavy lifting to interact with struct tags. An API like the following could work: // field - the type data and field information
// value - the instance data
// parameters - a list of the strings attached to the struct tag, interpretation of each value is up to the handler
type FieldHandler(field *reflect.StructField, value *reflect.Value, parameters []string) error
// v - any value
// tagName - the name of the struct tag to parser (json, bson, custom name, etc)
// handler - instance of `FieldHandler` that will perform the desired action (marshal, unmarshal, validation rules, etc)
func StructTagParser(v any, tagName string, handler FieldHandler) error
The compiler would then be able to enforce the formatting of the Struct Tag string data, however it would not be able to validate the internal meanings. So This would maintain backward compatibility since the struct tag data itself wouldn't be changed; while adding some additional functionality that makes using struct tags easier to work with and putting some additional guidelines on the format. The only part that might break is if something utilized the string data differently than expected; however, that should likely be pretty low impact that could be negated with documentation and notices. |
this can be included in go 1 actually, this won't break existing go code. |
This proposal is for a new syntax for struct tags, one that is formally defined in the grammar and can be validated by the compiler.
Problem
The current struct tag format is defined in the spec as a string literal. It doesn't go into any detail of what the format of that string might look like. If the user somehow stumbles upon the reflect package, a simple space-separated, key:"value" convention is mentioned. It doesn't go into detail about what the value might be, since that format is at the discretion of the package that uses said tag. There will never be a tool that will help the user write the value of a tag, similarly to what gocode does with regular code. The format itself might be poorly documented, or hard to find, leading one to guess what can be put as a value. The reflect package itself is probably not the biggest user-facing package in the standard library as well, leading to a plethora of stackoverflow questions about how multiple tags can be specified. I myself have made the error a few times of using a comma to delimit the different tags.
Proposal
EDIT: the original proposal introduced a new type. After the initial discussion, it was decided that there is no need for a new type, as a struct type or custom types whose underlying types can be constant (string/numeric/bool/...) will do just as well.
A tag value can be either a struct, whose field types can be constant, or custom types, whose underlying types are constant. According to the go spec, that means a field/custom type can be either a string, a boolean, a rune, an integer, a floating-point, or a complex number. Example definition and usage:
Users can instantiate values of such types within
struct
definitions, surrounded by[
and]
and delimited by,
. The type cannot be omitted when the value is instantiated.Benefits
Tags are just types, they are clearly defined and are part of a package's types. Tools (such as gocode) may now be made for assisting in using such tags, reducing the cognitive burden on users. Package authors will not need to create "value" parsers for their supported tags. As a type, a tag is now a first-class citizen in godoc. Even if a tag lacks any kind of documentation, a user still has a fighting chance of using it, since they can now easily go to do definition of a tag and just look up its fields, or see the definition in godoc. Finally, if the user has misspelled something, the compiler will now inform them of an error, instead of it occurring either at runtime, or being silently ignored as is the case right now.
Backwards compatibility
To preserve backwards compatibility, string-based tags will not be removed, but merely deprecated. To ensure a unified behavior across libraries, their authors should ignore any string-based tags if any of their recognized structured tags have been included for a field. For example:
A hypothetical json library, upon recognizing the presence of the
json.OmitEmpty
tag, should not bother looking for any string-based tags. Whereas, the yaml library in this example, will still use the defined string-based tag, since no structured yaml tags it recognizes have been included by the struct author.Side note
This proposal is strictly for replacing the current stuct tags. While the tag grammar can be extended to be applied to a lot more things that struct tags, this proposal is not suggesting that it should, and such a discussion should be done in a different proposal.
The text was updated successfully, but these errors were encountered: