github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/stdio/register.go (about) 1 package stdio 2 3 import ( 4 "context" 5 "fmt" 6 "sort" 7 8 "github.com/lmorg/murex/config" 9 ) 10 11 var pipes = make(map[string]func(string) (Io, error)) 12 13 // RegisterPipe is used by pipes (/builtins/) to regester themselves to murex. 14 // This function should only be called from a packages Init() func. 15 func RegisterPipe(name string, constructor func(string) (Io, error)) { 16 if pipes[name] != nil { 17 panic("Pipe already registered with the name: " + name) 18 } 19 20 pipes[name] = constructor 21 } 22 23 // CreatePipe returns an stdio.Io interface for a specified pipe type or errors if 24 // the pipe type is invalid. 25 func CreatePipe(pipeType, arguments string) (Io, error) { 26 if pipes[pipeType] == nil { 27 return nil, fmt.Errorf("`%s` is not a supported pipe type", pipeType) 28 } 29 30 return pipes[pipeType](arguments) 31 } 32 33 // DumpPipes returns a sorted array of regestered pipes. 34 func DumpPipes() (dump []string) { 35 for name := range pipes { 36 dump = append(dump, name) 37 } 38 39 sort.Strings(dump) 40 return 41 } 42 43 // RegisterReadArray is used by types (/builtins/types) to regester themselves to murex. 44 // This function should only be called from a packages Init() func. 45 func RegisterReadArray(dataType string, function func(ctx context.Context, read Io, callback func([]byte)) error) { 46 if readArray[dataType] != nil { 47 panic("readArray already registered for the data type: " + dataType) 48 } 49 50 readArray[dataType] = function 51 } 52 53 // RegisterReadArrayWithType is used by types (/builtins/types) to regester themselves to murex. 54 // This function should only be called from a packages Init() func. 55 func RegisterReadArrayWithType(dataType string, function func(ctx context.Context, read Io, callback func(interface{}, string)) error) { 56 if readArrayWithType[dataType] != nil { 57 panic("readArrayWithType already registered for the data type: " + dataType) 58 } 59 60 readArrayWithType[dataType] = function 61 } 62 63 // RegisterReadMap is used by pipes (/builtins/) to regester themselves to murex. 64 // This function should only be called from a packages Init() func. 65 func RegisterReadMap(dataType string, function func(read Io, config *config.Config, callback func(*Map)) error) { 66 if readMap[dataType] != nil { 67 panic("readMap already registered for the data type: " + dataType) 68 } 69 70 readMap[dataType] = function 71 } 72 73 // RegisterWriteArray is used by pipes (/builtins/) to regester themselves to murex. 74 // This function should only be called from a packages Init() func. 75 func RegisterWriteArray(dataType string, function func(read Io) (ArrayWriter, error)) { 76 if writeArray[dataType] != nil { 77 panic("writeArray already registered for the data type: " + dataType) 78 } 79 80 writeArray[dataType] = function 81 }