Skip to content
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

FieldByName Can't Get Hidden Field #880

Closed
cueckoo opened this issue Jul 3, 2021 · 2 comments
Closed

FieldByName Can't Get Hidden Field #880

cueckoo opened this issue Jul 3, 2021 · 2 comments
Labels
wontfix This will not be worked on

Comments

@cueckoo
Copy link
Collaborator

cueckoo commented Jul 3, 2021

Originally opened by @rawkode in cuelang/cue#880

What version of CUE are you using (cue version)?

0.3.0

Does this issue reproduce with the latest release?

FieldByName has a second parameter, a bool; which according to the comments should allow us to get a hidden field or definition.

package main

import (
	"fmt"

	"cuelang.org/go/cue"
)

func main() {
	var cueRuntime cue.Runtime

	testCue := `{
	_schema: {
		version: "v1"
	}
}`

	cueInstance, err := cueRuntime.Compile("", testCue)
	cueValue := cueInstance.Value()

	hiddenField, err := cueValue.FieldByName("_schema", true)
	if err != nil {
		fmt.Println("Error", err)
	}
	fmt.Println(hiddenField.Name)

	fields, _ := cueValue.Fields(cue.All())
	for fields.Next() {
		if fields.Label() == "_schema" {
			fmt.Println("Found _schema", fields.Value())
		}
	}
}
@cueckoo cueckoo added the wontfix This will not be worked on label Jul 3, 2021
@cueckoo cueckoo closed this as completed Jul 3, 2021
@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @mpvl in cuelang/cue#880 (comment)

We have just officially deprecated FieldByName. Hidden fields are tricky, as they are scoped by package, pretty much like Go's non-exported names. So FieldByName was broken for that purpose.

Really the only thing you'll need for looking up fields is LookupPath.

E.g. v.LookupPath(cue.MakePath(cue.Hid("_schema", "_"))).

This API will allow looking up hidden fields related to any of the packages.

@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @mpvl in cuelang/cue#880 (comment)

Note that the Iterator interface is broken too in that regard. We should implement Selector, instead of Label, to be accurate.

The API is currently being reworked to use the Path/Selector approach, which is nice, much more powerful, and prepares for the CUE query extension (#165 ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

1 participant