Git Product home page Git Product logo

Comments (4)

johakoch avatar johakoch commented on July 30, 2024

The value returned from toNumberFn.Call(args) is

cty/function/function.go:

func (f Function) Call(args []cty.Value) (val cty.Value, err error) {
	expectedType, dynTypeArgs, err := f.returnTypeForValues(args)
	if err != nil {
		return cty.NilVal, err
	}
	if dynTypeArgs {
		// returnTypeForValues sets this if any argument was inexactly typed
		// and the corresponding parameter did not indicate it could deal with
		// that. In that case we also avoid calling the implementation function
		// because it will also typically not be ready to deal with that case.
		return cty.UnknownVal(expectedType), nil
		       ^^^^^^^^^^^^^^
	}

because of

func (f Function) returnTypeForValues(args []cty.Value) (ty cty.Type, dynTypedArgs bool, err error) {
...
		if val.Type() == cty.DynamicPseudoType {
			if !spec.AllowDynamicType {
				return cty.DynamicPseudoType, true, nil
			}

If I add

				AllowDynamicType: true,

to the parameter specification in cty/function/stdlib/conversion.go

func MakeToFunc(wantTy cty.Type) function.Function {
	return function.New(&function.Spec{
		Description: fmt.Sprintf("Converts the given value to %s, or raises an error if that conversion is impossible.", wantTy.FriendlyName()),
		Params: []function.Parameter{
			{
				Name: "v",
				// We use DynamicPseudoType rather than wantTy here so that
				// all values will pass through the function API verbatim and
				// we can handle the conversion logic within the Type and
				// Impl functions. This allows us to customize the error
				// messages to be more appropriate for an explicit type
				// conversion, whereas the cty function system produces
				// messages aimed at _implicit_ type conversions.
				Type:             cty.DynamicPseudoType,
				AllowNull:        true,
			},
		},

there's no panic, because then the value returned from toNumberFn.Call(args) is a cty.NullVal(cty.Number).

from go-cty.

johakoch avatar johakoch commented on July 30, 2024

This occurs in an HCL-based config language with to_number() and json_encode() functions implemented by stdlib.MakeToFunc(cty.Number) and stdlib.JSONEncodeFunc when passing literal null: json_encode(to_number(null)).

So the cty.NullVal(cty.DynamicPseudoType) is the result of parsing a literal null using ParseHCL() on an *hclparse.Parser.

from go-cty.

johakoch avatar johakoch commented on July 30, 2024

@apparentlymart Do you have any thoughts on this?

from go-cty.

apparentlymart avatar apparentlymart commented on July 30, 2024

Thanks for reporting this, @johakoch!

You were right about the cause; I've fixed it in 4b76b75 and will make a new patch release containing that fix soon.

from go-cty.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.