-
Notifications
You must be signed in to change notification settings - Fork 170
cue: add function for Value.Attributes() #529
Conversation
cue/types.go
Outdated
@@ -2007,11 +2007,48 @@ func (v Value) Attribute(key string) Attribute { | |||
return Attribute{internal.NewNonExisting(key)} | |||
} | |||
|
|||
// Attributes returne all attributes for the Value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/returne/reports/
Note that the old API is a bit broken in that there are now both field attributes and attribute declarations. But that is not the problem of this CL. The attribute declarations could perhaps be obtained through Instance and Struct.
Either way, will be good to be explicit about what this does exactly in the doc comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to "Attribute reports all field attributes for the Value"
cue/types.go
Outdated
a.attr.SetName(name) | ||
} | ||
|
||
func (a *Attribute) Vals() map[string]string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would call this Values
or Map
, in general Go style is not in favor of using contractions.
But I would remove this method. It is not necessary, as everything can be done without this method and this method exposes implementation details in the API. It also papers over the fact that there can be multiple attributes with the same key in a field.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have this because I'd like to grab all the key/value items in an attribute, and then iterate over those making decisions along the way. I'm not sure how to iterate over all the fields in an Attribute without a function to access them. Maybe there should be a function Fields
to return them all as a slice instead of this? Renamed this to Map
for now.
@mpvl I'm confused by your last statement, could you provide an example of what you mean by "multiple attributes with the same key in a field?"
All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the ℹ️ Googlers: Go here for more info. |
1 similar comment
All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the ℹ️ Googlers: Go here for more info. |
@mpvl did I mess this PR up with a rebase? I addressed the comments you made but now I see a ton of other commits... (edit) think I fixed the rebase issue 🤦 |
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
1 similar comment
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
I've found the map to be less ideal, preferring to have the written order maintained, like Cue does elsewhere. Now thinking a Thoughts? |
Note that there are now also attribute at the declaration level. So the API needs to take these into account as well. |
tbh, the Attribute API is the more restrictive one for me than the Are they both using the same underlying Attribute type? I'd like to be able to extract all of the content from within the Attribute |
I've added a function to get declaration attributes. This may help in coming up with an API that supports both. |
@mpvl this seems to start supporting the suggestion you made here: #774 (comment) I have a number of open issues / requests / etc around Attributes, maybe a proposal of sorts is in order for them? I can start a new issue outlining all of these, something like "Attribute(s) API" thoughts? |
@verdverm : I've started converging on what would be a proper API, so maybe we don't need to go through these lengths. In a nutshell, we ideally should have only one call for fetching attributes ( In order to keep the API returning stable when we later introduce more attribute types, this method could take a mask specifying the kind of desired attrs:
The This is not too far off from the implementation in this CL. The heavy lifting of extracting the attrs has been implemented in The Does this sound reasonable? |
} | ||
|
||
// Map returns all key/value pairs for the attribute as a map | ||
func (a *Attribute) Map() map[string]string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method should be removed.
@@ -2098,11 +2098,45 @@ func (v Value) Attribute(key string) Attribute { | |||
return Attribute{internal.NewNonExisting(key)} | |||
} | |||
|
|||
// Attributes reports all field attributes for the Value. | |||
func (v Value) Attributes() []Attribute { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See CL-level comment.
First attempt by @verdverm, significant edits by @mpvl. The API has been designed so that new attribute types can be added without breaking anything: the user will always have to explicitly specify the types of attributes that are needed. This does not handle duplicate attributes. In the future, there could be merge functionality for this purpose. This could be done by adding option flags if we can settle on what constitutes good options. Original comments from Tony: This enables a user of the Go API to get all attributes so they can inspect them, rather than only being able to ask if a particular attribute exists on a value. Really useful for tools leveraging CUE! I recall @mpvl might have wanted the return to be a Value or List rather than the slice? Closes #529 #529 GitOrigin-RevId: 6a8951a Change-Id: Ie3cc9983656864fa44852b924a3142c82e336f4a
First attempt by @verdverm, significant edits by @mpvl. The API has been designed so that new attribute types can be added without breaking anything: the user will always have to explicitly specify the types of attributes that are needed. This does not handle duplicate attributes. In the future, there could be merge functionality for this purpose. This could be done by adding option flags if we can settle on what constitutes good options. Original comments from Tony: This enables a user of the Go API to get all attributes so they can inspect them, rather than only being able to ask if a particular attribute exists on a value. Really useful for tools leveraging CUE! I recall @mpvl might have wanted the return to be a Value or List rather than the slice? Closes #529 #529 GitOrigin-RevId: 6a8951a Change-Id: Ie3cc9983656864fa44852b924a3142c82e336f4a
First attempt by @verdverm, significant edits by @mpvl. The API has been designed so that new attribute types can be added without breaking anything: the user will always have to explicitly specify the types of attributes that are needed. This also adds the NumArgs, Arg, and RawArg methods to give access to the attribute fields. This does not handle duplicate attributes. In the future, there could be merge functionality for this purpose. This could be done by adding option flags if we can settle on what constitutes good options. Original comments from Tony: This enables a user of the Go API to get all attributes so they can inspect them, rather than only being able to ask if a particular attribute exists on a value. Really useful for tools leveraging CUE! I recall @mpvl might have wanted the return to be a Value or List rather than the slice? Closes #529 #529 GitOrigin-RevId: 6a8951a Change-Id: Ie3cc9983656864fa44852b924a3142c82e336f4a
First attempt by @verdverm, significant edits by @mpvl. The API has been designed so that new attribute types can be added without breaking anything: the user will always have to explicitly specify the types of attributes that are needed. This also adds the NumArgs, Arg, and RawArg methods to give access to the attribute fields. This does not handle duplicate attributes. In the future, there could be merge functionality for this purpose. This could be done by adding option flags if we can settle on what constitutes good options. Original comments from Tony: This enables a user of the Go API to get all attributes so they can inspect them, rather than only being able to ask if a particular attribute exists on a value. Really useful for tools leveraging CUE! I recall @mpvl might have wanted the return to be a Value or List rather than the slice? Closes #529 #529 GitOrigin-RevId: 6a8951a Change-Id: Ie3cc9983656864fa44852b924a3142c82e336f4a
This enables a user of the Go API to get all attributes so they can inspect them, rather than only being able to ask if a particular attribute exists on a value. Really useful for tools leveraging CUE!
I recall @mpvl might have wanted the return to be a Value or List rather than the slice?