1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 23:11:27 +00:00
Beego/orm/utils.go

279 lines
6.0 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-07-30 12:32:38 +00:00
package orm
import (
"fmt"
"math/big"
"reflect"
2013-07-30 12:32:38 +00:00
"strconv"
"strings"
"time"
)
2015-09-12 13:46:43 +00:00
// StrTo is the target string
2013-07-30 12:32:38 +00:00
type StrTo string
2015-09-12 13:46:43 +00:00
// Set string
2013-07-30 12:32:38 +00:00
func (f *StrTo) Set(v string) {
if v != "" {
*f = StrTo(v)
} else {
f.Clear()
}
}
2015-09-12 13:46:43 +00:00
// Clear string
2013-07-30 12:32:38 +00:00
func (f *StrTo) Clear() {
*f = StrTo(0x1E)
}
2015-09-12 13:46:43 +00:00
// Exist check string exist
2013-07-30 12:32:38 +00:00
func (f StrTo) Exist() bool {
return string(f) != string(0x1E)
}
2015-09-12 13:46:43 +00:00
// Bool string to bool
2013-07-30 12:32:38 +00:00
func (f StrTo) Bool() (bool, error) {
return strconv.ParseBool(f.String())
}
2015-09-12 13:46:43 +00:00
// Float32 string to float32
2013-07-30 12:32:38 +00:00
func (f StrTo) Float32() (float32, error) {
v, err := strconv.ParseFloat(f.String(), 32)
return float32(v), err
}
2015-09-12 13:46:43 +00:00
// Float64 string to float64
2013-07-30 12:32:38 +00:00
func (f StrTo) Float64() (float64, error) {
return strconv.ParseFloat(f.String(), 64)
}
2015-09-12 13:46:43 +00:00
// Int string to int
func (f StrTo) Int() (int, error) {
v, err := strconv.ParseInt(f.String(), 10, 32)
return int(v), err
}
2015-09-12 13:46:43 +00:00
// Int8 string to int8
func (f StrTo) Int8() (int8, error) {
v, err := strconv.ParseInt(f.String(), 10, 8)
return int8(v), err
}
2015-09-12 13:46:43 +00:00
// Int16 string to int16
2013-07-30 12:32:38 +00:00
func (f StrTo) Int16() (int16, error) {
v, err := strconv.ParseInt(f.String(), 10, 16)
return int16(v), err
}
2015-09-12 13:46:43 +00:00
// Int32 string to int32
2013-07-30 12:32:38 +00:00
func (f StrTo) Int32() (int32, error) {
v, err := strconv.ParseInt(f.String(), 10, 32)
return int32(v), err
}
2015-09-12 13:46:43 +00:00
// Int64 string to int64
2013-07-30 12:32:38 +00:00
func (f StrTo) Int64() (int64, error) {
v, err := strconv.ParseInt(f.String(), 10, 64)
if err != nil {
i := new(big.Int)
ni, ok := i.SetString(f.String(), 10) // octal
if !ok {
2017-03-17 17:45:30 +00:00
return v, err
}
return ni.Int64(), nil
}
2017-03-17 17:45:30 +00:00
return v, err
2013-07-30 12:32:38 +00:00
}
2015-09-12 13:46:43 +00:00
// Uint string to uint
func (f StrTo) Uint() (uint, error) {
v, err := strconv.ParseUint(f.String(), 10, 32)
return uint(v), err
}
2015-09-12 13:46:43 +00:00
// Uint8 string to uint8
func (f StrTo) Uint8() (uint8, error) {
v, err := strconv.ParseUint(f.String(), 10, 8)
return uint8(v), err
}
2015-09-12 13:46:43 +00:00
// Uint16 string to uint16
2013-07-30 12:32:38 +00:00
func (f StrTo) Uint16() (uint16, error) {
v, err := strconv.ParseUint(f.String(), 10, 16)
return uint16(v), err
}
2015-09-12 13:46:43 +00:00
// Uint32 string to uint31
2013-07-30 12:32:38 +00:00
func (f StrTo) Uint32() (uint32, error) {
v, err := strconv.ParseUint(f.String(), 10, 32)
return uint32(v), err
}
2015-09-12 13:46:43 +00:00
// Uint64 string to uint64
2013-07-30 12:32:38 +00:00
func (f StrTo) Uint64() (uint64, error) {
v, err := strconv.ParseUint(f.String(), 10, 64)
if err != nil {
i := new(big.Int)
ni, ok := i.SetString(f.String(), 10)
if !ok {
2017-03-17 17:45:30 +00:00
return v, err
}
return ni.Uint64(), nil
}
2017-03-17 17:45:30 +00:00
return v, err
2013-07-30 12:32:38 +00:00
}
2015-09-12 13:46:43 +00:00
// String string to string
2013-07-30 12:32:38 +00:00
func (f StrTo) String() string {
if f.Exist() {
return string(f)
}
return ""
}
2015-09-12 13:46:43 +00:00
// ToStr interface to string
2013-07-30 12:32:38 +00:00
func ToStr(value interface{}, args ...int) (s string) {
switch v := value.(type) {
case bool:
s = strconv.FormatBool(v)
case float32:
s = strconv.FormatFloat(float64(v), 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 32))
case float64:
s = strconv.FormatFloat(v, 'f', argInt(args).Get(0, -1), argInt(args).Get(1, 64))
case int:
s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
case int8:
s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
2013-07-30 12:32:38 +00:00
case int16:
s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
case int32:
s = strconv.FormatInt(int64(v), argInt(args).Get(0, 10))
case int64:
s = strconv.FormatInt(v, argInt(args).Get(0, 10))
case uint:
s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
case uint8:
s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
2013-07-30 12:32:38 +00:00
case uint16:
s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
case uint32:
s = strconv.FormatUint(uint64(v), argInt(args).Get(0, 10))
case uint64:
s = strconv.FormatUint(v, argInt(args).Get(0, 10))
case string:
s = v
case []byte:
s = string(v)
2013-07-30 12:32:38 +00:00
default:
s = fmt.Sprintf("%v", v)
}
return s
}
2015-09-12 13:46:43 +00:00
// ToInt64 interface to int64
func ToInt64(value interface{}) (d int64) {
val := reflect.ValueOf(value)
switch value.(type) {
case int, int8, int16, int32, int64:
d = val.Int()
case uint, uint8, uint16, uint32, uint64:
d = int64(val.Uint())
default:
panic(fmt.Errorf("ToInt64 need numeric not `%T`", value))
}
return
}
// snake string, XxYy to xx_yy , XxYY to xx_yy
2013-07-30 12:32:38 +00:00
func snakeString(s string) string {
data := make([]byte, 0, len(s)*2)
2016-08-29 16:15:02 +00:00
j := false
2013-07-30 12:32:38 +00:00
num := len(s)
for i := 0; i < num; i++ {
d := s[i]
2016-08-29 16:15:02 +00:00
if i > 0 && d >= 'A' && d <= 'Z' && j {
data = append(data, '_')
}
if d != '_' {
j = true
2013-07-30 12:32:38 +00:00
}
data = append(data, d)
}
2015-03-20 05:29:01 +00:00
return strings.ToLower(string(data[:]))
2013-07-30 12:32:38 +00:00
}
2014-01-17 15:28:54 +00:00
// camel string, xx_yy to XxYy
2013-07-30 12:32:38 +00:00
func camelString(s string) string {
data := make([]byte, 0, len(s))
flag, num := true, len(s)-1
2013-07-30 12:32:38 +00:00
for i := 0; i <= num; i++ {
d := s[i]
if d == '_' {
flag = true
2013-07-30 12:32:38 +00:00
continue
2017-03-17 17:24:45 +00:00
} else if flag {
if d >= 'a' && d <= 'z' {
d = d - 32
}
flag = false
2013-07-30 12:32:38 +00:00
}
data = append(data, d)
}
2015-03-20 05:29:01 +00:00
return string(data[:])
2013-07-30 12:32:38 +00:00
}
type argString []string
2014-01-17 15:28:54 +00:00
// get string by index from string slice
2013-07-30 12:32:38 +00:00
func (a argString) Get(i int, args ...string) (r string) {
if i >= 0 && i < len(a) {
r = a[i]
} else if len(args) > 0 {
r = args[0]
}
return
}
type argInt []int
2014-01-17 15:28:54 +00:00
// get int by index from int slice
2013-07-30 12:32:38 +00:00
func (a argInt) Get(i int, args ...int) (r int) {
if i >= 0 && i < len(a) {
r = a[i]
}
if len(args) > 0 {
r = args[0]
}
return
}
2014-01-17 15:28:54 +00:00
// parse time to string with location
2013-07-30 12:32:38 +00:00
func timeParse(dateString, format string) (time.Time, error) {
tp, err := time.ParseInLocation(format, dateString, DefaultTimeLoc)
return tp, err
}
2014-01-17 15:28:54 +00:00
// get pointer indirect type
2013-11-24 06:26:32 +00:00
func indirectType(v reflect.Type) reflect.Type {
switch v.Kind() {
case reflect.Ptr:
return indirectType(v.Elem())
default:
return v
}
}