You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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"
)
funcmain() {
// Create a new Jsonnet VMvm:=jsonnet.MakeVM()
// Custom Golang function embedjsonToStringFunc:=&jsonnet.NativeFunction{
Name: "convertToInt",
Params: ast.Identifiers{"x"},
Func: convertToInt,
}
vm.NativeFunction(jsonToStringFunc)
// Read the Jsonnet filejsonnetCode, err:=vm.EvaluateFile("input.jsonnet")
iferr!=nil {
log.Fatal(err)
}
// Marshal the output to YAMLvaroutputinterface{}
err=yaml.Unmarshal([]byte(jsonnetCode), &output)
iferr!=nil {
log.Fatal(err)
}
// Print the YAML outputyamlOutput, err:=yaml.Marshal(output)
iferr!=nil {
log.Fatal(err)
}
fmt.Println(string(yamlOutput))
}
funcconvertToInt(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 {
returnnil, 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 intiferr!=nil {
returnnil, err
}
returnnum, nil
}
The text was updated successfully, but these errors were encountered:
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
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
I am new to jsonnet, so I might be missing something.
float64
, but not integer.integer
throws error but returningfloat
works. (Resolved on latest master. Only affectsv0.20.0
. )input.jsonnet
main.go
The text was updated successfully, but these errors were encountered: