diff --git a/csvreader.go b/csvreader.go deleted file mode 100644 index 006f131..0000000 --- a/csvreader.go +++ /dev/null @@ -1,63 +0,0 @@ -package q5 - -import "io" -import "os" -import "bufio" -import "strings" - -type CsvReader struct { - columns map[string]int32 - values []string - - currLine int - lines []string -} - -func (this *CsvReader) Load(fileName string) bool { - this.columns = map[string]int32{} - this.values = []string{} - this.currLine = 0 - this.lines = []string{} - - f, err := os.Open(fileName) - if err == nil { - defer f.Close() - - br := bufio.NewReader(f) - for { - line, _, c := br.ReadLine() - if c == io.EOF { - break - } - this.lines = append(this.lines, string(line)) - } - } - return err == nil -} - -func (this *CsvReader) NextLine() bool { - if this.currLine >= len(this.lines) { - return false - } - this.values = strings.Split(this.lines[this.currLine], ",") - this.currLine++ - return true -} - -func (this *CsvReader) GetValue(fieldName string) *XValue { - index, ok := this.columns[fieldName] - if ok { - if index >= 0 && index < int32(len(this.values)) { - return (&XValue{}).SetString(this.values[index]) - } else { - return &XValue{} - } - } else { - return &XValue{} - } -} - -func (this *CsvReader) KeyExists(fieldName string) bool { - _, ok := this.columns[fieldName] - return ok -} diff --git a/mutable_xobject.go b/mutable_xobject.go new file mode 100644 index 0000000..be20a95 --- /dev/null +++ b/mutable_xobject.go @@ -0,0 +1,25 @@ +package q5 + +type MutableXObject struct { + XObject +} + +func (this *MutableXObject) PushXValue(val *XValue) *MutableXObject{ + return this +} + +func (this *MutableXObject) PushMutableXObject(val MutableXObject) *MutableXObject{ + return this +} + +func (this *MutableXObject) SetXValue(key string, val *XValue) *MutableXObject{ + return this +} + +func (this *MutableXObject) SetXObject(key string, val *XObject) *MutableXObject{ + return this +} + +func (this *MutableXObject) SetMutableXObject(key string, val *MutableXObject) *MutableXObject{ + return this +} diff --git a/strutils.go b/strutils.go index 884aa50..fab4c2c 100644 --- a/strutils.go +++ b/strutils.go @@ -1,7 +1,9 @@ package q5 +import "io" import "crypto/md5" import "encoding/hex" +import "hash/crc32" func Test()(a string) { return "testb" @@ -12,3 +14,11 @@ func Md5Str(data string) string { h.Write([]byte(data)) return hex.EncodeToString(h.Sum(nil)) } + +func Crc32(data string) uint32 { + ieee := crc32.NewIEEE() + io.WriteString(ieee, data) + + code := ieee.Sum32() + return code +} diff --git a/sysutils.go b/sysutils.go index ccfaf29..9c9b345 100644 --- a/sysutils.go +++ b/sysutils.go @@ -1,5 +1,6 @@ package q5 +import "net" import "net/http" import "time" @@ -29,3 +30,20 @@ func Request(r *http.Request, name string) *XValue { } return v } + +func GetRequestRemoteAddr(r *http.Request) string { + remoteAddr := r.RemoteAddr + if ip := r.Header.Get("X-Real-Ip"); ip != "" { + remoteAddr = ip + } else if ip = r.Header.Get("X-Forwarded-For"); ip != "" { + remoteAddr = ip + } else { + remoteAddr, _, _ = net.SplitHostPort(remoteAddr) + } + + if remoteAddr == "::1" { + remoteAddr = "127.0.0.1" + } + + return remoteAddr +} diff --git a/xobject.go b/xobject.go index 1e595c0..2122b76 100644 --- a/xobject.go +++ b/xobject.go @@ -11,6 +11,61 @@ type XObject struct { _val interface{} } +func (this *XObject) Size(key interface{}) int32 { + return 0 +} + +func (this *XObject) Reset() { +} + +func (this *XObject) GetType() int8{ + return this._type +} + func (this *XObject) AsXValue() *XValue { return nil } + +func (this *XObject) At(key interface{}) *XObject { + return nil +} + +func (this *XObject) Get(key interface{}) *XValue { + return nil +} + +func (this *XObject) HasKey(key interface{}) bool { + return false +} + +func (this *XObject) ReadFromFile(fileName string) bool { + return false +} + +func (this *XObject) ReadFromJsonFile(fileName string) bool { + return false +} + +func (this *XObject) ReadFromJsonString(data string) bool { + return false +} + +func (this *XObject) ReadFromXmlFile(fileName string) bool { + return false +} + +func (this *XObject) ReadFromXmlString(data string) bool { + return false +} + +func (this *XObject) ReadFromUrlQueryString(data string) bool { + return false +} + +func (this *XObject) ToJsonStr() string { + return "" +} + +func (this *XObject) ToUrlEncodeSr() string { + return "" +} diff --git a/xvalue.go b/xvalue.go index 5dd2ec1..bf17cb9 100644 --- a/xvalue.go +++ b/xvalue.go @@ -5,7 +5,8 @@ import ( ) const ( - XVT_INT = 0 + XVT_UNDEFINED = 0 + XVT_INT = iota XVT_FLOAT = iota XVT_STRING = iota XVT_BYTES = iota @@ -19,7 +20,45 @@ type XValue struct } func (this *XValue) GetType() int8 { - return this._type + if this._type == XVT_UNDEFINED { + return XVT_INT + } else { + return this._type + } +} + +func (this *XValue) IsUndefined() bool { + return this._type == XVT_UNDEFINED +} + +func (this *XValue) SetUInt8(val uint8) *XValue { + this.SetInt64(int64(val)) + return this +} + +func (this *XValue) SetInt8(val int8) *XValue { + this.SetInt64(int64(val)) + return this +} + +func (this *XValue) SetUInt16(val uint16) *XValue { + this.SetInt64(int64(val)) + return this +} + +func (this *XValue) SetInt16(val int16) *XValue { + this.SetInt64(int64(val)) + return this +} + +func (this *XValue) SetUInt32(val uint32) *XValue { + this.SetInt64(int64(val)) + return this +} + +func (this *XValue) SetInt32(val int32) *XValue { + this.SetInt64(int64(val)) + return this } func (this *XValue) SetInt64(val int64) *XValue { @@ -29,6 +68,11 @@ func (this *XValue) SetInt64(val int64) *XValue { return this } +func (this *XValue) SetFloat32(val float32) *XValue { + this.SetFloat64(float64(val)) + return this +} + func (this *XValue) SetFloat64(val float64) *XValue { val_copy := val this._type = XVT_FLOAT @@ -37,7 +81,7 @@ func (this *XValue) SetFloat64(val float64) *XValue { } func (this *XValue) SetString(val string) *XValue { - val_copy := "" + val + val_copy := val + "" this._type = XVT_STRING this._val = &val_copy return this @@ -57,6 +101,26 @@ func (this *XValue) SetUserData(val interface{}) *XValue { return this } +func (this *XValue) GetUInt8() uint8 { + return uint8(this.GetInt64()) +} + +func (this *XValue) GetInt8() int8 { + return int8(this.GetInt64()) +} + +func (this *XValue) GetUInt16() uint16 { + return uint16(this.GetInt64()) +} + +func (this *XValue) GetInt16() int16 { + return int16(this.GetInt64()) +} + +func (this *XValue) GetUInt32() uint32 { + return uint32(this.GetInt64()) +} + func (this *XValue) GetInt32() int32 { return int32(this.GetInt64()) } @@ -84,11 +148,50 @@ func (this *XValue) GetInt64() int64 { } } +func (this *XValue) GetFloat32() float32 { + return float32(this.GetFloat64()) +} + +func (this *XValue) GetFloat64() float64 { + switch this._type { + case XVT_INT: + return float64(*(this._val.(*int64))) + case XVT_FLOAT: + return *(this._val.(*float64)) + case XVT_STRING, XVT_BYTES, XVT_USERDATA: + val, err := strconv.ParseFloat(this.GetString(), 64) + if err == nil { + return val + } else { + val, err := strconv.ParseInt(this.GetString(), 10, 64) + if err == nil { + return float64(val) + } else { + return 0 + } + } + default: + return 0 + } +} + func (this *XValue) GetString() string { switch this._type { + case XVT_INT: + return strconv.FormatInt(this.GetInt64(), 10) + case XVT_FLOAT: + return strconv.FormatFloat(this.GetFloat64(), 'E', -1, 64) case XVT_STRING: return *(this._val.(*string)) default: return "" } } + +func (this *XValue) GetUserData() interface{} { + if this._type == XVT_USERDATA { + return this._val + } else { + return nil + } +}