github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/types/types.go (about) 1 package types 2 3 import ( 4 "bytes" 5 "strings" 6 ) 7 8 // These are only a list of the base types. Others can be added via builtins or during runtime. However their 9 // behavior will default to string (str). 10 const ( 11 Generic = "*" 12 Null = "null" 13 Die = "die" 14 Boolean = "bool" 15 String = "str" 16 Columns = "columns" 17 Binary = "bin" 18 CodeBlock = "block" 19 Json = "json" 20 JsonLines = "jsonl" 21 Number = "num" 22 Integer = "int" 23 Float = "float" 24 Path = "path" 25 Paths = "paths" 26 ) 27 28 // TrueString is `true` boolean value 29 const TrueString = "true" 30 31 // FalseString is `false` boolean value 32 const FalseString = "false" 33 34 // TrueByte is `true` as a []byte slice 35 var TrueByte = []byte(TrueString) 36 37 // FalseByte is `false` as a []byte slice 38 var FalseByte = []byte(FalseString) 39 40 // IsTrue checks if a process has returned a `true` state. 41 // This will check a few conditions as not every external process will return a non-zero exit number on a failure. 42 func IsTrue(stdout []byte, exitNum int) bool { 43 return IsTrueString(string(stdout), exitNum) 44 } 45 46 func IsTrueString(stdout string, exitNum int) bool { 47 switch { 48 case exitNum > 0: 49 return false 50 51 case exitNum < 0: 52 return true 53 54 default: 55 s := strings.ToLower(strings.TrimSpace(stdout)) 56 if len(s) == 0 || s == "null" || s == "0" || s == "false" || s == "no" || s == "off" || s == "fail" || s == "failed" || s == "disabled" { 57 return false 58 } 59 60 return true 61 } 62 } 63 64 // IsBlock checks if the []byte slice is a code or JSON block 65 func IsBlock(b []byte) bool { 66 b = bytes.TrimSpace(b) 67 if len(b) < 2 { 68 return false 69 } 70 71 if b[0] == '{' && b[len(b)-1] == '}' { 72 return true 73 } 74 75 return false 76 } 77 78 // IsBlockRune checks if the []rune slice is a code or JSON block 79 func IsBlockRune(r []rune) bool { 80 r = trimSpaceRune(r) 81 if len(r) < 2 { 82 return false 83 } 84 85 if r[0] == '{' && r[len(r)-1] == '}' { 86 return true 87 } 88 89 return false 90 } 91 92 func trimSpaceRune(r []rune) []rune { 93 if len(r) == 0 { 94 return []rune{} 95 } 96 97 for { 98 l := len(r) - 1 99 100 if r[l] == ' ' || r[l] == '\t' || r[l] == '\r' || r[l] == '\n' { 101 if l == 0 { 102 return []rune{} 103 } 104 r = r[:l] 105 } else { 106 break 107 } 108 } 109 110 for { 111 if r[0] == ' ' || r[0] == '\t' || r[0] == '\r' || r[0] == '\n' { 112 if len(r) == 1 { 113 return []rune{} 114 } 115 r = r[1:] 116 } else { 117 break 118 } 119 } 120 121 return r 122 }