Created
April 18, 2025 03:29
-
-
Save mtbarr/88007829afe6c808fd2d0602c9608c25 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package parser | |
| import "regexp" | |
| var ( | |
| onlyDigits = regexp.MustCompile(`^\d+$`) | |
| onlyZeroOrOne = regexp.MustCompile(`^[01]$`) | |
| signedInteger = regexp.MustCompile(`^-?\d+$`) | |
| floatNumber = regexp.MustCompile(`^-?\d+(\.\d+)?$`) | |
| createUnsignedIntParser = func(max int64) DataTypeParserAdapter { | |
| return *createIntSubTypeParser(onlyDigits, 0, int(max)) | |
| } | |
| createFloatParser = func(min, max float64) DataTypeParserAdapter { | |
| return *createNumericalSubTypeParser(floatNumber, min, max) | |
| } | |
| createIntRangeParser = func(min, max float64) DataTypeParserAdapter { | |
| return *createNumericalSubTypeParser(signedInteger, min, max) | |
| } | |
| createDigitRangeParser = func(min, max float64) DataTypeParserAdapter { | |
| return *createNumericalSubTypeParser(onlyDigits, min, max) | |
| } | |
| createBitParser = func() DataTypeParserAdapter { | |
| return *createNumericalSubTypeParser(onlyZeroOrOne, 0, 1) | |
| } | |
| ) | |
| var dataTypes = &map[string]DataTypeParserAdapter{ | |
| "TINYINT": createUnsignedIntParser(255), | |
| "SMALLINT": createUnsignedIntParser(65535), | |
| "MEDIUMINT": createUnsignedIntParser(16777215), | |
| "INT": createIntRangeParser(-2147483648, 2147483647), | |
| "BIGINT": createIntRangeParser(-9223372036854775808, 9223372036854775807), | |
| "FLOAT": createFloatParser(-3.4028235e+38, 3.4028235e+38), | |
| "DOUBLE": createFloatParser(-1.7976931348623157e+308, 1.7976931348623157e+308), | |
| "DECIMAL": createFloatParser(-1.7976931348623157e+308, 1.7976931348623157e+308), | |
| "BIT": createBitParser(), | |
| "BOOLEAN": createBitParser(), | |
| "CHAR": createDigitRangeParser(0, 255), | |
| "VARCHAR": createDigitRangeParser(0, 65535), | |
| "BINARY": createDigitRangeParser(0, 255), | |
| "VARBINARY": createDigitRangeParser(0, 65535), | |
| "TINYBLOB": createDigitRangeParser(0, 255), | |
| "BLOB": createDigitRangeParser(0, 65535), | |
| "MEDIUMBLOB": createDigitRangeParser(0, 16777215), | |
| "LONGBLOB": createDigitRangeParser(0, 4294967295), | |
| "TINYTEXT": createDigitRangeParser(0, 255), | |
| "TEXT": createDigitRangeParser(0, 65535), | |
| "MEDIUMTEXT": createDigitRangeParser(0, 16777215), | |
| "LONGTEXT": createDigitRangeParser(0, 4294967295), | |
| } | |
| type MySQLDataTypeParser struct { | |
| adapterMap *map[string]DataTypeParserAdapter | |
| } | |
| func NewMySQLDataTypeParser() *MySQLDataTypeParser { | |
| return &MySQLDataTypeParser{ | |
| adapterMap: dataTypes, | |
| } | |
| } | |
| func createNumericalSubTypeParser(exp *regexp.Regexp, minValue float64, maxValue float64) *DataTypeParserAdapter { | |
| return &DataTypeParserAdapter{ | |
| Regexp: exp, | |
| Configuration: &map[string]any{ | |
| "minValue": minValue, | |
| "maxValue": maxValue, | |
| }, | |
| } | |
| } | |
| func createIntSubTypeParser(exp *regexp.Regexp, minValue int, maxValue int) *DataTypeParserAdapter { | |
| return &DataTypeParserAdapter{ | |
| Regexp: exp, | |
| Configuration: &map[string]any{ | |
| "minValue": minValue, | |
| "maxValue": maxValue, | |
| }, | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment