lang: funcs: core: Clean up functions with constants

This commit is contained in:
James Shubin
2023-06-29 19:29:16 -04:00
parent d37862b123
commit 7d73c7fca9
9 changed files with 60 additions and 32 deletions

View File

@@ -30,11 +30,12 @@ const (
// AbsPathFuncName is the name this function is registered as.
AbsPathFuncName = "abspath"
pathArg = "path"
// arg names...
absPathArgNamePath = "path"
)
func init() {
funcs.ModuleRegister(ModuleName, "abspath", func() interfaces.Func { return &AbsPathFunc{} }) // must register the func and name
funcs.ModuleRegister(ModuleName, AbsPathFuncName, func() interfaces.Func { return &AbsPathFunc{} }) // must register the func and name
}
// AbsPathFunc is a function that returns the absolute, full path in the deploy
@@ -65,7 +66,7 @@ func (obj *AbsPathFunc) SetData(data *interfaces.FuncData) {
// ArgGen returns the Nth arg name for this function.
func (obj *AbsPathFunc) ArgGen(index int) (string, error) {
seq := []string{pathArg}
seq := []string{absPathArgNamePath}
if l := len(seq); index >= l {
return "", fmt.Errorf("index %d exceeds arg length of %d", index, l)
}
@@ -83,7 +84,7 @@ func (obj *AbsPathFunc) Info() *interfaces.Info {
return &interfaces.Info{
Pure: false, // maybe false because the file contents can change
Memo: false,
Sig: types.NewType(fmt.Sprintf("func(%s str) str", pathArg)),
Sig: types.NewType(fmt.Sprintf("func(%s str) str", absPathArgNamePath)),
}
}
@@ -117,7 +118,7 @@ func (obj *AbsPathFunc) Stream() error {
}
obj.last = input // store for next
path := input.Struct()[pathArg].Str()
path := input.Struct()[absPathArgNamePath].Str()
// TODO: add validation for absolute path?
if obj.path != nil && *obj.path == path {
continue // nothing changed

View File

@@ -30,6 +30,9 @@ import (
const (
// ReadFileFuncName is the name this function is registered as.
ReadFileFuncName = "readfile"
// arg names...
readFileArgNameFilename = "filename"
)
func init() {
@@ -64,7 +67,7 @@ func (obj *ReadFileFunc) SetData(data *interfaces.FuncData) {
// ArgGen returns the Nth arg name for this function.
func (obj *ReadFileFunc) ArgGen(index int) (string, error) {
seq := []string{"filename"}
seq := []string{readFileArgNameFilename}
if l := len(seq); index >= l {
return "", fmt.Errorf("index %d exceeds arg length of %d", index, l)
}
@@ -82,7 +85,7 @@ func (obj *ReadFileFunc) Info() *interfaces.Info {
return &interfaces.Info{
Pure: false, // maybe false because the file contents can change
Memo: false,
Sig: types.NewType("func(filename str) str"),
Sig: types.NewType(fmt.Sprintf("func(%s str) str", readFileArgNameFilename)),
}
}
@@ -116,7 +119,7 @@ func (obj *ReadFileFunc) Stream() error {
}
obj.last = input // store for next
filename := input.Struct()["filename"].Str()
filename := input.Struct()[readFileArgNameFilename].Str()
// TODO: add validation for absolute path?
// TODO: add check for empty string
if obj.filename != nil && *obj.filename == filename {

View File

@@ -29,6 +29,9 @@ import (
const (
// ReadFileAbsFuncName is the name this function is registered as.
ReadFileAbsFuncName = "readfileabs"
// arg names...
readfileArgNameFilename = "filename"
)
func init() {
@@ -64,7 +67,7 @@ func (obj *ReadFileAbsFunc) SetData(data *interfaces.FuncData) {
// ArgGen returns the Nth arg name for this function.
func (obj *ReadFileAbsFunc) ArgGen(index int) (string, error) {
seq := []string{"filename"}
seq := []string{readfileArgNameFilename}
if l := len(seq); index >= l {
return "", fmt.Errorf("index %d exceeds arg length of %d", index, l)
}
@@ -82,7 +85,7 @@ func (obj *ReadFileAbsFunc) Info() *interfaces.Info {
return &interfaces.Info{
Pure: false, // maybe false because the file contents can change
Memo: false,
Sig: types.NewType("func(filename str) str"),
Sig: types.NewType(fmt.Sprintf("func(%s str) str", readfileArgNameFilename)),
}
}
@@ -116,7 +119,7 @@ func (obj *ReadFileAbsFunc) Stream() error {
}
obj.last = input // store for next
filename := input.Struct()["filename"].Str()
filename := input.Struct()[readfileArgNameFilename].Str()
// TODO: add validation for absolute path?
// TODO: add check for empty string
if obj.filename != nil && *obj.filename == filename {

View File

@@ -38,6 +38,11 @@ import (
const (
// VUMeterFuncName is the name this function is registered as.
VUMeterFuncName = "vumeter"
// arg names...
vuMeterArgNameSymbol = "symbol"
vuMeterArgNameMultiplier = "multiplier"
vuMeterArgNamePeak = "peak"
)
func init() {
@@ -66,7 +71,7 @@ func (obj *VUMeterFunc) String() string {
// ArgGen returns the Nth arg name for this function.
func (obj *VUMeterFunc) ArgGen(index int) (string, error) {
seq := []string{"symbol", "multiplier", "peak"}
seq := []string{vuMeterArgNameSymbol, vuMeterArgNameMultiplier, vuMeterArgNamePeak}
if l := len(seq); index >= l {
return "", fmt.Errorf("index %d exceeds arg length of %d", index, l)
}
@@ -121,7 +126,7 @@ func (obj *VUMeterFunc) Info() *interfaces.Info {
return &interfaces.Info{
Pure: true,
Memo: false,
Sig: types.NewType("func(symbol str, multiplier int, peak float) str"),
Sig: types.NewType(fmt.Sprintf("func(%s str, %s int, %s float) str", vuMeterArgNameSymbol, vuMeterArgNameMultiplier, vuMeterArgNamePeak)),
}
}
@@ -158,9 +163,9 @@ func (obj *VUMeterFunc) Stream() error {
}
obj.last = input // store for next
obj.symbol = input.Struct()["symbol"].Str()
obj.multiplier = input.Struct()["multiplier"].Int()
obj.peak = input.Struct()["peak"].Float()
obj.symbol = input.Struct()[vuMeterArgNameSymbol].Str()
obj.multiplier = input.Struct()[vuMeterArgNameMultiplier].Int()
obj.peak = input.Struct()[vuMeterArgNamePeak].Float()
once.Do(onceFunc)
continue // we must wrap around and go in through goChan

View File

@@ -32,6 +32,9 @@ import (
const (
// ReadFileFuncName is the name this function is registered as.
ReadFileFuncName = "readfile"
// arg names...
readFileArgNameFilename = "filename"
)
func init() {
@@ -64,7 +67,7 @@ func (obj *ReadFileFunc) String() string {
// ArgGen returns the Nth arg name for this function.
func (obj *ReadFileFunc) ArgGen(index int) (string, error) {
seq := []string{"filename"}
seq := []string{readFileArgNameFilename}
if l := len(seq); index >= l {
return "", fmt.Errorf("index %d exceeds arg length of %d", index, l)
}
@@ -82,7 +85,7 @@ func (obj *ReadFileFunc) Info() *interfaces.Info {
return &interfaces.Info{
Pure: false, // maybe false because the file contents can change
Memo: false,
Sig: types.NewType("func(filename str) str"),
Sig: types.NewType(fmt.Sprintf("func(%s str) str", readFileArgNameFilename)),
}
}
@@ -121,7 +124,7 @@ func (obj *ReadFileFunc) Stream() error {
}
obj.last = input // store for next
filename := input.Struct()["filename"].Str()
filename := input.Struct()[readFileArgNameFilename].Str()
// TODO: add validation for absolute path?
// TODO: add check for empty string
if obj.filename != nil && *obj.filename == filename {

View File

@@ -32,6 +32,9 @@ import (
const (
// SystemFuncName is the name this function is registered as.
SystemFuncName = "system"
// arg names...
systemArgNameCmd = "cmd"
)
func init() {
@@ -60,7 +63,7 @@ func (obj *SystemFunc) String() string {
// ArgGen returns the Nth arg name for this function.
func (obj *SystemFunc) ArgGen(index int) (string, error) {
seq := []string{"shell_command"}
seq := []string{systemArgNameCmd}
if l := len(seq); index >= l {
return "", fmt.Errorf("index %d exceeds arg length of %d", index, l)
}
@@ -78,7 +81,7 @@ func (obj *SystemFunc) Info() *interfaces.Info {
return &interfaces.Info{
Pure: false, // definitely false
Memo: false,
Sig: types.NewType("func(shell_command str) str"),
Sig: types.NewType(fmt.Sprintf("func(%s str) str", systemArgNameCmd)),
Err: obj.Validate(),
}
}
@@ -133,7 +136,7 @@ func (obj *SystemFunc) Stream() error {
return nil
}
}
shellCommand := input.Struct()["shell_command"].Str()
shellCommand := input.Struct()[systemArgNameCmd].Str()
// Kill the previous command, if any.
if obj.cancel != nil {

View File

@@ -32,6 +32,9 @@ const (
// Random1FuncName is the name this function is registered as.
Random1FuncName = "random1"
// arg names...
random1ArgNameLength = "length"
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
@@ -65,7 +68,7 @@ func (obj *Random1Func) String() string {
// ArgGen returns the Nth arg name for this function.
func (obj *Random1Func) ArgGen(index int) (string, error) {
seq := []string{"length"}
seq := []string{random1ArgNameLength}
if l := len(seq); index >= l {
return "", fmt.Errorf("index %d exceeds arg length of %d", index, l)
}
@@ -82,7 +85,7 @@ func (obj *Random1Func) Validate() error {
func (obj *Random1Func) Info() *interfaces.Info {
return &interfaces.Info{
Pure: false,
Sig: types.NewType("func(length int) str"),
Sig: types.NewType(fmt.Sprintf("func(%s int) str", random1ArgNameLength)),
Err: obj.Validate(),
}
}
@@ -139,7 +142,7 @@ func (obj *Random1Func) Stream() error {
return fmt.Errorf("you can only pass a single input to random")
}
length := input.Struct()["length"].Int()
length := input.Struct()[random1ArgNameLength].Int()
// TODO: if negative, randomly pick a length ?
if length < 0 {
return fmt.Errorf("can't generate a negative length")

View File

@@ -30,6 +30,10 @@ import (
const (
// ExchangeFuncName is the name this function is registered as.
ExchangeFuncName = "exchange"
// arg names...
exchangeArgNameNamespace = "namespace"
exchangeArgNameValue = "value"
)
func init() {
@@ -59,7 +63,7 @@ func (obj *ExchangeFunc) String() string {
// ArgGen returns the Nth arg name for this function.
func (obj *ExchangeFunc) ArgGen(index int) (string, error) {
seq := []string{"namespace", "value"}
seq := []string{exchangeArgNameNamespace, exchangeArgNameValue}
if l := len(seq); index >= l {
return "", fmt.Errorf("index %d exceeds arg length of %d", index, l)
}
@@ -80,7 +84,7 @@ func (obj *ExchangeFunc) Info() *interfaces.Info {
// TODO: do we want to allow this to be statically polymorphic,
// and have value be any type we might want?
// output is map of: hostname => value
Sig: types.NewType("func(namespace str, value str) map{str: str}"),
Sig: types.NewType(fmt.Sprintf("func(%s str, %s str) map{str: str}", exchangeArgNameNamespace, exchangeArgNameValue)),
Err: obj.Validate(),
}
}
@@ -117,7 +121,7 @@ func (obj *ExchangeFunc) Stream() error {
}
obj.last = input // store for next
namespace := input.Struct()["namespace"].Str()
namespace := input.Struct()[exchangeArgNameNamespace].Str()
if namespace == "" {
return fmt.Errorf("can't use an empty namespace")
}
@@ -139,7 +143,7 @@ func (obj *ExchangeFunc) Stream() error {
return fmt.Errorf("can't change namespace, previously: `%s`", obj.namespace)
}
value := input.Struct()["value"].Str()
value := input.Struct()[exchangeArgNameValue].Str()
if obj.init.Debug {
obj.init.Logf("value: %+v", value)
}

View File

@@ -30,6 +30,9 @@ import (
const (
// KVLookupFuncName is the name this function is registered as.
KVLookupFuncName = "kvlookup"
// arg names...
kvLookupArgNameNamespace = "namespace"
)
func init() {
@@ -59,7 +62,7 @@ func (obj *KVLookupFunc) String() string {
// ArgGen returns the Nth arg name for this function.
func (obj *KVLookupFunc) ArgGen(index int) (string, error) {
seq := []string{"namespace"}
seq := []string{kvLookupArgNameNamespace}
if l := len(seq); index >= l {
return "", fmt.Errorf("index %d exceeds arg length of %d", index, l)
}
@@ -78,7 +81,7 @@ func (obj *KVLookupFunc) Info() *interfaces.Info {
Pure: false, // definitely false
Memo: false,
// output is map of: hostname => value
Sig: types.NewType("func(namespace str) map{str: str}"),
Sig: types.NewType(fmt.Sprintf("func(%s str) map{str: str}", kvLookupArgNameNamespace)),
Err: obj.Validate(),
}
}
@@ -115,7 +118,7 @@ func (obj *KVLookupFunc) Stream() error {
}
obj.last = input // store for next
namespace := input.Struct()["namespace"].Str()
namespace := input.Struct()[kvLookupArgNameNamespace].Str()
if namespace == "" {
return fmt.Errorf("can't use an empty namespace")
}