diff --git a/lang/funcs/core/core.go b/lang/funcs/core/core.go index d962f616..5bda7995 100644 --- a/lang/funcs/core/core.go +++ b/lang/funcs/core/core.go @@ -21,6 +21,7 @@ import ( // import so the funcs register _ "github.com/purpleidea/mgmt/lang/funcs/core/datetime" _ "github.com/purpleidea/mgmt/lang/funcs/core/example" + _ "github.com/purpleidea/mgmt/lang/funcs/core/example/nested" _ "github.com/purpleidea/mgmt/lang/funcs/core/fmt" _ "github.com/purpleidea/mgmt/lang/funcs/core/math" _ "github.com/purpleidea/mgmt/lang/funcs/core/os" diff --git a/lang/funcs/core/example/nested/hello_func.go b/lang/funcs/core/example/nested/hello_func.go new file mode 100644 index 00000000..d097f5c8 --- /dev/null +++ b/lang/funcs/core/example/nested/hello_func.go @@ -0,0 +1,38 @@ +// Mgmt +// Copyright (C) 2013-2019+ James Shubin and the project contributors +// Written by James Shubin and the project contributors +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package corenested + +import ( + coreexample "github.com/purpleidea/mgmt/lang/funcs/core/example" + "github.com/purpleidea/mgmt/lang/funcs/simple" + "github.com/purpleidea/mgmt/lang/types" +) + +func init() { + simple.ModuleRegister(coreexample.ModuleName+"/"+ModuleName, "hello", &types.FuncValue{ + T: types.NewType("func() str"), + V: Hello, + }) +} + +// Hello returns some string. This is just to test nesting. +func Hello(input []types.Value) (types.Value, error) { + return &types.StrValue{ + V: "Hello!", + }, nil +} diff --git a/lang/funcs/core/example/nested/nested.go b/lang/funcs/core/example/nested/nested.go new file mode 100644 index 00000000..3611a5d4 --- /dev/null +++ b/lang/funcs/core/example/nested/nested.go @@ -0,0 +1,23 @@ +// Mgmt +// Copyright (C) 2013-2019+ James Shubin and the project contributors +// Written by James Shubin and the project contributors +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package corenested + +const ( + // ModuleName is the prefix given to all the functions in this module. + ModuleName = "nested" +) diff --git a/lang/funcs/core/template_func.go b/lang/funcs/core/template_func.go index 09c3d0e5..9c40fc85 100644 --- a/lang/funcs/core/template_func.go +++ b/lang/funcs/core/template_func.go @@ -333,6 +333,7 @@ func safename(name string) string { // TODO: should we pick a different replacement char? char := funcs.ReplaceChar // can't be any of: .-# result := strings.Replace(name, funcs.ModuleSep, char, -1) + result = strings.Replace(result, "/", char, -1) // nested imports if result == name { // No change, so add a prefix for package-less functions... This // prevents conflicts from sys.func1 -> sys_func1 which would be diff --git a/lang/interpret_test/TestAstFunc2/nested-import0.output b/lang/interpret_test/TestAstFunc2/nested-import0.output new file mode 100644 index 00000000..8cc700c7 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/nested-import0.output @@ -0,0 +1 @@ +Vertex: test[Hello!] diff --git a/lang/interpret_test/TestAstFunc2/nested-import0/main.mcl b/lang/interpret_test/TestAstFunc2/nested-import0/main.mcl new file mode 100644 index 00000000..8d414dee --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/nested-import0/main.mcl @@ -0,0 +1,5 @@ +import "example/nested" + +$x = nested.hello() + +test $x {} diff --git a/lang/interpret_test/TestAstFunc2/nested-import1.output b/lang/interpret_test/TestAstFunc2/nested-import1.output new file mode 100644 index 00000000..8cc700c7 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/nested-import1.output @@ -0,0 +1 @@ +Vertex: test[Hello!] diff --git a/lang/interpret_test/TestAstFunc2/nested-import1/main.mcl b/lang/interpret_test/TestAstFunc2/nested-import1/main.mcl new file mode 100644 index 00000000..48ca5190 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/nested-import1/main.mcl @@ -0,0 +1,5 @@ +import "example/nested" as foo + +$x = foo.hello() + +test $x {} diff --git a/lang/structs.go b/lang/structs.go index 38b2f0b5..4f59dfd2 100644 --- a/lang/structs.go +++ b/lang/structs.go @@ -2072,9 +2072,9 @@ func (obj *StmtProg) importScope(info *interfaces.ImportData, scope *interfaces. // but recursive imports mean this is not always the active file... if info.IsSystem { // system imports are the exact name, eg "fmt" - systemScope, err := obj.importSystemScope(info.Alias) + systemScope, err := obj.importSystemScope(info.Name) if err != nil { - return nil, errwrap.Wrapf(err, "system import of `%s` failed", info.Alias) + return nil, errwrap.Wrapf(err, "system import of `%s` failed", info.Name) } return systemScope, nil }