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

Error when using custom functions with int parameters or return types #761

Open
thatInfrastructureGuy opened this issue Jul 12, 2024 · 1 comment

Comments

@thatInfrastructureGuy
Copy link

thatInfrastructureGuy commented Jul 12, 2024

I am new to jsonnet, so I might be missing something.

  • Issue-1: Custom function only accepts float64, but not integer.
  • Issue-2: Custom function returning integer throws error but returning float works. (Resolved on latest master. Only affects v0.20.0. )

input.jsonnet

local convertToInt = std.native('convertToInt');
// local replicas = convertToInt(3); -> (Issue-1)This does NOT Accept integer. But float64 works.
local replicas = convertToInt('3');
{
    replicas: replicas,
}

main.go

package main

import (
	"fmt"
	"log"
	"strconv"

	jsonnet "github.com/google/go-jsonnet"
	"github.com/google/go-jsonnet/ast"
	"gopkg.in/yaml.v3"
)

func main() {
	// Create a new Jsonnet VM
	vm := jsonnet.MakeVM()

	// Custom Golang function embed
	jsonToStringFunc := &jsonnet.NativeFunction{
		Name:   "convertToInt",
		Params: ast.Identifiers{"x"},
		Func:   convertToInt,
	}
	vm.NativeFunction(jsonToStringFunc)

	// Read the Jsonnet file
	jsonnetCode, err := vm.EvaluateFile("input.jsonnet")
	if err != nil {
		log.Fatal(err)
	}

	// Marshal the output to YAML
	var output interface{}
	err = yaml.Unmarshal([]byte(jsonnetCode), &output)
	if err != nil {
		log.Fatal(err)
	}

	// Print the YAML output
	yamlOutput, err := yaml.Marshal(output)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(yamlOutput))
}

func convertToInt(x []interface{}) (interface{}, error) {
	log.Printf("%v %T", x, x)
	y, ok := x[0].(string) 
	// y, ok := x[0].(int) // (Issue-1) int does not work, but float64 works, when x is a number.
	if !ok {
		return nil, fmt.Errorf("Error: Not a string")
	}
	log.Printf("%v %T", y, y)
	num, err := strconv.ParseFloat(y, 10) // works
	// num, err := strconv.Atoi(y) // (Issue-2)does not work if returning int
	if err != nil {
		return nil, err
	}
	return num, nil
}
@thatInfrastructureGuy thatInfrastructureGuy changed the title Error when using custom functions with int Error when using custom functions with intparameters or return types Jul 12, 2024
@thatInfrastructureGuy thatInfrastructureGuy changed the title Error when using custom functions with intparameters or return types Error when using custom functions with int parameters or return types Jul 12, 2024
@thatInfrastructureGuy
Copy link
Author

thatInfrastructureGuy commented Jul 12, 2024

Based on #223

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant