go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/lucicfg/strutil.go (about)

     1  // Copyright 2019 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package lucicfg
    16  
    17  import (
    18  	"encoding/base64"
    19  	"encoding/hex"
    20  	"fmt"
    21  
    22  	"go.starlark.net/starlark"
    23  	"gopkg.in/yaml.v2"
    24  
    25  	"go.chromium.org/luci/common/data/text/intsetexpr"
    26  )
    27  
    28  // See //internal/strutil.star for where these functions are referenced.
    29  func init() {
    30  	declNative("expand_int_set", func(call nativeCall) (starlark.Value, error) {
    31  		var s starlark.String
    32  		if err := call.unpack(1, &s); err != nil {
    33  			return nil, err
    34  		}
    35  		res, err := intsetexpr.Expand(s.GoString())
    36  		if err != nil {
    37  			return nil, fmt.Errorf("expand_int_set: %s", err)
    38  		}
    39  		out := make([]starlark.Value, len(res))
    40  		for i, r := range res {
    41  			out[i] = starlark.String(r)
    42  		}
    43  		return starlark.NewList(out), nil
    44  	})
    45  
    46  	declNative("json_to_yaml", func(call nativeCall) (starlark.Value, error) {
    47  		var json starlark.String
    48  		if err := call.unpack(1, &json); err != nil {
    49  			return nil, err
    50  		}
    51  		var buf any
    52  		if err := yaml.Unmarshal([]byte(json.GoString()), &buf); err != nil {
    53  			return nil, fmt.Errorf("failed to unmarshal JSON as YAML: %s", err)
    54  		}
    55  		out, err := yaml.Marshal(buf)
    56  		if err != nil {
    57  			return nil, err
    58  		}
    59  		return starlark.String(out), nil
    60  	})
    61  
    62  	declNative("b64_encode", func(call nativeCall) (starlark.Value, error) {
    63  		var s starlark.String
    64  		if err := call.unpack(1, &s); err != nil {
    65  			return nil, err
    66  		}
    67  		return starlark.String(base64.StdEncoding.EncodeToString([]byte(s.GoString()))), nil
    68  	})
    69  
    70  	declNative("b64_decode", func(call nativeCall) (starlark.Value, error) {
    71  		var s starlark.String
    72  		if err := call.unpack(1, &s); err != nil {
    73  			return nil, err
    74  		}
    75  		raw, err := base64.StdEncoding.DecodeString(s.GoString())
    76  		if err != nil {
    77  			return nil, err
    78  		}
    79  		return starlark.String(string(raw)), nil
    80  	})
    81  
    82  	declNative("hex_encode", func(call nativeCall) (starlark.Value, error) {
    83  		var s starlark.String
    84  		if err := call.unpack(1, &s); err != nil {
    85  			return nil, err
    86  		}
    87  		return starlark.String(hex.EncodeToString([]byte(s.GoString()))), nil
    88  	})
    89  
    90  	declNative("hex_decode", func(call nativeCall) (starlark.Value, error) {
    91  		var s starlark.String
    92  		if err := call.unpack(1, &s); err != nil {
    93  			return nil, err
    94  		}
    95  		raw, err := hex.DecodeString(s.GoString())
    96  		if err != nil {
    97  			return nil, err
    98  		}
    99  		return starlark.String(string(raw)), nil
   100  	})
   101  }