Skip to content

Commit 661a159

Browse files
authored
Merge pull request #3289 from crazy-max/bake-stdlib-description
bake: set missing stdlib functions description
2 parents 9a07004 + ddec767 commit 661a159

File tree

3 files changed

+61
-41
lines changed

3 files changed

+61
-41
lines changed

bake/hclparser/stdlib.go

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,36 @@ import (
1818
)
1919

2020
type funcDef struct {
21-
name string
22-
fn function.Function
23-
factory func() function.Function
21+
name string
22+
descriptionAlt string
23+
fn function.Function
24+
factory func() function.Function
2425
}
2526

2627
var stdlibFunctions = []funcDef{
2728
{name: "absolute", fn: stdlib.AbsoluteFunc},
2829
{name: "add", fn: stdlib.AddFunc},
2930
{name: "and", fn: stdlib.AndFunc},
30-
{name: "base64decode", fn: encoding.Base64DecodeFunc},
31-
{name: "base64encode", fn: encoding.Base64EncodeFunc},
31+
{name: "base64decode", fn: encoding.Base64DecodeFunc, descriptionAlt: `Decodes a string containing a base64 sequence.`},
32+
{name: "base64encode", fn: encoding.Base64EncodeFunc, descriptionAlt: `Encodes a string to a base64 sequence.`},
3233
{name: "basename", factory: basenameFunc},
33-
{name: "bcrypt", fn: crypto.BcryptFunc},
34+
{name: "bcrypt", fn: crypto.BcryptFunc, descriptionAlt: `Computes a hash of the given string using the Blowfish cipher.`},
3435
{name: "byteslen", fn: stdlib.BytesLenFunc},
3536
{name: "bytesslice", fn: stdlib.BytesSliceFunc},
36-
{name: "can", fn: tryfunc.CanFunc},
37+
{name: "can", fn: tryfunc.CanFunc, descriptionAlt: `Tries to evaluate the expression given in its first argument.`},
3738
{name: "ceil", fn: stdlib.CeilFunc},
3839
{name: "chomp", fn: stdlib.ChompFunc},
3940
{name: "chunklist", fn: stdlib.ChunklistFunc},
40-
{name: "cidrhost", fn: cidr.HostFunc},
41-
{name: "cidrnetmask", fn: cidr.NetmaskFunc},
42-
{name: "cidrsubnet", fn: cidr.SubnetFunc},
43-
{name: "cidrsubnets", fn: cidr.SubnetsFunc},
41+
{name: "cidrhost", fn: cidr.HostFunc, descriptionAlt: `Calculates a full host IP address within a given IP network address prefix.`},
42+
{name: "cidrnetmask", fn: cidr.NetmaskFunc, descriptionAlt: `Converts an IPv4 address prefix given in CIDR notation into a subnet mask address.`},
43+
{name: "cidrsubnet", fn: cidr.SubnetFunc, descriptionAlt: `Calculates a subnet address within a given IP network address prefix.`},
44+
{name: "cidrsubnets", fn: cidr.SubnetsFunc, descriptionAlt: `Calculates many consecutive subnet addresses at once, rather than just a single subnet extension.`},
4445
{name: "coalesce", fn: stdlib.CoalesceFunc},
4546
{name: "coalescelist", fn: stdlib.CoalesceListFunc},
4647
{name: "compact", fn: stdlib.CompactFunc},
4748
{name: "concat", fn: stdlib.ConcatFunc},
4849
{name: "contains", fn: stdlib.ContainsFunc},
49-
{name: "convert", fn: typeexpr.ConvertFunc},
50+
{name: "convert", fn: typeexpr.ConvertFunc, descriptionAlt: `Converts a value to a specified type constraint, using HCL's customdecode extension for type expression support.`},
5051
{name: "csvdecode", fn: stdlib.CSVDecodeFunc},
5152
{name: "dirname", factory: dirnameFunc},
5253
{name: "distinct", fn: stdlib.DistinctFunc},
@@ -76,7 +77,7 @@ var stdlibFunctions = []funcDef{
7677
{name: "lookup", fn: stdlib.LookupFunc},
7778
{name: "lower", fn: stdlib.LowerFunc},
7879
{name: "max", fn: stdlib.MaxFunc},
79-
{name: "md5", fn: crypto.Md5Func},
80+
{name: "md5", fn: crypto.Md5Func, descriptionAlt: `Computes the MD5 hash of a given string and encodes it with hexadecimal digits.`},
8081
{name: "merge", fn: stdlib.MergeFunc},
8182
{name: "min", fn: stdlib.MinFunc},
8283
{name: "modulo", fn: stdlib.ModuloFunc},
@@ -94,17 +95,17 @@ var stdlibFunctions = []funcDef{
9495
{name: "replace", fn: stdlib.ReplaceFunc},
9596
{name: "reverse", fn: stdlib.ReverseFunc},
9697
{name: "reverselist", fn: stdlib.ReverseListFunc},
97-
{name: "rsadecrypt", fn: crypto.RsaDecryptFunc},
98+
{name: "rsadecrypt", fn: crypto.RsaDecryptFunc, descriptionAlt: `Decrypts an RSA-encrypted ciphertext.`},
9899
{name: "sanitize", factory: sanitizeFunc},
99100
{name: "sethaselement", fn: stdlib.SetHasElementFunc},
100101
{name: "setintersection", fn: stdlib.SetIntersectionFunc},
101102
{name: "setproduct", fn: stdlib.SetProductFunc},
102103
{name: "setsubtract", fn: stdlib.SetSubtractFunc},
103104
{name: "setsymmetricdifference", fn: stdlib.SetSymmetricDifferenceFunc},
104105
{name: "setunion", fn: stdlib.SetUnionFunc},
105-
{name: "sha1", fn: crypto.Sha1Func},
106-
{name: "sha256", fn: crypto.Sha256Func},
107-
{name: "sha512", fn: crypto.Sha512Func},
106+
{name: "sha1", fn: crypto.Sha1Func, descriptionAlt: `Computes the SHA1 hash of a given string and encodes it with hexadecimal digits.`},
107+
{name: "sha256", fn: crypto.Sha256Func, descriptionAlt: `Computes the SHA256 hash of a given string and encodes it with hexadecimal digits.`},
108+
{name: "sha512", fn: crypto.Sha512Func, descriptionAlt: `Computes the SHA512 hash of a given string and encodes it with hexadecimal digits.`},
108109
{name: "signum", fn: stdlib.SignumFunc},
109110
{name: "slice", fn: stdlib.SliceFunc},
110111
{name: "sort", fn: stdlib.SortFunc},
@@ -119,11 +120,11 @@ var stdlibFunctions = []funcDef{
119120
{name: "trimprefix", fn: stdlib.TrimPrefixFunc},
120121
{name: "trimspace", fn: stdlib.TrimSpaceFunc},
121122
{name: "trimsuffix", fn: stdlib.TrimSuffixFunc},
122-
{name: "try", fn: tryfunc.TryFunc},
123+
{name: "try", fn: tryfunc.TryFunc, descriptionAlt: `Variadic function that tries to evaluate all of is arguments in sequence until one succeeds, in which case it returns that result, or returns an error if none of them succeed.`},
123124
{name: "upper", fn: stdlib.UpperFunc},
124-
{name: "urlencode", fn: encoding.URLEncodeFunc},
125-
{name: "uuidv4", fn: uuid.V4Func},
126-
{name: "uuidv5", fn: uuid.V5Func},
125+
{name: "urlencode", fn: encoding.URLEncodeFunc, descriptionAlt: `Applies URL encoding to a given string.`},
126+
{name: "uuidv4", fn: uuid.V4Func, descriptionAlt: `Generates and returns a Type-4 UUID in the standard hexadecimal string format.`},
127+
{name: "uuidv5", fn: uuid.V5Func, descriptionAlt: `Generates and returns a Type-5 UUID in the standard hexadecimal string format.`},
127128
{name: "values", fn: stdlib.ValuesFunc},
128129
{name: "zipmap", fn: stdlib.ZipmapFunc},
129130
}
@@ -264,3 +265,19 @@ func Stdlib() map[string]function.Function {
264265
}
265266
return funcs
266267
}
268+
269+
func StdlibFuncDescription(name string) string {
270+
for _, v := range stdlibFunctions {
271+
if v.name != name {
272+
continue
273+
}
274+
if v.descriptionAlt != "" {
275+
return v.descriptionAlt
276+
}
277+
if v.factory != nil {
278+
return v.factory().Description()
279+
}
280+
return v.fn.Description()
281+
}
282+
return ""
283+
}

0 commit comments

Comments
 (0)