carvel-dev / ytt

YAML templating tool that works on YAML structure instead of text
https://carvel.dev/ytt
Apache License 2.0
1.65k stars 135 forks source link

Documentation incorrectly instructs user to use `struct.encode()` to collect groups of functions. #389

Closed kieron-dev closed 2 years ago

kieron-dev commented 3 years ago

Scope

Please fix the referenced documentation so that users are instructed to use struct.make() rather than struct.encode() as the former is suitable for structs holding values of any type while the latter only holds primitives.


Original report

What steps did you take: Along the lines of collection-of-functions in the docs, create a file like

load("@ytt:struct", "struct")

def _valid_port(port):
  True
end

urls = struct.encode({"valid_port": _valid_port})

What happened: When this is loaded, I see the error

Evaluating starlark template: 
- (p) unknown type *starlark.Function for conversion to go value (backtrace: goroutine 1 [running]:
    in <toplevel>
      7 | urls = struct.encode({"valid_port": _valid_port})

    reason:
     runtime/debug.Stack(0x9406e0, 0x90a860, 0xc000073f30)
        runtime/debug/stack.go:24 +0x9d
     github.com/k14s/ytt/pkg/template/core.ErrWrapper.func1.1(0xc000160a20)
        github.com/k14s/ytt@/pkg/template/core/errs.go:23 +0x158
     panic(0x90a860, 0xc000073f30)
        runtime/panic.go:679 +0x1b2
     github.com/k14s/ytt/pkg/template/core.StarlarkValue.asInterface(0xb3b6e0, 0xc000166000, 0xb3b720, 0xc00006fc00, 0x90a860, 0xc000073f20)
        github.com/k14s/ytt@/pkg/template/core/starlark_value.go:103 +0x6cb
     github.com/k14s/ytt/pkg/template/core.StarlarkValue.dictAsInterface(0xb3b6e0, 0xc000166000, 0xc000166000, 0x0, 0x0)
        github.com/k14s/ytt@/pkg/template/core/starlark_value.go:113 +0x108
     github.com/k14s/ytt/pkg/template/core.StarlarkValue.asInterface(0xb3b6e0, 0xc000166000, 0xb3b6e0, 0xc000166000, 0xc00006f840, 0xc0001608a0)
        github.com/k14s/ytt@/pkg/template/core/starlark_value.go:85 +0x2b6
     github.com/k14s/ytt/pkg/template/core.StarlarkValue.AsGoValue(...)
        github.com/k14s/ytt@/pkg/template/core/starlark_value.go:27
     github.com/k14s/ytt/pkg/yttlibrary.structModule.Encode(0xc0000aa870, 0xc00011e480, 0xc000073f00, 0x1, 0x1, 0x0, 0x0, 0x0, 0xec0e20, 0x42c8fa, ...)
        github.com/k14s/ytt@/pkg/yttlibrary/struct.go:98 +0xc8
     github.com/k14s/ytt/pkg/template/core.ErrWrapper.func1(0xc0000aa870, 0xc00011e480, 0xc000073f00, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        github.com/k14s/ytt@/pkg/template/core/errs.go:28 +0xff
     github.com/k14s/starlark-go/starlark.(*Builtin).CallInternal(0xc00011e480, 0xc0000aa870, 0xc000073f00, 0x1, 0x1, 0x0, 0x0, 0x0, 0xea4100, 0x7f38eb2cc6d0, ...)
        github.com/k14s/starlark-go@v0.0.0-20200720175618-3a5c849cc368/starlark/value.go:653 +0x8b
     github.com/k14s/starlark-go/starlark.Call(0xc0000aa870, 0xb3b6a0, 0xc00011e480, 0xc000073f00, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, ...)
        github.com/k14s/starlark-go@v0.0.0-20200720175618-3a5c849cc368/starlark/eval.go:1028 +0x190
     github.com/k14s/starlark-go/starlark.(*Function).CallInternal(0xc00006fb40, 0xc0000aa870, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x3, ...)
        github.com/k14s/starlark-go@v0.0.0-20200720175618-3a5c849cc368/starlark/interp.go:305 +0x3ed8
     github.com/k14s/starlark-go/starlark.Call(0xc0000aa870, 0xb3b720, 0xc00006fb40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, ...)
        github.com/k14s/starlark-go@v0.0.0-20200720175618-3a5c849cc368/starlark/eval.go:1028 +0x190
     github.com/k14s/starlark-go/starlark.(*Program).Init(0xc000096228, 0xc0000aa870, 0xc00011fe00, 0x0, 0x0, 0xc00006fa00)
        github.com/k14s/starlark-go@v0.0.0-20200720175618-3a5c849cc368/starlark/eval.go:358 +0x96
     github.com/k14s/ytt/pkg/template.(*CompiledTemplate).eval(0xc000098fc0, 0xc0000aa870, 0xc00011fe00, 0x0, 0x0, 0x0, 0x0, 0x0)
        github.com/k14s/ytt@/pkg/template/compiled_template.go:164 +0x247
     github.com/k14s/ytt/pkg/template.(*CompiledTemplate).Eval(0xc000098fc0, 0xc0000aa870, 0xb32ca0, 0xc000098f60, 0xc0000aa5f0, 0xc0000aa870, 0xc00011fbf0, 0x8, 0x20)
        github.com/k14s/ytt@/pkg/template/compiled_template.go:121 +0x6c8
     github.com/k14s/ytt/pkg/workspace.(*TemplateLoader).EvalStarlark(0xc000098f60, 0xc0000aa690, 0xc0000aa690, 0xc0000aa5f0, 0x1, 0x1, 0x1)
        github.com/k14s/ytt@/pkg/workspace/template_loader.go:258 +0x6bd
     github.com/k14s/ytt/pkg/workspace.(*LibraryLoader).eval(0xc0001619b0, 0xc00006f4c0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        github.com/k14s/ytt@/pkg/workspace/library_loader.go:234 +0x3b3
     github.com/k14s/ytt/pkg/workspace.(*LibraryLoader).Eval(0xc0001619b0, 0xc00006f4c0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        github.com/k14s/ytt@/pkg/workspace/library_loader.go:137 +0x77
     github.com/k14s/ytt/pkg/cmd/template.(*Options).RunWithFiles(0xc0000b7540, 0xc0000737c0, 0x2, 0x2, 0xb3a2a0, 0xc00011f230, 0x0, 0x0, 0x0, 0x0, ...)
        github.com/k14s/ytt@/pkg/cmd/template/cmd.go:169 +0x4f3
     github.com/k14s/ytt/pkg/cmd/template.(*Options).Run(0xc0000b7540, 0x0, 0x0)
        github.com/k14s/ytt@/pkg/cmd/template/cmd.go:96 +0x4e4
     github.com/k14s/ytt/pkg/cmd/template.NewCmd.func1(0xc0000d6dc0, 0xc00011ee40, 0x0, 0x3, 0x0, 0x0)
        github.com/k14s/ytt@/pkg/cmd/template/cmd.go:60 +0x2a
     github.com/cppforlife/cobrautil.WrapRunEForCmd.func1.1(0xc0000d6dc0, 0xc00011ee40, 0x0, 0x3, 0x0, 0x0)
        github.com/cppforlife/cobrautil@v0.0.0-20200514214827-bb86e6965d72/misc.go:45 +0xaf
     github.com/cppforlife/cobrautil.WrapRunEForCmd.func1.1(0xc0000d6dc0, 0xc00011ee40, 0x0, 0x3, 0x0, 0x0)
        github.com/cppforlife/cobrautil@v0.0.0-20200514214827-bb86e6965d72/misc.go:45 +0xaf
     github.com/spf13/cobra.(*Command).execute(0xc0000d6dc0, 0xc00006e090, 0x3, 0x3, 0xc0000d6dc0, 0xc00006e090)
        github.com/spf13/cobra@v1.0.0/command.go:842 +0x460
     github.com/spf13/cobra.(*Command).ExecuteC(0xc0000d6dc0, 0xc0000d6dc0, 0xea3500, 0xc0000a41f8)
        github.com/spf13/cobra@v1.0.0/command.go:950 +0x349
     github.com/spf13/cobra.(*Command).Execute(...)
        github.com/spf13/cobra@v1.0.0/command.go:887
     main.main()
        github.com/k14s/ytt@/cmd/ytt/ytt.go:21 +0x112
     )

What did you expect: No error

Anything else you would like to add: This occurs both using star and yml files. Also locally and in the playground.

Environment: ytt version 0.32.0 NAME="Ubuntu" VERSION="20.04.2 LTS (Focal Fossa)"


Vote on this request

This is an invitation to the community to vote on issues, to help us prioritize our backlog. Use the "smiley face" up to the right of this comment to vote.

👍 "I would like to see this addressed as soon as possible" 👎 "There are other more important things to focus on right now"

We are also happy to receive and review Pull Requests if you want to help working on this issue.

kieron-dev commented 3 years ago

I've just seen the alternative method for doing this in the FAQs: https://carvel.dev/ytt/docs/latest/faq/#can-i-load-multiple-functions-without-having-to-name-each-one. Using struct.make rather than struct.encode works.

kieron-dev commented 3 years ago

I suggest updating the docs to move the collection of functions example from struct.encode to struct.make.

cari-lynn commented 3 years ago

Thanks for making this issue, this is a error in the documentation. struct.encode() does not work for functions so the example should be for struct.make() just like you suggested. Here's a snippit from the same documentation referenced above that confirms this:

Notes:

encode() cannot encode functions nor YAML Fragments. If you wish to make a struct that contains attributes that hold these types, consider make().
pivotaljohn commented 2 years ago

Fixed in vmware-tanzu/carvel#291.

Thanks again for the report @kieron-dev! 🙏🏻