github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/actions/lua/encoding/hex/hex.go (about)

     1  package hex
     2  
     3  import (
     4  	"encoding/hex"
     5  
     6  	"github.com/Shopify/go-lua"
     7  )
     8  
     9  func Open(l *lua.State) {
    10  	open := func(l *lua.State) int {
    11  		lua.NewLibrary(l, library)
    12  		return 1
    13  	}
    14  	lua.Require(l, "encoding/hex", open, false)
    15  	l.Pop(1)
    16  }
    17  
    18  var library = []lua.RegistryFunction{
    19  	{Name: "encode", Function: encode},
    20  	{Name: "decode", Function: decode},
    21  }
    22  
    23  func encode(l *lua.State) int {
    24  	value := lua.CheckString(l, 1)
    25  	encoded := hex.EncodeToString([]byte(value))
    26  
    27  	l.PushString(encoded)
    28  	return 1
    29  }
    30  
    31  func decode(l *lua.State) int {
    32  	value := lua.CheckString(l, 1)
    33  	decoded, err := hex.DecodeString(value)
    34  	if err != nil {
    35  		lua.Errorf(l, "%s", err.Error())
    36  		panic("unreachable")
    37  	}
    38  
    39  	l.PushString(string(decoded))
    40  	return 1
    41  }