utils/nativefuncs.go
func RegisterNativeFuncs(vm *jsonnet.VM, resolver Resolver) {
	// TODO(mkm): go-jsonnet 0.12.x now contains native std.parseJson; deprecate and remove this one.
	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "parseJson",
		Params: []jsonnetAst.Identifier{"json"},
		Func: func(args []interface{}) (res interface{}, err error) {
			data := []byte(args[0].(string))
			err = json.Unmarshal(data, &res)
			return
		},
	})

	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "parseYaml",
		Params: []jsonnetAst.Identifier{"yaml"},
		Func: func(args []interface{}) (res interface{}, err error) {
			ret := []interface{}{}
			data := []byte(args[0].(string))
			d := yaml.NewYAMLToJSONDecoder(bytes.NewReader(data))
			for {
				var doc interface{}
				if err := d.Decode(&doc); err != nil {
					if err == io.EOF {
						break
					}
					return nil, err
				}
				ret = append(ret, doc)
			}
			return ret, nil
		},
	})

	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "manifestJson",
		Params: []jsonnetAst.Identifier{"json", "indent"},
		Func: func(args []interface{}) (res interface{}, err error) {
			value := args[0]
			indent := int(args[1].(float64))
			data, err := json.MarshalIndent(value, "", strings.Repeat(" ", indent))
			if err != nil {
				return "", err
			}
			data = append(data, byte('\n'))
			return string(data), nil
		},
	})

	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "manifestYaml",
		Params: []jsonnetAst.Identifier{"json"},
		Func: func(args []interface{}) (res interface{}, err error) {
			value := args[0]
			output, err := goyaml.Marshal(value)
			return string(output), err
		},
	})

	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "resolveImage",
		Params: []jsonnetAst.Identifier{"image"},
		Func: func(args []interface{}) (res interface{}, err error) {
			return resolveImage(resolver, args[0].(string))
		},
	})

	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "escapeStringRegex",
		Params: []jsonnetAst.Identifier{"str"},
		Func: func(args []interface{}) (res interface{}, err error) {
			return regexp.QuoteMeta(args[0].(string)), nil
		},
	})

	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "regexMatch",
		Params: []jsonnetAst.Identifier{"regex", "string"},
		Func: func(args []interface{}) (res interface{}, err error) {
			return regexp.MatchString(args[0].(string), args[1].(string))
		},
	})

	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "regexSubst",
		Params: []jsonnetAst.Identifier{"regex", "src", "repl"},
		Func: func(args []interface{}) (res interface{}, err error) {
			regex := args[0].(string)
			src := args[1].(string)
			repl := args[2].(string)

			r, err := regexp.Compile(regex)
			if err != nil {
				return "", err
			}
			return r.ReplaceAllString(src, repl), nil
		},
	})
}
func RegisterNativeFuncs(vm *jsonnet.VM, resolver Resolver) {
	// TODO(mkm): go-jsonnet 0.12.x now contains native std.parseJson; deprecate and remove this one.
	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "parseJson",
		Params: []jsonnetAst.Identifier{"json"},
		Func: func(args []interface{}) (res interface{}, err error) {
			data := []byte(args[0].(string))
			err = json.Unmarshal(data, &res)
			return
		},
	})

	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "parseYaml",
		Params: []jsonnetAst.Identifier{"yaml"},
		Func: func(args []interface{}) (res interface{}, err error) {
			ret := []interface{}{}
			data := []byte(args[0].(string))
			d := yaml.NewYAMLToJSONDecoder(bytes.NewReader(data))
			for {
				var doc interface{}
				if err := d.Decode(&doc); err != nil {
					if err == io.EOF {
						break
					}
					return nil, err
				}
				ret = append(ret, doc)
			}
			return ret, nil
		},
	})

	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "manifestJson",
		Params: []jsonnetAst.Identifier{"json", "indent"},
		Func: func(args []interface{}) (res interface{}, err error) {
			value := args[0]
			indent := int(args[1].(float64))
			data, err := json.MarshalIndent(value, "", strings.Repeat(" ", indent))
			if err != nil {
				return "", err
			}
			data = append(data, byte('\n'))
			return string(data), nil
		},
	})

	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "manifestYaml",
		Params: []jsonnetAst.Identifier{"json"},
		Func: func(args []interface{}) (res interface{}, err error) {
			value := args[0]
			output, err := goyaml.Marshal(value)
			return string(output), err
		},
	})

	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "resolveImage",
		Params: []jsonnetAst.Identifier{"image"},
		Func: func(args []interface{}) (res interface{}, err error) {
			return resolveImage(resolver, args[0].(string))
		},
	})

	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "escapeStringRegex",
		Params: []jsonnetAst.Identifier{"str"},
		Func: func(args []interface{}) (res interface{}, err error) {
			return regexp.QuoteMeta(args[0].(string)), nil
		},
	})

	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "regexMatch",
		Params: []jsonnetAst.Identifier{"regex", "string"},
		Func: func(args []interface{}) (res interface{}, err error) {
			return regexp.MatchString(args[0].(string), args[1].(string))
		},
	})

	vm.NativeFunction(&jsonnet.NativeFunction{
		Name:   "regexSubst",
		Params: []jsonnetAst.Identifier{"regex", "src", "repl"},
		Func: func(args []interface{}) (res interface{}, err error) {
			regex := args[0].(string)
			src := args[1].(string)
			repl := args[2].(string)

			r, err := regexp.Compile(regex)
			if err != nil {
				return "", err
			}
			return r.ReplaceAllString(src, repl), nil
		},
	})
}
func RegisterNativeFuncs ( vm * jsonnet . VM , resolver Resolver ) { // TODO(mkm): go-jsonnet 0.12.x now contains native std.parseJson; deprecate and remove this one. vm . NativeFunction ( & jsonnet . NativeFunction { Name : " " , Params : [ ] jsonnetAst . Identifier { " " } , Func : func ( args [ ] interface { } ) ( res interface { } , err error ) { data := [ ] byte ( args [ 0 ] . ( string ) ) err = json . Unmarshal ( data , & res ) return } , } ) vm . NativeFunction ( & jsonnet . NativeFunction { Name : " " , Params : [ ] jsonnetAst . Identifier { " " } , Func : func ( args [ ] interface { } ) ( res interface { } , err error ) { ret := [ ] interface { } { } data := [ ] byte ( args [ 0 ] . ( string ) ) d := yaml . NewYAMLToJSONDecoder ( bytes . NewReader ( data ) ) for { var doc interface { } if err := d . Decode ( & doc ) ; err != nil { if err == io . EOF { break } return nil , err } ret = append ( ret , doc ) } return ret , nil } , } ) vm . NativeFunction ( & jsonnet . NativeFunction { Name : " " , Params : [ ] jsonnetAst . Identifier { " " , " " } , Func : func ( args [ ] interface { } ) ( res interface { } , err error ) { value := args [ 0 ] indent := int ( args [ 1 ] . ( float64 ) ) data , err := json . MarshalIndent ( value , " " , strings . Repeat ( " " , indent ) ) if err != nil { return " " , err } data = append ( data , byte ( '\n' ) ) return string ( data ) , nil } , } ) vm . NativeFunction ( & jsonnet . NativeFunction { Name : " " , Params : [ ] jsonnetAst . Identifier { " " } , Func : func ( args [ ] interface { } ) ( res interface { } , err error ) { value := args [ 0 ] output , err := goyaml . Marshal ( value ) return string ( output ) , err } , } ) vm . NativeFunction ( & jsonnet . NativeFunction { Name : " " , Params : [ ] jsonnetAst . Identifier { " " } , Func : func ( args [ ] interface { } ) ( res interface { } , err error ) { return resolveImage ( resolver , args [ 0 ] . ( string ) ) } , } ) vm . NativeFunction ( & jsonnet . NativeFunction { Name : " " , Params : [ ] jsonnetAst . Identifier { " " } , Func : func ( args [ ] interface { } ) ( res interface { } , err error ) { return regexp . QuoteMeta ( args [ 0 ] . ( string ) ) , nil } , } ) vm . NativeFunction ( & jsonnet . NativeFunction { Name : " " , Params : [ ] jsonnetAst . Identifier { " " , " " } , Func : func ( args [ ] interface { } ) ( res interface { } , err error ) { return regexp . MatchString ( args [ 0 ] . ( string ) , args [ 1 ] . ( string ) ) } , } ) vm . NativeFunction ( & jsonnet . NativeFunction { Name : " " , Params : [ ] jsonnetAst . Identifier { " " , " " , " " } , Func : func ( args [ ] interface { } ) ( res interface { } , err error ) { regex := args [ 0 ] . ( string ) src := args [ 1 ] . ( string ) repl := args [ 2 ] . ( string ) r , err := regexp . Compile ( regex ) if err != nil { return " " , err } return r . ReplaceAllString ( src , repl ) , nil } , } ) }
// RegisterNativeFuncs adds kubecfg's native jsonnet functions to provided VM
RegisterNativeFuncs adds kubecfg s native jsonnet functions to provided VM