From 75bafa4fd30a00e52fb69528272f2e435b1cb364 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Thu, 5 Jun 2025 22:45:26 -0400 Subject: [PATCH] mcl, docs: Use the less ambiguous form of the import Update the style guide as well! --- docs/style-guide.md | 9 +++++++++ examples/lang/concat.mcl | 4 ++-- examples/lang/join.mcl | 4 ++-- examples/lang/virt2.mcl | 4 ++-- lang/core/embedded/provisioner/top.mcl | 1 - .../interpret_test/TestAstFunc2/deploy-readfile0.txtar | 8 ++++---- .../interpret_test/TestAstFunc2/deploy-readfile1.txtar | 8 ++++---- .../interpret_test/TestAstFunc2/deploy-readfile2.txtar | 6 +++--- .../interpret_test/TestAstFunc2/deploy-readfile3.txtar | 4 ++-- modules/dhcp/main.mcl | 6 +++--- modules/shorewall/main.mcl | 10 +++++----- 11 files changed, 36 insertions(+), 28 deletions(-) diff --git a/docs/style-guide.md b/docs/style-guide.md index f948bd19..3eaa735f 100644 --- a/docs/style-guide.md +++ b/docs/style-guide.md @@ -256,6 +256,15 @@ like: `import "https://github.com/purpleidea/mgmt-banana/"` and namespace it as `import "https://github.com/purpleidea/mgmt-banana/" as tomato` or something similar. +### Imports + +When importing "golang" modules such as "golang/strings" it's recommended to use +the `import "golang/strings" as golang_strings` format. This is to avoid +confusion with the normal core package you get from `import "strings"`. + +In the long-term, we expect to remove the `"golang/"` namespace when our own +standard library is complete enough. + ### Licensing We believe that sharing code helps reduce unnecessary re-invention, so that we diff --git a/examples/lang/concat.mcl b/examples/lang/concat.mcl index 5cda0889..13a9726e 100644 --- a/examples/lang/concat.mcl +++ b/examples/lang/concat.mcl @@ -1,4 +1,4 @@ -import "golang/strings" +import "golang/strings" as golang_strings import "list" $l1 = ["a", "b", "c", "d", "e", "f", "g",] @@ -10,7 +10,7 @@ $l6 = ["y", "zed",] $l = list.concat($l1, $l2, $l3, $l4, $l5, $l6) -$joined = strings.join($l, "-") +$joined = golang_strings.join($l, "-") print "alphabet" { msg => "${joined}", diff --git a/examples/lang/join.mcl b/examples/lang/join.mcl index 3bff3990..71011627 100644 --- a/examples/lang/join.mcl +++ b/examples/lang/join.mcl @@ -1,8 +1,8 @@ import "fmt" -import "golang/strings" +import "golang/strings" as golang_strings $l = ["a", "b", "c",] -$joined = strings.join($l, ", ") +$joined = golang_strings.join($l, ", ") print "debug" { msg => "${joined}", diff --git a/examples/lang/virt2.mcl b/examples/lang/virt2.mcl index 756c8f42..b0cae34f 100644 --- a/examples/lang/virt2.mcl +++ b/examples/lang/virt2.mcl @@ -2,10 +2,10 @@ import "fmt" import "os" -import "golang/strings" +import "golang/strings" as golang_strings import "example" -$input = example.str2int(strings.trim_space(os.readfile("/tmp/cpu-count"))) +$input = example.str2int(golang_strings.trim_space(os.readfile("/tmp/cpu-count"))) $count = if $input > 8 { 8 } else { diff --git a/lang/core/embedded/provisioner/top.mcl b/lang/core/embedded/provisioner/top.mcl index 5cdcf644..8a27e053 100644 --- a/lang/core/embedded/provisioner/top.mcl +++ b/lang/core/embedded/provisioner/top.mcl @@ -32,7 +32,6 @@ import "fmt" import "os" -import "golang/strings" import "embedded/provisioner" # embedded import # TODO: get all of the values first from the cli config file, and then a webui diff --git a/lang/interpret_test/TestAstFunc2/deploy-readfile0.txtar b/lang/interpret_test/TestAstFunc2/deploy-readfile0.txtar index 7fb9b45a..870f8d3c 100644 --- a/lang/interpret_test/TestAstFunc2/deploy-readfile0.txtar +++ b/lang/interpret_test/TestAstFunc2/deploy-readfile0.txtar @@ -1,7 +1,7 @@ -- metadata.yaml -- #files: "files/" # these are some extra files we can use (is the default) -- main.mcl -- -import "golang/strings" +import "golang/strings" as golang_strings import "deploy" import "second.mcl" import "mod1/" @@ -41,9 +41,9 @@ if $x3 != $mod1.x3 { } # hide the newlines from our output -test [strings.trim_space($x1),] {} -test [strings.trim_space($x2),] {} -test [strings.trim_space($x3),] {} +test [golang_strings.trim_space($x1),] {} +test [golang_strings.trim_space($x2),] {} +test [golang_strings.trim_space($x3),] {} # debugging: #test "f1" { # anotherstr => $x1, diff --git a/lang/interpret_test/TestAstFunc2/deploy-readfile1.txtar b/lang/interpret_test/TestAstFunc2/deploy-readfile1.txtar index 09e3399e..8e855848 100644 --- a/lang/interpret_test/TestAstFunc2/deploy-readfile1.txtar +++ b/lang/interpret_test/TestAstFunc2/deploy-readfile1.txtar @@ -1,7 +1,7 @@ -- metadata.yaml -- #files: "files/" # these are some extra files we can use (is the default) -- main.mcl -- -import "golang/strings" +import "golang/strings" as golang_strings import "deploy" import "second.mcl" import "mod1/" @@ -41,9 +41,9 @@ if $x3 != $mod1.x3 { } # hide the newlines from our output -test [strings.trim_space($x1),] {} -test [strings.trim_space($x2),] {} -test [strings.trim_space($x3),] {} +test [golang_strings.trim_space($x1),] {} +test [golang_strings.trim_space($x2),] {} +test [golang_strings.trim_space($x3),] {} # debugging: #test "f1" { # anotherstr => $x1, diff --git a/lang/interpret_test/TestAstFunc2/deploy-readfile2.txtar b/lang/interpret_test/TestAstFunc2/deploy-readfile2.txtar index 1834f0a7..1c523763 100644 --- a/lang/interpret_test/TestAstFunc2/deploy-readfile2.txtar +++ b/lang/interpret_test/TestAstFunc2/deploy-readfile2.txtar @@ -1,7 +1,7 @@ -- metadata.yaml -- #files: "files/" # these are some extra files we can use (is the default) -- main.mcl -- -import "golang/strings" +import "golang/strings" as golang_strings import "deploy" import "second.mcl" @@ -31,8 +31,8 @@ if $x2 != $second.x2 { } # hide the newlines from our output -test [strings.trim_space($x1),] {} -test [strings.trim_space($x2),] {} +test [golang_strings.trim_space($x1),] {} +test [golang_strings.trim_space($x2),] {} -- second.mcl -- import "deploy" diff --git a/lang/interpret_test/TestAstFunc2/deploy-readfile3.txtar b/lang/interpret_test/TestAstFunc2/deploy-readfile3.txtar index c6985304..5593e50e 100644 --- a/lang/interpret_test/TestAstFunc2/deploy-readfile3.txtar +++ b/lang/interpret_test/TestAstFunc2/deploy-readfile3.txtar @@ -1,7 +1,7 @@ -- metadata.yaml -- #files: "files/" # these are some extra files we can use (is the default) -- main.mcl -- -import "golang/strings" +import "golang/strings" as golang_strings import "deploy" $f1 = "/files/file1" @@ -19,7 +19,7 @@ if $x1 != deploy.readfile($f1) { } # hide the newlines from our output -test [strings.trim_space($x1),] {} +test [golang_strings.trim_space($x1),] {} -- files/file1 -- This is file1 in the files/ folder. -- OUTPUT -- diff --git a/modules/dhcp/main.mcl b/modules/dhcp/main.mcl index c080c18c..c18af7e2 100644 --- a/modules/dhcp/main.mcl +++ b/modules/dhcp/main.mcl @@ -31,7 +31,7 @@ import "deploy" import "fmt" import "golang" import "local" -import "golang/strings" +import "golang/strings" as golang_strings import "os" class server($st) { @@ -146,7 +146,7 @@ class server:subnet($name, $st) { netmask => "${netmask}", broadcast => "${broadcast}", router => "${router}", # TODO: support multiple - dns => strings.join($dns, ", "), + dns => golang_strings.join($dns, ", "), range => $valid_range, #nextserver => "${nextserver}", #filename => "${filename}", @@ -206,7 +206,7 @@ class server:host($name, $st) { $tmpl = struct{ name => "${name}", macaddress => "${macaddress}", - valid_fixedaddress => strings.join($fixedaddress, ","), + valid_fixedaddress => golang_strings.join($fixedaddress, ","), #fixedaddress => $fixedaddress, # TODO: when it's a list hostname => "${hostname}", nextserver => "${nextserver}", diff --git a/modules/shorewall/main.mcl b/modules/shorewall/main.mcl index b921d74d..3429a345 100644 --- a/modules/shorewall/main.mcl +++ b/modules/shorewall/main.mcl @@ -31,7 +31,7 @@ import "deploy" import "fmt" import "golang" import "local" -import "golang/strings" +import "golang/strings" as golang_strings # Class prepare adds some common things you probably want to run when using this @@ -199,7 +199,7 @@ class firewall:interface($name, $zone, $st) { print "interface: ${name}" {} include interface_base - $interface = $st->interface || (strings.to_upper($zone) + "_IF") # eg: NET_IF + $interface = $st->interface || (golang_strings.to_upper($zone) + "_IF") # eg: NET_IF $physical = $st->physical || $name $options []str = $st->options || [] # TODO: add option validation? $comment = $st->comment || "" @@ -328,14 +328,14 @@ class firewall:rule($name, $st) { #$original = $st->original || [] # TODO $comment = $st->comment || "" - $source_ips_joined = strings.join($source_ips, ",") + $source_ips_joined = golang_strings.join($source_ips, ",") $valid_source = if $source_ips_joined == "" { "${source}" } else { "${source}:${source_ips_joined}" } - $dest_ips_joined = strings.join($dest_ips, ",") + $dest_ips_joined = golang_strings.join($dest_ips, ",") $valid_dest = if $dest_ips_joined == "" { "${dest}" } else { @@ -506,7 +506,7 @@ class firewall:snat($name, $st) { $port = $st->port || 0 $comment = $st->comment || "" - $valid_source = strings.join($source, ",") + $valid_source = golang_strings.join($source, ",") $valid_proto = if $proto == "" { "-"