mirror of
https://gitee.com/johng/gf
synced 2026-06-11 03:41:44 +08:00
Compare commits
33 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bca5532df8 | |||
| 72eeadd9aa | |||
| 0af55794f6 | |||
| 25a6c53533 | |||
| 9f9172c775 | |||
| 320e0db417 | |||
| cb8362d447 | |||
| 45a83fc53c | |||
| 3411bd1c1d | |||
| 6ab0a77364 | |||
| 281bae4116 | |||
| 218c692fe0 | |||
| fa69b581e1 | |||
| bd0baceeca | |||
| e71c837472 | |||
| 782aaabd07 | |||
| 8ae9276732 | |||
| 79a3aa5916 | |||
| 733c5db228 | |||
| 6171c621a7 | |||
| 802568856c | |||
| 127fb67185 | |||
| 5db039bbce | |||
| 8a3365d18e | |||
| 2ae5b1a4f8 | |||
| f9515d7126 | |||
| b56679a97c | |||
| d1b123964a | |||
| 991f7c4958 | |||
| 770619c39e | |||
| 4ad5450b80 | |||
| 49ce7fe885 | |||
| e66f63262b |
@ -14,6 +14,7 @@ env:
|
||||
|
||||
services:
|
||||
- mysql
|
||||
- redis-server
|
||||
|
||||
addons:
|
||||
hosts:
|
||||
|
||||
@ -4,16 +4,11 @@
|
||||
[](https://godoc.org/github.com/gogf/gf)
|
||||
[](https://travis-ci.org/gogf/gf)
|
||||
[](https://goreportcard.com/report/github.com/gogf/gf)
|
||||
[](https://goframe.org)
|
||||
[](https://codecov.io/gh/gogf/gf/branch/master)
|
||||
[](https://github.com/gogf/gf)
|
||||
[](https://github.com/gogf/gf)
|
||||
[](https://github.com/gogf/gf/releases)
|
||||
|
||||
<!--
|
||||
[](https://codecov.io/gh/gogf/gf)
|
||||
[](https://www.codetriage.com/gogf/gf)
|
||||
-->
|
||||
|
||||
`GF(Go Frame)`是一款模块化、松耦合、生产级Go应用开发框架。提供了常用的核心开发组件,如:缓存、日志、文件、时间、队列、数组、集合、字符串、定时器、命令行、文件锁、内存锁、对象池、连接池、数据校验、数据编码、文件监控、定时任务、数据库ORM、TCP/UDP组件、进程管理/通信、
|
||||
并发安全容器等等。并提供了Web服务开发的系列核心组件,如:Router、Cookie、Session、服务注册、配置管理、模板引擎等等,支持热重启、热更新、多域名、多端口、多服务、HTTPS、Rewrite等特性。
|
||||
|
||||
|
||||
2
TODO.MD
2
TODO.MD
@ -57,7 +57,7 @@
|
||||
1. 更新跨域请求CORS相关功能文档;
|
||||
1. ghttp的热重启的本地进程端口监听,在不使用该特性时默认关闭掉;
|
||||
1. gcfg包目前允许添加重复的目录路径,需要在SetPath/AddPath时判断重复性,不能添加重复的路径;
|
||||
|
||||
1. gdb执行数据写入时,如果参数为struct/[]struct,自动映射与表字段对应关系,不再使用gconv标签标识;
|
||||
|
||||
|
||||
|
||||
|
||||
@ -441,8 +441,8 @@ func (a *IntArray) Unique() *IntArray {
|
||||
//
|
||||
// 使用自定义方法执行加锁修改操作。
|
||||
func (a *IntArray) LockFunc(f func(array []int)) *IntArray {
|
||||
a.mu.Lock(true)
|
||||
defer a.mu.Unlock(true)
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
f(a.array)
|
||||
return a
|
||||
}
|
||||
@ -451,8 +451,8 @@ func (a *IntArray) LockFunc(f func(array []int)) *IntArray {
|
||||
//
|
||||
// 使用自定义方法执行加锁读取操作。
|
||||
func (a *IntArray) RLockFunc(f func(array []int)) *IntArray {
|
||||
a.mu.RLock(true)
|
||||
defer a.mu.RUnlock(true)
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
f(a.array)
|
||||
return a
|
||||
}
|
||||
|
||||
@ -433,8 +433,8 @@ func (a *Array) Unique() *Array {
|
||||
//
|
||||
// 使用自定义方法执行加锁修改操作
|
||||
func (a *Array) LockFunc(f func(array []interface{})) *Array {
|
||||
a.mu.Lock(true)
|
||||
defer a.mu.Unlock(true)
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
f(a.array)
|
||||
return a
|
||||
}
|
||||
@ -443,8 +443,8 @@ func (a *Array) LockFunc(f func(array []interface{})) *Array {
|
||||
//
|
||||
// 使用自定义方法执行加锁读取操作
|
||||
func (a *Array) RLockFunc(f func(array []interface{})) *Array {
|
||||
a.mu.RLock(true)
|
||||
defer a.mu.RUnlock(true)
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
f(a.array)
|
||||
return a
|
||||
}
|
||||
|
||||
@ -440,8 +440,8 @@ func (a *StringArray) Unique() *StringArray {
|
||||
//
|
||||
// 使用自定义方法执行加锁修改操作。
|
||||
func (a *StringArray) LockFunc(f func(array []string)) *StringArray {
|
||||
a.mu.Lock(true)
|
||||
defer a.mu.Unlock(true)
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
f(a.array)
|
||||
return a
|
||||
}
|
||||
@ -450,8 +450,8 @@ func (a *StringArray) LockFunc(f func(array []string)) *StringArray {
|
||||
//
|
||||
// 使用自定义方法执行加锁读取操作。
|
||||
func (a *StringArray) RLockFunc(f func(array []string)) *StringArray {
|
||||
a.mu.RLock(true)
|
||||
defer a.mu.RUnlock(true)
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
f(a.array)
|
||||
return a
|
||||
}
|
||||
|
||||
@ -415,8 +415,8 @@ func (a *SortedIntArray) Clear() *SortedIntArray {
|
||||
//
|
||||
// 使用自定义方法执行加锁修改操作。
|
||||
func (a *SortedIntArray) LockFunc(f func(array []int)) *SortedIntArray {
|
||||
a.mu.Lock(true)
|
||||
defer a.mu.Unlock(true)
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
f(a.array)
|
||||
return a
|
||||
}
|
||||
@ -425,8 +425,8 @@ func (a *SortedIntArray) LockFunc(f func(array []int)) *SortedIntArray {
|
||||
//
|
||||
// 使用自定义方法执行加锁读取操作。
|
||||
func (a *SortedIntArray) RLockFunc(f func(array []int)) *SortedIntArray {
|
||||
a.mu.RLock(true)
|
||||
defer a.mu.RUnlock(true)
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
f(a.array)
|
||||
return a
|
||||
}
|
||||
|
||||
@ -422,8 +422,8 @@ func (a *SortedArray) Clear() *SortedArray {
|
||||
//
|
||||
// 使用自定义方法执行加锁修改操作。
|
||||
func (a *SortedArray) LockFunc(f func(array []interface{})) *SortedArray {
|
||||
a.mu.Lock(true)
|
||||
defer a.mu.Unlock(true)
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
f(a.array)
|
||||
return a
|
||||
}
|
||||
@ -432,8 +432,8 @@ func (a *SortedArray) LockFunc(f func(array []interface{})) *SortedArray {
|
||||
//
|
||||
// 使用自定义方法执行加锁读取操作。
|
||||
func (a *SortedArray) RLockFunc(f func(array []interface{})) *SortedArray {
|
||||
a.mu.RLock(true)
|
||||
defer a.mu.RUnlock(true)
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
f(a.array)
|
||||
return a
|
||||
}
|
||||
|
||||
@ -410,8 +410,8 @@ func (a *SortedStringArray) Clear() *SortedStringArray {
|
||||
//
|
||||
// 使用自定义方法执行加锁修改操作。
|
||||
func (a *SortedStringArray) LockFunc(f func(array []string)) *SortedStringArray {
|
||||
a.mu.Lock(true)
|
||||
defer a.mu.Unlock(true)
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
f(a.array)
|
||||
return a
|
||||
}
|
||||
@ -420,8 +420,8 @@ func (a *SortedStringArray) LockFunc(f func(array []string)) *SortedStringArray
|
||||
//
|
||||
// 使用自定义方法执行加锁读取操作。
|
||||
func (a *SortedStringArray) RLockFunc(f func(array []string)) *SortedStringArray {
|
||||
a.mu.RLock(true)
|
||||
defer a.mu.RUnlock(true)
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
f(a.array)
|
||||
return a
|
||||
}
|
||||
|
||||
@ -291,8 +291,8 @@ func (gm *Map) Clear() {
|
||||
//
|
||||
// 并发安全锁操作,使用自定义方法执行加锁修改操作
|
||||
func (gm *Map) LockFunc(f func(m map[interface{}]interface{})) {
|
||||
gm.mu.Lock(true)
|
||||
defer gm.mu.Unlock(true)
|
||||
gm.mu.Lock()
|
||||
defer gm.mu.Unlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
@ -300,8 +300,8 @@ func (gm *Map) LockFunc(f func(m map[interface{}]interface{})) {
|
||||
//
|
||||
// 并发安全锁操作,使用自定义方法执行加锁读取操作
|
||||
func (gm *Map) RLockFunc(f func(m map[interface{}]interface{})) {
|
||||
gm.mu.RLock(true)
|
||||
defer gm.mu.RUnlock(true)
|
||||
gm.mu.RLock()
|
||||
defer gm.mu.RUnlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
|
||||
@ -238,15 +238,15 @@ func (gm *IntBoolMap) Clear() {
|
||||
|
||||
// 并发安全锁操作,使用自定义方法执行加锁修改操作
|
||||
func (gm *IntBoolMap) LockFunc(f func(m map[int]bool)) {
|
||||
gm.mu.Lock(true)
|
||||
defer gm.mu.Unlock(true)
|
||||
gm.mu.Lock()
|
||||
defer gm.mu.Unlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
// 并发安全锁操作,使用自定义方法执行加锁读取操作
|
||||
func (gm *IntBoolMap) RLockFunc(f func(m map[int]bool)) {
|
||||
gm.mu.RLock(true)
|
||||
defer gm.mu.RUnlock(true)
|
||||
gm.mu.RLock()
|
||||
defer gm.mu.RUnlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
|
||||
@ -238,15 +238,15 @@ func (gm *IntIntMap) Clear() {
|
||||
|
||||
// 并发安全锁操作,使用自定义方法执行加锁修改操作
|
||||
func (gm *IntIntMap) LockFunc(f func(m map[int]int)) {
|
||||
gm.mu.Lock(true)
|
||||
defer gm.mu.Unlock(true)
|
||||
gm.mu.Lock()
|
||||
defer gm.mu.Unlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
// 并发安全锁操作,使用自定义方法执行加锁读取操作
|
||||
func (gm *IntIntMap) RLockFunc(f func(m map[int]int)) {
|
||||
gm.mu.RLock(true)
|
||||
defer gm.mu.RUnlock(true)
|
||||
gm.mu.RLock()
|
||||
defer gm.mu.RUnlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
|
||||
@ -231,8 +231,8 @@ func (gm *IntInterfaceMap) LockFunc(f func(m map[int]interface{})) {
|
||||
|
||||
// 并发安全锁操作,使用自定义方法执行加锁读取操作
|
||||
func (gm *IntInterfaceMap) RLockFunc(f func(m map[int]interface{})) {
|
||||
gm.mu.RLock(true)
|
||||
defer gm.mu.RUnlock(true)
|
||||
gm.mu.RLock()
|
||||
defer gm.mu.RUnlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
|
||||
@ -239,15 +239,15 @@ func (gm *IntStringMap) Clear() {
|
||||
|
||||
// 并发安全锁操作,使用自定义方法执行加锁修改操作
|
||||
func (gm *IntStringMap) LockFunc(f func(m map[int]string)) {
|
||||
gm.mu.Lock(true)
|
||||
defer gm.mu.Unlock(true)
|
||||
gm.mu.Lock()
|
||||
defer gm.mu.Unlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
// 并发安全锁操作,使用自定义方法执行加锁读取操作
|
||||
func (gm *IntStringMap) RLockFunc(f func(m map[int]string)) {
|
||||
gm.mu.RLock(true)
|
||||
defer gm.mu.RUnlock(true)
|
||||
gm.mu.RLock()
|
||||
defer gm.mu.RUnlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
|
||||
@ -238,15 +238,15 @@ func (gm *StringBoolMap) Clear() {
|
||||
|
||||
// 并发安全锁操作,使用自定义方法执行加锁修改操作
|
||||
func (gm *StringBoolMap) LockFunc(f func(m map[string]bool)) {
|
||||
gm.mu.Lock(true)
|
||||
defer gm.mu.Unlock(true)
|
||||
gm.mu.Lock()
|
||||
defer gm.mu.Unlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
// 并发安全锁操作,使用自定义方法执行加锁读取操作
|
||||
func (gm *StringBoolMap) RLockFunc(f func(m map[string]bool)) {
|
||||
gm.mu.RLock(true)
|
||||
defer gm.mu.RUnlock(true)
|
||||
gm.mu.RLock()
|
||||
defer gm.mu.RUnlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
|
||||
@ -239,15 +239,15 @@ func (gm *StringIntMap) Clear() {
|
||||
|
||||
// 并发安全写锁操作,使用自定义方法执行加锁修改操作
|
||||
func (gm *StringIntMap) LockFunc(f func(m map[string]int)) {
|
||||
gm.mu.Lock(true)
|
||||
defer gm.mu.Unlock(true)
|
||||
gm.mu.Lock()
|
||||
defer gm.mu.Unlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
// 并发安全读锁操作,使用自定义方法执行加锁读取操作
|
||||
func (gm *StringIntMap) RLockFunc(f func(m map[string]int)) {
|
||||
gm.mu.RLock(true)
|
||||
defer gm.mu.RUnlock(true)
|
||||
gm.mu.RLock()
|
||||
defer gm.mu.RUnlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
|
||||
@ -224,15 +224,15 @@ func (gm *StringInterfaceMap) Clear() {
|
||||
|
||||
// 并发安全写锁操作,使用自定义方法执行加锁修改操作
|
||||
func (gm *StringInterfaceMap) LockFunc(f func(m map[string]interface{})) {
|
||||
gm.mu.Lock(true)
|
||||
defer gm.mu.Unlock(true)
|
||||
gm.mu.Lock()
|
||||
defer gm.mu.Unlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
// 并发安全读锁操作,使用自定义方法执行加锁读取操作
|
||||
func (gm *StringInterfaceMap) RLockFunc(f func(m map[string]interface{})) {
|
||||
gm.mu.RLock(true)
|
||||
defer gm.mu.RUnlock(true)
|
||||
gm.mu.RLock()
|
||||
defer gm.mu.RUnlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
|
||||
@ -238,15 +238,15 @@ func (gm *StringStringMap) Clear() {
|
||||
|
||||
// 并发安全写锁操作,使用自定义方法执行加锁修改操作
|
||||
func (gm *StringStringMap) LockFunc(f func(m map[string]string)) {
|
||||
gm.mu.Lock(true)
|
||||
defer gm.mu.Unlock(true)
|
||||
gm.mu.Lock()
|
||||
defer gm.mu.Unlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
// 并发安全读锁操作,使用自定义方法执行加锁读取操作
|
||||
func (gm *StringStringMap) RLockFunc(f func(m map[string]string)) {
|
||||
gm.mu.RLock(true)
|
||||
defer gm.mu.RUnlock(true)
|
||||
gm.mu.RLock()
|
||||
defer gm.mu.RUnlock()
|
||||
f(gm.m)
|
||||
}
|
||||
|
||||
|
||||
@ -15,9 +15,8 @@
|
||||
package gqueue
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"github.com/gogf/gf/g/container/glist"
|
||||
"math"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// 1、这是一个先进先出的队列(chan <-- list);
|
||||
@ -28,9 +27,8 @@ import (
|
||||
//
|
||||
// 4、由于功能主体是chan,那么操作仍然像chan那样具有阻塞效果;
|
||||
type Queue struct {
|
||||
mu sync.Mutex // 底层链表写锁
|
||||
limit int // 队列限制大小
|
||||
list *list.List // 底层数据链表
|
||||
list *glist.List // 底层数据链表
|
||||
events chan struct{} // 写入事件通知
|
||||
closed chan struct{} // 队列关闭通知
|
||||
C chan interface{} // 队列数据读取
|
||||
@ -50,7 +48,7 @@ func New(limit...int) *Queue {
|
||||
q.limit = limit[0]
|
||||
q.C = make(chan interface{}, limit[0])
|
||||
} else {
|
||||
q.list = list.New()
|
||||
q.list = glist.New()
|
||||
q.events = make(chan struct{}, math.MaxInt32)
|
||||
q.C = make(chan interface{}, gDEFAULT_QUEUE_SIZE)
|
||||
go q.startAsyncLoop()
|
||||
@ -68,7 +66,6 @@ func (q *Queue) startAsyncLoop() {
|
||||
for {
|
||||
if length := q.list.Len(); length > 0 {
|
||||
array := make([]interface{}, length)
|
||||
q.mu.Lock()
|
||||
for i := 0; i < length; i++ {
|
||||
if e := q.list.Front(); e != nil {
|
||||
array[i] = q.list.Remove(e)
|
||||
@ -76,7 +73,6 @@ func (q *Queue) startAsyncLoop() {
|
||||
break
|
||||
}
|
||||
}
|
||||
q.mu.Unlock()
|
||||
for _, v := range array {
|
||||
q.C <- v
|
||||
}
|
||||
@ -93,9 +89,7 @@ func (q *Queue) Push(v interface{}) {
|
||||
if q.limit > 0 {
|
||||
q.C <- v
|
||||
} else {
|
||||
q.mu.Lock()
|
||||
q.list.PushBack(v)
|
||||
q.mu.Unlock()
|
||||
q.events <- struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
@ -149,8 +149,8 @@ func (r *Ring) Unlink(n int) *Ring {
|
||||
|
||||
// 读锁遍历,往后只读遍历,回调函数返回true表示继续遍历,否则退出遍历
|
||||
func (r *Ring) RLockIteratorNext(f func(value interface{}) bool) {
|
||||
r.mu.RLock(true)
|
||||
defer r.mu.RUnlock(true)
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
if !f(r.ring.Value) {
|
||||
return
|
||||
}
|
||||
@ -163,8 +163,8 @@ func (r *Ring) RLockIteratorNext(f func(value interface{}) bool) {
|
||||
|
||||
// 读锁遍历,往前只读遍历,回调函数返回true表示继续遍历,否则退出遍历
|
||||
func (r *Ring) RLockIteratorPrev(f func(value interface{}) bool) {
|
||||
r.mu.RLock(true)
|
||||
defer r.mu.RUnlock(true)
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
if !f(r.ring.Value) {
|
||||
return
|
||||
}
|
||||
@ -177,8 +177,8 @@ func (r *Ring) RLockIteratorPrev(f func(value interface{}) bool) {
|
||||
|
||||
// 写锁遍历,往后写遍历,回调函数返回true表示继续遍历,否则退出遍历
|
||||
func (r *Ring) LockIteratorNext(f func(item *ring.Ring) bool) {
|
||||
r.mu.RLock(true)
|
||||
defer r.mu.RUnlock(true)
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
if !f(r.ring) {
|
||||
return
|
||||
}
|
||||
@ -191,8 +191,8 @@ func (r *Ring) LockIteratorNext(f func(item *ring.Ring) bool) {
|
||||
|
||||
// 写锁遍历,往前写遍历,回调函数返回true表示继续遍历,否则退出遍历
|
||||
func (r *Ring) LockIteratorPrev(f func(item *ring.Ring) bool) {
|
||||
r.mu.RLock(true)
|
||||
defer r.mu.RUnlock(true)
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
if !f(r.ring) {
|
||||
return
|
||||
}
|
||||
|
||||
@ -139,8 +139,8 @@ func (set *Set) String() string {
|
||||
//
|
||||
// 使用自定义方法执行加锁修改操作。
|
||||
func (set *Set) LockFunc(f func(m map[interface{}]struct{})) *Set {
|
||||
set.mu.Lock(true)
|
||||
defer set.mu.Unlock(true)
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
f(set.m)
|
||||
return set
|
||||
}
|
||||
@ -149,8 +149,8 @@ func (set *Set) LockFunc(f func(m map[interface{}]struct{})) *Set {
|
||||
//
|
||||
// 使用自定义方法执行加锁读取操作。
|
||||
func (set *Set) RLockFunc(f func(m map[interface{}]struct{})) *Set {
|
||||
set.mu.RLock(true)
|
||||
defer set.mu.RUnlock(true)
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
f(set.m)
|
||||
return set
|
||||
}
|
||||
|
||||
@ -130,8 +130,8 @@ func (set *IntSet) String() string {
|
||||
//
|
||||
// 使用自定义方法执行加锁修改操作。
|
||||
func (set *IntSet) LockFunc(f func(m map[int]struct{})) *IntSet {
|
||||
set.mu.Lock(true)
|
||||
defer set.mu.Unlock(true)
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
f(set.m)
|
||||
return set
|
||||
}
|
||||
@ -140,8 +140,8 @@ func (set *IntSet) LockFunc(f func(m map[int]struct{})) *IntSet {
|
||||
//
|
||||
// 使用自定义方法执行加锁读取操作。
|
||||
func (set *IntSet) RLockFunc(f func(m map[int]struct{})) *IntSet {
|
||||
set.mu.RLock(true)
|
||||
defer set.mu.RUnlock(true)
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
f(set.m)
|
||||
return set
|
||||
}
|
||||
|
||||
@ -130,8 +130,8 @@ func (set *StringSet) String() string {
|
||||
//
|
||||
// 使用自定义方法执行加锁修改操作。
|
||||
func (set *StringSet) LockFunc(f func(m map[string]struct{})) *StringSet {
|
||||
set.mu.Lock(true)
|
||||
defer set.mu.Unlock(true)
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
f(set.m)
|
||||
return set
|
||||
}
|
||||
@ -140,8 +140,8 @@ func (set *StringSet) LockFunc(f func(m map[string]struct{})) *StringSet {
|
||||
//
|
||||
// 使用自定义方法执行加锁读取操作。
|
||||
func (set *StringSet) RLockFunc(f func(m map[string]struct{})) *StringSet {
|
||||
set.mu.RLock(true)
|
||||
defer set.mu.RUnlock(true)
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
f(set.m)
|
||||
return set
|
||||
}
|
||||
|
||||
@ -39,8 +39,8 @@ type DB interface {
|
||||
doPrepare(link dbLink, query string) (*sql.Stmt, error)
|
||||
doInsert(link dbLink, table string, data interface{}, option int, batch...int) (result sql.Result, err error)
|
||||
doBatchInsert(link dbLink, table string, list interface{}, option int, batch...int) (result sql.Result, err error)
|
||||
doUpdate(link dbLink, table string, data interface{}, condition interface{}, args ...interface{}) (result sql.Result, err error)
|
||||
doDelete(link dbLink, table string, condition interface{}, args ...interface{}) (result sql.Result, err error)
|
||||
doUpdate(link dbLink, table string, data interface{}, condition string, args ...interface{}) (result sql.Result, err error)
|
||||
doDelete(link dbLink, table string, condition string, args ...interface{}) (result sql.Result, err error)
|
||||
|
||||
// 数据库查询
|
||||
GetAll(query string, args ...interface{}) (Result, error)
|
||||
|
||||
@ -312,7 +312,7 @@ func (bs *dbBase) doInsert(link dbLink, table string, data interface{}, option i
|
||||
return bs.db.doBatchInsert(link, table, data, option, batch...)
|
||||
case reflect.Map: fallthrough
|
||||
case reflect.Struct:
|
||||
dataMap = Map(gconv.Map(data))
|
||||
dataMap = gconv.Map(data)
|
||||
default:
|
||||
return result, errors.New(fmt.Sprint("unsupported data type:", kind))
|
||||
}
|
||||
@ -320,7 +320,7 @@ func (bs *dbBase) doInsert(link dbLink, table string, data interface{}, option i
|
||||
for k, v := range dataMap {
|
||||
fields = append(fields, charL + k + charR)
|
||||
values = append(values, "?")
|
||||
params = append(params, v)
|
||||
params = append(params, convertParam(v))
|
||||
}
|
||||
operation := getInsertOperationByOption(option)
|
||||
updateStr := ""
|
||||
@ -369,6 +369,10 @@ func (bs *dbBase) doBatchInsert(link dbLink, table string, list interface{}, opt
|
||||
var params []interface{}
|
||||
listMap := (List)(nil)
|
||||
switch v := list.(type) {
|
||||
case Result:
|
||||
listMap = v.ToList()
|
||||
case Record:
|
||||
listMap = List{v.ToMap()}
|
||||
case List:
|
||||
listMap = v
|
||||
case Map:
|
||||
@ -436,7 +440,7 @@ func (bs *dbBase) doBatchInsert(link dbLink, table string, list interface{}, opt
|
||||
}
|
||||
for i := 0; i < len(listMap); i++ {
|
||||
for _, k := range keys {
|
||||
params = append(params, listMap[i][k])
|
||||
params = append(params, convertParam(listMap[i][k]))
|
||||
}
|
||||
values = append(values, valueHolderStr)
|
||||
if len(values) == batchNum {
|
||||
@ -479,17 +483,13 @@ func (bs *dbBase) doBatchInsert(link dbLink, table string, list interface{}, opt
|
||||
// CURD操作:数据更新,统一采用sql预处理。
|
||||
// data参数支持string/map/struct/*struct类型。
|
||||
func (bs *dbBase) Update(table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error) {
|
||||
link, err := bs.db.Master()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bs.db.doUpdate(link, table, data, condition, args ...)
|
||||
newWhere, newArgs := formatCondition(condition, args)
|
||||
return bs.db.doUpdate(nil, table, data, newWhere, newArgs ...)
|
||||
}
|
||||
|
||||
// CURD操作:数据更新,统一采用sql预处理。
|
||||
// data参数支持string/map/struct/*struct类型类型。
|
||||
func (bs *dbBase) doUpdate(link dbLink, table string, data interface{}, condition interface{}, args ...interface{}) (result sql.Result, err error) {
|
||||
params := ([]interface{})(nil)
|
||||
func (bs *dbBase) doUpdate(link dbLink, table string, data interface{}, condition string, args ...interface{}) (result sql.Result, err error) {
|
||||
updates := ""
|
||||
charL, charR := bs.db.getChars()
|
||||
// 使用反射进行类型判断
|
||||
@ -499,43 +499,51 @@ func (bs *dbBase) doUpdate(link dbLink, table string, data interface{}, conditio
|
||||
rv = rv.Elem()
|
||||
kind = rv.Kind()
|
||||
}
|
||||
params := []interface{}(nil)
|
||||
switch kind {
|
||||
case reflect.Map: fallthrough
|
||||
case reflect.Struct:
|
||||
var fields []string
|
||||
for k, v := range gconv.Map(data) {
|
||||
fields = append(fields, fmt.Sprintf("%s%s%s=?", charL, k, charR))
|
||||
params = append(params, gconv.String(v))
|
||||
params = append(params, convertParam(v))
|
||||
}
|
||||
updates = strings.Join(fields, ",")
|
||||
default:
|
||||
updates = gconv.String(data)
|
||||
}
|
||||
for _, v := range args {
|
||||
params = append(params, gconv.String(v))
|
||||
if len(params) > 0 {
|
||||
args = append(params, args...)
|
||||
}
|
||||
// 如果没有传递link,那么使用默认的写库对象
|
||||
if link == nil {
|
||||
if link, err = bs.db.Master(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
newWhere, newArgs := formatCondition(condition, params)
|
||||
return bs.db.doExec(link, fmt.Sprintf("UPDATE %s SET %s WHERE %s", table, updates, newWhere), newArgs...)
|
||||
if len(condition) == 0 {
|
||||
return bs.db.doExec(link, fmt.Sprintf("UPDATE %s SET %s", table, updates), args...)
|
||||
}
|
||||
return bs.db.doExec(link, fmt.Sprintf("UPDATE %s SET %s WHERE %s", table, updates, condition), args...)
|
||||
}
|
||||
|
||||
// CURD操作:删除数据
|
||||
func (bs *dbBase) Delete(table string, condition interface{}, args ...interface{}) (result sql.Result, err error) {
|
||||
link, err := bs.db.Master()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bs.db.doDelete(link, table, condition, args ...)
|
||||
newWhere, newArgs := formatCondition(condition, args)
|
||||
return bs.db.doDelete(nil, table, newWhere, newArgs ...)
|
||||
}
|
||||
|
||||
// CURD操作:删除数据
|
||||
func (bs *dbBase) doDelete(link dbLink, table string, condition interface{}, args ...interface{}) (result sql.Result, err error) {
|
||||
newWhere, newArgs := formatCondition(condition, args)
|
||||
return bs.db.doExec(link, fmt.Sprintf("DELETE FROM %s WHERE %s", table, newWhere), newArgs...)
|
||||
func (bs *dbBase) doDelete(link dbLink, table string, condition string, args ...interface{}) (result sql.Result, err error) {
|
||||
if link == nil {
|
||||
if link, err = bs.db.Master(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if len(condition) == 0 {
|
||||
return bs.db.doExec(link, fmt.Sprintf("DELETE FROM %s", table), args...)
|
||||
}
|
||||
return bs.db.doExec(link, fmt.Sprintf("DELETE FROM %s WHERE %s", table, condition), args...)
|
||||
}
|
||||
|
||||
// 获得缓存对象
|
||||
|
||||
@ -31,41 +31,62 @@ func formatCondition(where interface{}, args []interface{}) (newWhere string, ne
|
||||
rv = rv.Elem()
|
||||
kind = rv.Kind()
|
||||
}
|
||||
tmpArgs := []interface{}(nil)
|
||||
switch kind {
|
||||
// 注意当where为map/struct类型时,args参数必须为空。
|
||||
// map/struct类型
|
||||
case reflect.Map: fallthrough
|
||||
case reflect.Struct:
|
||||
for k, v := range gconv.Map(where) {
|
||||
for key, value := range gconv.Map(where) {
|
||||
if buffer.Len() > 0 {
|
||||
buffer.WriteString(" AND ")
|
||||
}
|
||||
// 支持slice键值/属性,这个时候作为IN查询
|
||||
switch reflect.ValueOf(v).Kind() {
|
||||
// 支持slice键值/属性,如果只有一个?占位符号,那么作为IN查询,否则打散作为多个查询参数
|
||||
rv := reflect.ValueOf(value)
|
||||
switch rv.Kind() {
|
||||
case reflect.Slice: fallthrough
|
||||
case reflect.Array:
|
||||
buffer.WriteString(k + " IN(?)")
|
||||
default:
|
||||
if gstr.Pos(k, "<") == -1 && gstr.Pos(k, ">") == -1 && gstr.Pos(k, "=") == -1 {
|
||||
buffer.WriteString(k + "=?")
|
||||
count := gstr.Count(key, "?")
|
||||
if count == 0 {
|
||||
buffer.WriteString(key + " IN(?)")
|
||||
tmpArgs = append(tmpArgs, value)
|
||||
} else if count != rv.Len() {
|
||||
buffer.WriteString(key)
|
||||
tmpArgs = append(tmpArgs, value)
|
||||
} else {
|
||||
buffer.WriteString(k + "?")
|
||||
buffer.WriteString(key)
|
||||
// 如果键名/属性名称中带有多个?占位符号,那么将参数打散
|
||||
tmpArgs = append(tmpArgs, gconv.Interfaces(value)...)
|
||||
}
|
||||
default:
|
||||
if value == nil {
|
||||
buffer.WriteString(key)
|
||||
} else {
|
||||
if gstr.Pos(key, "?") == -1 {
|
||||
if gstr.Pos(key, "<") == -1 && gstr.Pos(key, ">") == -1 && gstr.Pos(key, "=") == -1 {
|
||||
buffer.WriteString(key + "=?")
|
||||
} else {
|
||||
buffer.WriteString(key + "?")
|
||||
}
|
||||
} else {
|
||||
buffer.WriteString(key)
|
||||
}
|
||||
tmpArgs = append(tmpArgs, value)
|
||||
}
|
||||
}
|
||||
// 当给定的Where参数为map/struct时,args参数必定为空,
|
||||
// 考虑到后续还会对args做处理,特别是判断slice类型,这里直接给args赋值。
|
||||
args = append(args, v)
|
||||
}
|
||||
newWhere = buffer.String()
|
||||
|
||||
default:
|
||||
buffer.WriteString(gconv.String(where))
|
||||
}
|
||||
// 没有任何条件查询参数,直接返回
|
||||
if buffer.Len() == 0 {
|
||||
buffer.WriteString("1=1")
|
||||
return "", args
|
||||
}
|
||||
// 查询条件参数处理,主要处理slice参数类型
|
||||
newWhere = buffer.String()
|
||||
if len(args) > 0 {
|
||||
for index, arg := range args {
|
||||
tmpArgs = append(tmpArgs, args...)
|
||||
// 查询条件参数处理,主要处理slice参数类型
|
||||
if len(tmpArgs) > 0 {
|
||||
for index, arg := range tmpArgs {
|
||||
rv := reflect.ValueOf(arg)
|
||||
kind := rv.Kind()
|
||||
if kind == reflect.Ptr {
|
||||
@ -90,6 +111,14 @@ func formatCondition(where interface{}, args []interface{}) (newWhere string, ne
|
||||
return s
|
||||
})
|
||||
default:
|
||||
// 支持例如 Where/And/Or("uid", 1) 这种格式
|
||||
if gstr.Pos(newWhere, "?") == -1 {
|
||||
if gstr.Pos(newWhere, "<") == -1 && gstr.Pos(newWhere, ">") == -1 && gstr.Pos(newWhere, "=") == -1 {
|
||||
newWhere += "=?"
|
||||
} else {
|
||||
newWhere += "?"
|
||||
}
|
||||
}
|
||||
newArgs = append(newArgs, arg)
|
||||
}
|
||||
}
|
||||
@ -97,6 +126,22 @@ func formatCondition(where interface{}, args []interface{}) (newWhere string, ne
|
||||
return
|
||||
}
|
||||
|
||||
// 将预处理参数转换为底层数据库引擎支持的格式。
|
||||
// 主要是判断参数是否为复杂数据类型,如果是,那么转换为基础类型。
|
||||
func convertParam(value interface{}) interface{} {
|
||||
rv := reflect.ValueOf(value)
|
||||
kind := rv.Kind()
|
||||
if kind == reflect.Ptr {
|
||||
rv = rv.Elem()
|
||||
kind = rv.Kind()
|
||||
}
|
||||
switch kind {
|
||||
case reflect.Struct:
|
||||
return gconv.String(value)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
// 打印SQL对象(仅在debug=true时有效)
|
||||
func printSql(v *Sql) {
|
||||
s := fmt.Sprintf("%s, %v, %s, %s, %d ms, %s", v.Sql, v.Args,
|
||||
|
||||
@ -3,17 +3,18 @@
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
//
|
||||
// @author john, ymrjqyy
|
||||
|
||||
package gdb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
"database/sql"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
_ "github.com/gogf/gf/third/github.com/go-sql-driver/mysql"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
_ "github.com/gogf/gf/third/github.com/go-sql-driver/mysql"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 数据库链式操作模型对象
|
||||
@ -35,7 +36,7 @@ type Model struct {
|
||||
cacheEnabled bool // 当前SQL操作是否开启查询缓存功能
|
||||
cacheTime int // 查询缓存时间
|
||||
cacheName string // 查询缓存名称
|
||||
alterable bool // 当前模型是否运行可修改模式(默认情况下链式操作不会修改当前模型,而是创建新的模型返回)
|
||||
safe bool // 当前模型是否运行安全模式(可修改当前模型,否则每一次链式操作都是返回新的模型对象)
|
||||
}
|
||||
|
||||
// 链式操作,数据表字段,可支持多个表,以半角逗号连接
|
||||
@ -45,6 +46,7 @@ func (bs *dbBase) Table(tables string) (*Model) {
|
||||
tablesInit : tables,
|
||||
tables : tables,
|
||||
fields : "*",
|
||||
safe : false,
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,6 +62,7 @@ func (tx *TX) Table(tables string) (*Model) {
|
||||
tx : tx,
|
||||
tablesInit : tables,
|
||||
tables : tables,
|
||||
safe : false,
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,21 +83,25 @@ func (md *Model) Clone() *Model {
|
||||
return newModel
|
||||
}
|
||||
|
||||
// 标识当前对象可被修改。
|
||||
// 标识当前对象运行安全模式(可被修改)。
|
||||
// 1. 默认情况下,模型对象的对象属性无法被修改,
|
||||
// 每一次链式操作都是克隆一个新的模型对象,这样所有的操作都不会污染模型对象。
|
||||
// 但是链式操作如果需要分开执行,那么需要将新的克隆对象赋值给旧的模型对象继续操作。
|
||||
// 2. 当标识模型对象为可修改,那么在当前模型对象的所有链式操作均会影响下一次的链式操作,
|
||||
// 即使是链式操作分开执行。
|
||||
// 3. 大部分ORM框架默认模型对象是可修改的,但是GF框架的ORM提供给开发者更灵活,更安全的链式操作选项。
|
||||
func (md *Model) Alterable() *Model {
|
||||
md.alterable = true
|
||||
func (md *Model) Safe(safe...bool) *Model {
|
||||
if len(safe) > 0 {
|
||||
md.safe = safe[0]
|
||||
} else {
|
||||
md.safe = true
|
||||
}
|
||||
return md
|
||||
}
|
||||
|
||||
// 返回操作的模型对象,可能是当前对象,也可能是新的克隆对象,根据alterable决定。
|
||||
func (md *Model) getModel() *Model {
|
||||
if md.alterable {
|
||||
if !md.safe {
|
||||
return md
|
||||
} else {
|
||||
return md.Clone()
|
||||
@ -137,16 +144,15 @@ func (md *Model) Filter() (*Model) {
|
||||
}
|
||||
|
||||
// 链式操作,condition,支持string & gdb.Map.
|
||||
// 注意,多个Where调用时,会相互覆盖,只有最后一个Where语句生效。
|
||||
// 注意,多个Where调用时,会自动转换为And条件调用。
|
||||
func (md *Model) Where(where interface{}, args ...interface{}) (*Model) {
|
||||
model := md.getModel()
|
||||
model := md.getModel()
|
||||
if model.where != "" {
|
||||
return md.And(where, args...)
|
||||
}
|
||||
newWhere, newArgs := formatCondition(where, args)
|
||||
model.where = newWhere
|
||||
model.whereArgs = newArgs
|
||||
// 支持 Where("uid", 1)这种格式
|
||||
if len(args) == 1 && strings.Index(model.where , "?") < 0 {
|
||||
model.where += "=?"
|
||||
}
|
||||
return model
|
||||
}
|
||||
|
||||
@ -154,8 +160,12 @@ func (md *Model) Where(where interface{}, args ...interface{}) (*Model) {
|
||||
func (md *Model) And(where interface{}, args ...interface{}) (*Model) {
|
||||
model := md.getModel()
|
||||
newWhere, newArgs := formatCondition(where, args)
|
||||
model.where += " AND " + newWhere
|
||||
model.whereArgs = append(model.whereArgs, newArgs...)
|
||||
if len(model.where) > 0 && model.where[0] == '(' {
|
||||
model.where = fmt.Sprintf(`%s AND (%s)`, model.where, newWhere)
|
||||
} else {
|
||||
model.where = fmt.Sprintf(`(%s) AND (%s)`, model.where, newWhere)
|
||||
}
|
||||
model.whereArgs = append(model.whereArgs, newArgs...)
|
||||
return model
|
||||
}
|
||||
|
||||
@ -163,8 +173,12 @@ func (md *Model) And(where interface{}, args ...interface{}) (*Model) {
|
||||
func (md *Model) Or(where interface{}, args ...interface{}) (*Model) {
|
||||
model := md.getModel()
|
||||
newWhere, newArgs := formatCondition(where, args)
|
||||
model.where += " OR " + newWhere
|
||||
model.whereArgs = append(model.whereArgs, newArgs...)
|
||||
if len(model.where) > 0 && model.where[0] == '(' {
|
||||
model.where = fmt.Sprintf(`%s OR (%s)`, model.where, newWhere)
|
||||
} else {
|
||||
model.where = fmt.Sprintf(`(%s) OR (%s)`, model.where, newWhere)
|
||||
}
|
||||
model.whereArgs = append(model.whereArgs, newArgs...)
|
||||
return model
|
||||
}
|
||||
|
||||
@ -190,8 +204,7 @@ func (md *Model) Limit(start int, limit int) (*Model) {
|
||||
return model
|
||||
}
|
||||
|
||||
// 链式操作,翻页
|
||||
// @author ymrjqyy
|
||||
// 链式操作,翻页,注意分页页码从1开始,而Limit方法从0开始。
|
||||
func (md *Model) ForPage(page, limit int) (*Model) {
|
||||
model := md.getModel()
|
||||
model.start = (page - 1) * limit
|
||||
@ -234,13 +247,17 @@ func (md *Model) Data(data ...interface{}) *Model {
|
||||
}
|
||||
model.data = m
|
||||
} else {
|
||||
switch data[0].(type) {
|
||||
switch params := data[0].(type) {
|
||||
case Result:
|
||||
model.data = params.ToList()
|
||||
case Record:
|
||||
model.data = params.ToMap()
|
||||
case List:
|
||||
model.data = data[0]
|
||||
model.data = params
|
||||
case Map:
|
||||
model.data = data[0]
|
||||
model.data = params
|
||||
default:
|
||||
rv := reflect.ValueOf(data[0])
|
||||
rv := reflect.ValueOf(params)
|
||||
kind := rv.Kind()
|
||||
if kind == reflect.Ptr {
|
||||
rv = rv.Elem()
|
||||
@ -407,9 +424,9 @@ func (md *Model) Update() (result sql.Result, err error) {
|
||||
}
|
||||
}
|
||||
if md.tx == nil {
|
||||
return md.db.Update(md.tables, md.data, md.where, md.whereArgs ...)
|
||||
return md.db.doUpdate(nil, md.tables, md.data, md.where, md.whereArgs ...)
|
||||
} else {
|
||||
return md.tx.Update(md.tables, md.data, md.where, md.whereArgs ...)
|
||||
return md.tx.doUpdate(md.tables, md.data, md.where, md.whereArgs ...)
|
||||
}
|
||||
}
|
||||
|
||||
@ -421,9 +438,9 @@ func (md *Model) Delete() (result sql.Result, err error) {
|
||||
}
|
||||
}()
|
||||
if md.tx == nil {
|
||||
return md.db.Delete(md.tables, md.where, md.whereArgs...)
|
||||
return md.db.doDelete(nil, md.tables, md.where, md.whereArgs...)
|
||||
} else {
|
||||
return md.tx.Delete(md.tables, md.where, md.whereArgs...)
|
||||
return md.tx.doDelete(md.tables, md.where, md.whereArgs...)
|
||||
}
|
||||
}
|
||||
|
||||
@ -586,14 +603,13 @@ func (md *Model) getFormattedSql() string {
|
||||
return s
|
||||
}
|
||||
|
||||
// 组块结果集
|
||||
// @author ymrjqyy
|
||||
// @author 2018-08-15
|
||||
// 组块结果集。
|
||||
func (md *Model) Chunk(limit int, callback func(result Result, err error) bool) {
|
||||
page := 1
|
||||
page := 1
|
||||
model := md
|
||||
for {
|
||||
md.ForPage(page, limit)
|
||||
data, err := md.getAll(md.getFormattedSql(), md.whereArgs...)
|
||||
model = model.ForPage(page, limit)
|
||||
data, err := model.All()
|
||||
if err != nil {
|
||||
callback(nil, err)
|
||||
break
|
||||
|
||||
@ -162,14 +162,28 @@ func (tx *TX) BatchSave(table string, list interface{}, batch...int) (sql.Result
|
||||
return tx.db.doBatchInsert(tx.tx, table, list, OPTION_SAVE, batch...)
|
||||
}
|
||||
|
||||
// CURD操作:数据更新,统一采用sql预处理
|
||||
// data参数支持字符串或者关联数组类型,内部会自行做判断处理
|
||||
// CURD操作:数据更新,统一采用sql预处理,
|
||||
// data参数支持字符串或者关联数组类型,内部会自行做判断处理.
|
||||
func (tx *TX) Update(table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error) {
|
||||
newWhere, newArgs := formatCondition(condition, args)
|
||||
return tx.doUpdate(table, data, newWhere, newArgs ...)
|
||||
}
|
||||
|
||||
// 与Update方法的区别是不处理条件参数
|
||||
func (tx *TX) doUpdate(table string, data interface{}, condition string, args ...interface{}) (sql.Result, error) {
|
||||
return tx.db.doUpdate(tx.tx, table, data, condition, args ...)
|
||||
}
|
||||
|
||||
// CURD操作:删除数据
|
||||
func (tx *TX) Delete(table string, condition interface{}, args ...interface{}) (sql.Result, error) {
|
||||
newWhere, newArgs := formatCondition(condition, args)
|
||||
return tx.doDelete(table, newWhere, newArgs ...)
|
||||
}
|
||||
|
||||
// 与Delete方法的区别是不处理条件参数
|
||||
func (tx *TX) doDelete(table string, condition string, args ...interface{}) (sql.Result, error) {
|
||||
return tx.db.doDelete(tx.tx, table, condition, args ...)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,11 +1,20 @@
|
||||
package gdb_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
"github.com/gogf/gf/g/database/gdb"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
// 初始化表数据量
|
||||
INIT_DATA_SIZE = 10
|
||||
)
|
||||
|
||||
var (
|
||||
// 数据库对象/接口
|
||||
db gdb.DB
|
||||
@ -26,10 +35,12 @@ func init() {
|
||||
Priority: 1,
|
||||
}
|
||||
hostname, _ := os.Hostname()
|
||||
// 本地测试hack
|
||||
if hostname == "ijohn" {
|
||||
node.Pass = "12345678"
|
||||
}
|
||||
gdb.AddDefaultConfigNode(node)
|
||||
gdb.AddConfigNode("test", node)
|
||||
gdb.AddConfigNode(gdb.DEFAULT_GROUP_NAME, node)
|
||||
if r, err := gdb.New(); err != nil {
|
||||
gtest.Fatal(err)
|
||||
} else {
|
||||
@ -39,12 +50,25 @@ func init() {
|
||||
if _, err := db.Exec("CREATE DATABASE IF NOT EXISTS `test` CHARACTER SET UTF8"); err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
// 选择操作数据库
|
||||
db.SetSchema("test")
|
||||
if _, err := db.Exec("DROP TABLE IF EXISTS `user`"); err != nil {
|
||||
gtest.Fatal(err)
|
||||
// 创建默认用户表
|
||||
createTable("user")
|
||||
}
|
||||
|
||||
// 创建指定名称的user测试表,当table为空时,创建随机的表名。
|
||||
// 创建的测试表默认没有任何数据。
|
||||
// 执行完成后返回该表名。
|
||||
// TODO 支持更多数据库
|
||||
func createTable(table...string) (name string) {
|
||||
if len(table) > 0 {
|
||||
name = table[0]
|
||||
} else {
|
||||
name = fmt.Sprintf(`user_%d`, gtime.Nanosecond())
|
||||
}
|
||||
if _, err := db.Exec(`
|
||||
CREATE TABLE user (
|
||||
dropTable(name)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户ID',
|
||||
passport varchar(45) NOT NULL COMMENT '账号',
|
||||
password char(32) NOT NULL COMMENT '密码',
|
||||
@ -52,7 +76,38 @@ func init() {
|
||||
create_time timestamp NOT NULL COMMENT '创建时间/注册时间',
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
`); err != nil {
|
||||
`, name)); err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 删除指定表.
|
||||
func dropTable(table string) {
|
||||
if _, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)); err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// See createTable.
|
||||
// 创建测试表,并初始化默认数据。
|
||||
func createInitTable(table...string) (name string) {
|
||||
name = createTable(table...)
|
||||
array := garray.New(true)
|
||||
for i := 1; i <= INIT_DATA_SIZE; i++ {
|
||||
array.Append(g.Map{
|
||||
"id" : i,
|
||||
"passport" : fmt.Sprintf(`t%d`, i),
|
||||
"password" : fmt.Sprintf(`p%d`, i),
|
||||
"nickname" : fmt.Sprintf(`T%d`, i),
|
||||
"create_time" : gtime.Now().String(),
|
||||
})
|
||||
}
|
||||
result, err := db.Table(name).Data(array.Slice()).Insert()
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
n, e := result.RowsAffected()
|
||||
gtest.Assert(e, nil)
|
||||
gtest.Assert(n, INIT_DATA_SIZE)
|
||||
return
|
||||
}
|
||||
|
||||
@ -9,6 +9,15 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDbBase_Ping(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
err1 := db.PingMaster()
|
||||
err2 := db.PingSlave()
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDbBase_Query(t *testing.T) {
|
||||
if _, err := db.Query("SELECT ?", 1); err != nil {
|
||||
gtest.Fatal(err)
|
||||
@ -144,57 +153,102 @@ func TestDbBase_Insert(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDbBase_BatchInsert(t *testing.T) {
|
||||
if r, err := db.BatchInsert("user", g.List {
|
||||
{
|
||||
"id" : 2,
|
||||
"passport" : "t2",
|
||||
"password" : "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname" : "T2",
|
||||
"create_time" : gtime.Now().String(),
|
||||
},
|
||||
{
|
||||
"id" : 3,
|
||||
"passport" : "t3",
|
||||
"password" : "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname" : "T3",
|
||||
"create_time" : gtime.Now().String(),
|
||||
},
|
||||
}, 1); err != nil {
|
||||
gtest.Fatal(err)
|
||||
} else {
|
||||
n, _ := r.RowsAffected()
|
||||
gtest.Assert(n, 2)
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
if r, err := db.BatchInsert("user", g.List {
|
||||
{
|
||||
"id" : 2,
|
||||
"passport" : "t2",
|
||||
"password" : "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname" : "T2",
|
||||
"create_time" : gtime.Now().String(),
|
||||
},
|
||||
{
|
||||
"id" : 3,
|
||||
"passport" : "t3",
|
||||
"password" : "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname" : "T3",
|
||||
"create_time" : gtime.Now().String(),
|
||||
},
|
||||
}, 1); err != nil {
|
||||
gtest.Fatal(err)
|
||||
} else {
|
||||
n, _ := r.RowsAffected()
|
||||
gtest.Assert(n, 2)
|
||||
}
|
||||
|
||||
result, err := db.Delete("user", "id>?", 1)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
n, _ := result.RowsAffected()
|
||||
gtest.Assert(n, 2)
|
||||
|
||||
// []interface{}
|
||||
if r, err := db.BatchInsert("user", []interface{} {
|
||||
map[interface{}]interface{} {
|
||||
"id" : 2,
|
||||
"passport" : "t2",
|
||||
"password" : "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname" : "T2",
|
||||
"create_time" : gtime.Now().String(),
|
||||
},
|
||||
map[interface{}]interface{} {
|
||||
"id" : 3,
|
||||
"passport" : "t3",
|
||||
"password" : "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname" : "T3",
|
||||
"create_time" : gtime.Now().String(),
|
||||
},
|
||||
}, 1); err != nil {
|
||||
gtest.Fatal(err)
|
||||
} else {
|
||||
n, _ := r.RowsAffected()
|
||||
result, err := db.Delete("user", "id>?", 1)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
n, _ := result.RowsAffected()
|
||||
gtest.Assert(n, 2)
|
||||
}
|
||||
|
||||
// []interface{}
|
||||
if r, err := db.BatchInsert("user", []interface{} {
|
||||
map[interface{}]interface{} {
|
||||
"id" : 2,
|
||||
"passport" : "t2",
|
||||
"password" : "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname" : "T2",
|
||||
"create_time" : gtime.Now().String(),
|
||||
},
|
||||
map[interface{}]interface{} {
|
||||
"id" : 3,
|
||||
"passport" : "t3",
|
||||
"password" : "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname" : "T3",
|
||||
"create_time" : gtime.Now().String(),
|
||||
},
|
||||
}, 1); err != nil {
|
||||
gtest.Fatal(err)
|
||||
} else {
|
||||
n, _ := r.RowsAffected()
|
||||
gtest.Assert(n, 2)
|
||||
}
|
||||
})
|
||||
// batch insert map
|
||||
gtest.Case(t, func() {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
result, err := db.BatchInsert(table, g.Map{
|
||||
"id" : 1,
|
||||
"passport" : "t1",
|
||||
"password" : "p1",
|
||||
"nickname" : "T1",
|
||||
"create_time" : gtime.Now().String(),
|
||||
})
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
n, _ := result.RowsAffected()
|
||||
gtest.Assert(n, 1)
|
||||
})
|
||||
// batch insert struct
|
||||
gtest.Case(t, func() {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
type User struct {
|
||||
Id int `gconv:"id"`
|
||||
Passport string `gconv:"passport"`
|
||||
Password string `gconv:"password"`
|
||||
NickName string `gconv:"nickname"`
|
||||
CreateTime *gtime.Time `gconv:"create_time"`
|
||||
}
|
||||
user := &User{
|
||||
Id : 1,
|
||||
Passport : "t1",
|
||||
Password : "p1",
|
||||
NickName : "T1",
|
||||
CreateTime : gtime.Now(),
|
||||
}
|
||||
result, err := db.BatchInsert(table, user)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
n, _ := result.RowsAffected()
|
||||
gtest.Assert(n, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDbBase_Save(t *testing.T) {
|
||||
|
||||
@ -90,29 +90,66 @@ func TestModel_Insert(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestModel_Batch(t *testing.T) {
|
||||
result, err := db.Table("user").Filter().Data(g.List{
|
||||
{
|
||||
"id" : 2,
|
||||
"uid" : 2,
|
||||
"passport" : "t2",
|
||||
"password" : "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname" : "T2",
|
||||
"create_time" : gtime.Now().String(),
|
||||
},
|
||||
{
|
||||
"id" : 3,
|
||||
"uid" : 3,
|
||||
"passport" : "t3",
|
||||
"password" : "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname" : "T3",
|
||||
"create_time" : gtime.Now().String(),
|
||||
},
|
||||
}).Batch(1).Insert()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
n, _ := result.RowsAffected()
|
||||
gtest.Assert(n, 2)
|
||||
// batch insert
|
||||
gtest.Case(t, func() {
|
||||
result, err := db.Table("user").Filter().Data(g.List{
|
||||
{
|
||||
"id" : 2,
|
||||
"uid" : 2,
|
||||
"passport" : "t2",
|
||||
"password" : "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname" : "T2",
|
||||
"create_time" : gtime.Now().String(),
|
||||
},
|
||||
{
|
||||
"id" : 3,
|
||||
"uid" : 3,
|
||||
"passport" : "t3",
|
||||
"password" : "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname" : "T3",
|
||||
"create_time" : gtime.Now().String(),
|
||||
},
|
||||
}).Batch(1).Insert()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
n, _ := result.RowsAffected()
|
||||
gtest.Assert(n, 2)
|
||||
})
|
||||
|
||||
// batch save
|
||||
gtest.Case(t, func() {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
result, err := db.Table(table).All()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(result), INIT_DATA_SIZE)
|
||||
for _, v := range result {
|
||||
v["nickname"].Set(v["nickname"].String() + v["id"].String())
|
||||
}
|
||||
r, e := db.Table(table).Data(result).Save()
|
||||
gtest.Assert(e, nil)
|
||||
n, e := r.RowsAffected()
|
||||
gtest.Assert(e, nil)
|
||||
gtest.Assert(n, INIT_DATA_SIZE*2)
|
||||
})
|
||||
|
||||
// batch replace
|
||||
gtest.Case(t, func() {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
result, err := db.Table(table).All()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(result), INIT_DATA_SIZE)
|
||||
for _, v := range result {
|
||||
v["nickname"].Set(v["nickname"].String() + v["id"].String())
|
||||
}
|
||||
r, e := db.Table(table).Data(result).Replace()
|
||||
gtest.Assert(e, nil)
|
||||
n, e := r.RowsAffected()
|
||||
gtest.Assert(e, nil)
|
||||
gtest.Assert(n, INIT_DATA_SIZE*2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestModel_Replace(t *testing.T) {
|
||||
@ -146,12 +183,23 @@ func TestModel_Save(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestModel_Update(t *testing.T) {
|
||||
result, err := db.Table("user").Data("passport", "t22").Where("passport=?", "t2").Update()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
n, _ := result.RowsAffected()
|
||||
gtest.Assert(n, 1)
|
||||
gtest.Case(t, func() {
|
||||
result, err := db.Table("user").Data("passport", "t22").Where("passport=?", "t2").Update()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
n, _ := result.RowsAffected()
|
||||
gtest.Assert(n, 1)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
result, err := db.Table("user").Data("passport", "t2").Where("passport='t22'").Update()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
n, _ := result.RowsAffected()
|
||||
gtest.Assert(n, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestModel_Clone(t *testing.T) {
|
||||
@ -175,9 +223,9 @@ func TestModel_Clone(t *testing.T) {
|
||||
gtest.Assert(result[1]["id"].Int(), 3)
|
||||
}
|
||||
|
||||
func TestModel_Alterable(t *testing.T) {
|
||||
func TestModel_Safe(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
md := db.Table("user").Alterable().Where("id IN(?)", g.Slice{1,3})
|
||||
md := db.Table("user").Safe(false).Where("id IN(?)", g.Slice{1,3})
|
||||
count, err := md.Count()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
@ -190,6 +238,20 @@ func TestModel_Alterable(t *testing.T) {
|
||||
}
|
||||
gtest.Assert(count, 1)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
md := db.Table("user").Safe(true).Where("id IN(?)", g.Slice{1,3})
|
||||
count, err := md.Count()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
gtest.Assert(count, 2)
|
||||
md.And("id = ?", 1)
|
||||
count, err = md.Count()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
gtest.Assert(count, 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestModel_All(t *testing.T) {
|
||||
@ -420,12 +482,52 @@ func TestModel_GroupBy(t *testing.T) {
|
||||
func TestModel_Where(t *testing.T) {
|
||||
// string
|
||||
gtest.Case(t, func() {
|
||||
result, err := db.Table("user").Where("id=? and nickname=?", 3, "T3").One()
|
||||
result, err := db.Table("user").Where("id=? and nickname=?", 3, "T3").One()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
gtest.AssertGT(len(result), 0)
|
||||
gtest.Assert(result["id"].Int(), 3)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
result, err := db.Table("user").Where("id", 3).One()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
gtest.AssertGT(len(result), 0)
|
||||
gtest.Assert(result["id"].Int(), 3)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
result, err := db.Table("user").Where("id", 3).Where("nickname", "T3").One()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
gtest.Assert(result["id"].Int(), 3)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
result, err := db.Table("user").Where("id", 3).And("nickname", "T3").One()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
gtest.Assert(result["id"].Int(), 3)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
result, err := db.Table("user").Where("id", 30).Or("nickname", "T3").One()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
gtest.Assert(result["id"].Int(), 3)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
result, err := db.Table("user").Where("id", 30).Or("nickname", "T3").And("id>?", 1).One()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(result["id"].Int(), 3)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
result, err := db.Table("user").Where("id", 30).Or("nickname", "T3").And("id>", 1).One()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(result["id"].Int(), 3)
|
||||
})
|
||||
// map
|
||||
gtest.Case(t, func() {
|
||||
result, err := db.Table("user").Where(g.Map{"id" : 3, "nickname" : "T3"}).One()
|
||||
@ -437,11 +539,39 @@ func TestModel_Where(t *testing.T) {
|
||||
// map key operator
|
||||
gtest.Case(t, func() {
|
||||
result, err := db.Table("user").Where(g.Map{"id>" : 1, "id<" : 3}).One()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(result["id"].Int(), 2)
|
||||
})
|
||||
// complicated where 1
|
||||
gtest.Case(t, func() {
|
||||
//db.SetDebug(true)
|
||||
conditions := g.Map{
|
||||
"nickname like ?" : "%T%",
|
||||
"id between ? and ?" : g.Slice{1,3},
|
||||
"id > 0" : nil,
|
||||
"create_time > 0" : nil,
|
||||
"id" : g.Slice{1,2,3},
|
||||
}
|
||||
result, err := db.Table("user").Where(conditions).OrderBy("id asc").All()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(result), 3)
|
||||
gtest.Assert(result[0]["id"].Int(), 1)
|
||||
})
|
||||
// complicated where 2
|
||||
gtest.Case(t, func() {
|
||||
//db.SetDebug(true)
|
||||
conditions := g.Map{
|
||||
"nickname like ?" : "%T%",
|
||||
"id between ? and ?" : g.Slice{1,3},
|
||||
"id >= ?" : 1,
|
||||
"create_time > ?" : 0,
|
||||
"id in(?)" : g.Slice{1,2,3},
|
||||
}
|
||||
result, err := db.Table("user").Where(conditions).OrderBy("id asc").All()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(result), 3)
|
||||
gtest.Assert(result[0]["id"].Int(), 1)
|
||||
})
|
||||
// struct
|
||||
gtest.Case(t, func() {
|
||||
type User struct {
|
||||
|
||||
@ -8,21 +8,21 @@
|
||||
package gjson
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gogf/gf/g/text/gregex"
|
||||
"strings"
|
||||
"strconv"
|
||||
"io/ioutil"
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/encoding/gtoml"
|
||||
"github.com/gogf/gf/g/encoding/gxml"
|
||||
"github.com/gogf/gf/g/encoding/gyaml"
|
||||
"github.com/gogf/gf/g/encoding/gtoml"
|
||||
"github.com/gogf/gf/g/text/gstr"
|
||||
"time"
|
||||
"github.com/gogf/gf/g/internal/rwmutex"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/gfcache"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/text/gregex"
|
||||
"github.com/gogf/gf/g/text/gstr"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -110,11 +110,7 @@ func DecodeToJson (b []byte) (*Json, error) {
|
||||
|
||||
// 支持多种配置文件类型转换为json格式内容并解析为gjson.Json对象
|
||||
func Load (path string) (*Json, error) {
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return LoadContent(data, gfile.Ext(path))
|
||||
return LoadContent(gfcache.GetBinContents(path), gfile.Ext(path))
|
||||
}
|
||||
|
||||
// 支持的配置文件格式:xml, json, yaml/yml, toml,
|
||||
@ -546,6 +542,11 @@ func (j *Json) Get(pattern...string) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 判断锁给定pattern是否数据存在
|
||||
func (j *Json) Contains(pattern...string) bool {
|
||||
return j.Get(pattern...) != nil
|
||||
}
|
||||
|
||||
// 计算指定pattern的元素长度(pattern对应数据类型为map[string]interface{}/[]interface{}时有效)
|
||||
func (j *Json) Len(pattern string) int {
|
||||
p := j.getPointerByPattern(pattern)
|
||||
|
||||
@ -73,7 +73,7 @@ func View(name...string) *gview.View {
|
||||
}
|
||||
key := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_VIEW, group)
|
||||
return instances.GetOrSetFuncLock(key, func() interface{} {
|
||||
path := cmdenv.Get("gf.gview.path", gfile.SelfDir()).String()
|
||||
path := cmdenv.Get("gf.gview.path", gfile.Pwd()).String()
|
||||
view := gview.New(path)
|
||||
// 添加基于源码的搜索目录检索地址,常用于开发环境调试,只添加入口文件目录
|
||||
if p := gfile.MainPkgPath(); p != "" && gfile.Exists(p) {
|
||||
@ -94,11 +94,22 @@ func Config(file...string) *gcfg.Config {
|
||||
}
|
||||
return instances.GetOrSetFuncLock(fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_CONFIG, configFile),
|
||||
func() interface{} {
|
||||
path := cmdenv.Get("gf.gcfg.path", gfile.SelfDir()).String()
|
||||
config := gcfg.New(path, configFile)
|
||||
// 添加基于源码的搜索目录检索地址,常用于开发环境调试,只添加入口文件目录
|
||||
if p := gfile.MainPkgPath(); p != "" && gfile.Exists(p) {
|
||||
config.AddPath(p)
|
||||
pwdPath := gfile.Pwd()
|
||||
envPath := cmdenv.Get("gf.gcfg.path").String()
|
||||
selfPath := gfile.SelfDir()
|
||||
mainPath := gfile.MainPkgPath()
|
||||
config := gcfg.New(pwdPath, configFile)
|
||||
// 自定义的环境变量/启动参数路径,优先级最高,覆盖默认的工作目录
|
||||
if envPath != "" && gfile.Exists(envPath) {
|
||||
config.SetPath(envPath)
|
||||
}
|
||||
// 二进制文件执行目录
|
||||
if selfPath != "" && gfile.Exists(selfPath) {
|
||||
config.AddPath(selfPath)
|
||||
}
|
||||
// 开发环境源码main包目录
|
||||
if mainPath != "" && gfile.Exists(mainPath) {
|
||||
config.AddPath(mainPath)
|
||||
}
|
||||
return config
|
||||
}).(*gcfg.Config)
|
||||
|
||||
43
g/frame/gins/gins_basic_test.go
Normal file
43
g/frame/gins/gins_basic_test.go
Normal file
@ -0,0 +1,43 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gins_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/frame/gins"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_SetGet(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gins.Set("test-user", 1)
|
||||
gtest.Assert(gins.Get("test-user"), 1)
|
||||
gtest.Assert(gins.Get("none-exists"), nil)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gins.GetOrSet("test-1", 1), 1)
|
||||
gtest.Assert(gins.Get("test-1"), 1)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gins.GetOrSetFunc("test-2", func() interface{} {
|
||||
return 2
|
||||
}), 2)
|
||||
gtest.Assert(gins.Get("test-2"), 2)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gins.GetOrSetFuncLock("test-3", func() interface{} {
|
||||
return 3
|
||||
}), 3)
|
||||
gtest.Assert(gins.Get("test-3"), 3)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gins.SetIfNotExist("test-4", 4), true)
|
||||
gtest.Assert(gins.Get("test-4"), 4)
|
||||
gtest.Assert(gins.SetIfNotExist("test-4", 5), false)
|
||||
gtest.Assert(gins.Get("test-4"), 4)
|
||||
})
|
||||
}
|
||||
166
g/frame/gins/gins_config_test.go
Normal file
166
g/frame/gins/gins_config_test.go
Normal file
@ -0,0 +1,166 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gins_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/frame/gins"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_Config(t *testing.T) {
|
||||
config := `
|
||||
# 模板引擎目录
|
||||
viewpath = "/home/www/templates/"
|
||||
test = "v=1"
|
||||
# MySQL数据库配置
|
||||
[database]
|
||||
[[database.default]]
|
||||
host = "127.0.0.1"
|
||||
port = "3306"
|
||||
user = "root"
|
||||
pass = ""
|
||||
name = "test"
|
||||
type = "mysql"
|
||||
role = "master"
|
||||
charset = "utf8"
|
||||
priority = "1"
|
||||
[[database.default]]
|
||||
host = "127.0.0.1"
|
||||
port = "3306"
|
||||
user = "root"
|
||||
pass = "8692651"
|
||||
name = "test"
|
||||
type = "mysql"
|
||||
role = "master"
|
||||
charset = "utf8"
|
||||
priority = "1"
|
||||
# Redis数据库配置
|
||||
[redis]
|
||||
disk = "127.0.0.1:6379,0"
|
||||
cache = "127.0.0.1:6379,1"
|
||||
`
|
||||
gtest.Case(t, func() {
|
||||
gtest.AssertNE(gins.Config(), nil)
|
||||
})
|
||||
// relative path
|
||||
gtest.Case(t, func() {
|
||||
path := "config.toml"
|
||||
err := gfile.PutContents(path, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(path)
|
||||
defer gins.Config().Reload()
|
||||
gtest.Assert(gins.Config().Get("test"), "v=1")
|
||||
gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
|
||||
gtest.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
|
||||
})
|
||||
// for gfsnotify callbacks to refresh cache of config file
|
||||
time.Sleep(500*time.Millisecond)
|
||||
|
||||
// relative path, config folder
|
||||
gtest.Case(t, func() {
|
||||
path := "config/config.toml"
|
||||
err := gfile.PutContents(path, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(path)
|
||||
defer gins.Config().Reload()
|
||||
gtest.Assert(gins.Config().Get("test"), "v=1")
|
||||
gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
|
||||
gtest.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
|
||||
})
|
||||
// for gfsnotify callbacks to refresh cache of config file
|
||||
time.Sleep(500*time.Millisecond)
|
||||
|
||||
gtest.Case(t, func() {
|
||||
path := "test.toml"
|
||||
err := gfile.PutContents(path, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(path)
|
||||
defer gins.Config().Reload()
|
||||
gtest.Assert(gins.Config("test.toml").Get("test"), "v=1")
|
||||
gtest.Assert(gins.Config("test.toml").Get("database.default.1.host"), "127.0.0.1")
|
||||
gtest.Assert(gins.Config("test.toml").Get("redis.disk"), "127.0.0.1:6379,0")
|
||||
})
|
||||
// for gfsnotify callbacks to refresh cache of config file
|
||||
time.Sleep(500*time.Millisecond)
|
||||
|
||||
gtest.Case(t, func() {
|
||||
path := "config/test.toml"
|
||||
err := gfile.PutContents(path, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(path)
|
||||
defer gins.Config().Reload()
|
||||
gtest.Assert(gins.Config("test.toml").Get("test"), "v=1")
|
||||
gtest.Assert(gins.Config("test.toml").Get("database.default.1.host"), "127.0.0.1")
|
||||
gtest.Assert(gins.Config("test.toml").Get("redis.disk"), "127.0.0.1:6379,0")
|
||||
})
|
||||
// for gfsnotify callbacks to refresh cache of config file
|
||||
time.Sleep(500*time.Millisecond)
|
||||
|
||||
|
||||
// absolute path
|
||||
gtest.Case(t, func() {
|
||||
path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.Nanosecond())
|
||||
file := fmt.Sprintf(`%s/%s`, path, "config.toml")
|
||||
err := gfile.PutContents(file, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(file)
|
||||
defer gins.Config().Reload()
|
||||
gtest.Assert(gins.Config().AddPath(path), nil)
|
||||
gtest.Assert(gins.Config().Get("test"), "v=1")
|
||||
gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
|
||||
gtest.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
|
||||
})
|
||||
time.Sleep(500*time.Millisecond)
|
||||
|
||||
gtest.Case(t, func() {
|
||||
path := fmt.Sprintf(`%s/%d/config`, gfile.TempDir(), gtime.Nanosecond())
|
||||
file := fmt.Sprintf(`%s/%s`, path, "config.toml")
|
||||
err := gfile.PutContents(file, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(file)
|
||||
defer gins.Config().Reload()
|
||||
gtest.Assert(gins.Config().AddPath(path), nil)
|
||||
gtest.Assert(gins.Config().Get("test"), "v=1")
|
||||
gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
|
||||
gtest.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
|
||||
})
|
||||
time.Sleep(500*time.Millisecond)
|
||||
|
||||
gtest.Case(t, func() {
|
||||
path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.Nanosecond())
|
||||
file := fmt.Sprintf(`%s/%s`, path, "test.toml")
|
||||
err := gfile.PutContents(file, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(file)
|
||||
defer gins.Config("test.toml").Reload()
|
||||
|
||||
gtest.Assert(gins.Config("test.toml").AddPath(path), nil)
|
||||
gtest.Assert(gins.Config("test.toml").Get("test"), "v=1")
|
||||
gtest.Assert(gins.Config("test.toml").Get("database.default.1.host"), "127.0.0.1")
|
||||
gtest.Assert(gins.Config("test.toml").Get("redis.disk"), "127.0.0.1:6379,0")
|
||||
})
|
||||
time.Sleep(500*time.Millisecond)
|
||||
|
||||
gtest.Case(t, func() {
|
||||
path := fmt.Sprintf(`%s/%d/config`, gfile.TempDir(), gtime.Nanosecond())
|
||||
file := fmt.Sprintf(`%s/%s`, path, "test.toml")
|
||||
err := gfile.PutContents(file, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(file)
|
||||
defer gins.Config("test.toml").Reload()
|
||||
|
||||
gtest.Assert(gins.Config("test.toml").AddPath(path), nil)
|
||||
gtest.Assert(gins.Config("test.toml").Get("test"), "v=1")
|
||||
gtest.Assert(gins.Config("test.toml").Get("database.default.1.host"), "127.0.0.1")
|
||||
gtest.Assert(gins.Config("test.toml").Get("redis.disk"), "127.0.0.1:6379,0")
|
||||
})
|
||||
}
|
||||
75
g/frame/gins/gins_database_test.go
Normal file
75
g/frame/gins/gins_database_test.go
Normal file
@ -0,0 +1,75 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gins_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/frame/gins"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_Database(t *testing.T) {
|
||||
config := `
|
||||
# 模板引擎目录
|
||||
viewpath = "/home/www/templates/"
|
||||
test = "v=2"
|
||||
# MySQL数据库配置
|
||||
[database]
|
||||
[[database.default]]
|
||||
host = "127.0.0.1"
|
||||
port = "3306"
|
||||
user = "root"
|
||||
pass = ""
|
||||
# pass = "12345678"
|
||||
name = "test"
|
||||
type = "mysql"
|
||||
role = "master"
|
||||
charset = "utf8"
|
||||
priority = "1"
|
||||
[[database.test]]
|
||||
host = "127.0.0.1"
|
||||
port = "3306"
|
||||
user = "root"
|
||||
pass = ""
|
||||
# pass = "12345678"
|
||||
name = "test"
|
||||
type = "mysql"
|
||||
role = "master"
|
||||
charset = "utf8"
|
||||
priority = "1"
|
||||
# Redis数据库配置
|
||||
[redis]
|
||||
default = "127.0.0.1:6379,0"
|
||||
cache = "127.0.0.1:6379,1"
|
||||
`
|
||||
path := "config.toml"
|
||||
err := gfile.PutContents(path, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(path)
|
||||
defer gins.Config().Reload()
|
||||
|
||||
// for gfsnotify callbacks to refresh cache of config file
|
||||
time.Sleep(500*time.Millisecond)
|
||||
|
||||
gtest.Case(t, func() {
|
||||
fmt.Println("gins Test_Database", gins.Config().Get("test"))
|
||||
|
||||
dbDefault := gins.Database()
|
||||
dbTest := gins.Database("test")
|
||||
gtest.AssertNE(dbDefault, nil)
|
||||
gtest.AssertNE(dbTest, nil)
|
||||
|
||||
gtest.Assert(dbDefault.PingMaster(), nil)
|
||||
gtest.Assert(dbDefault.PingSlave(), nil)
|
||||
gtest.Assert(dbTest.PingMaster(), nil)
|
||||
gtest.Assert(dbTest.PingSlave(), nil)
|
||||
})
|
||||
}
|
||||
|
||||
78
g/frame/gins/gins_redis_test.go
Normal file
78
g/frame/gins/gins_redis_test.go
Normal file
@ -0,0 +1,78 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gins_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/frame/gins"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_Redis(t *testing.T) {
|
||||
config := `
|
||||
# 模板引擎目录
|
||||
viewpath = "/home/www/templates/"
|
||||
test = "v=3"
|
||||
# MySQL数据库配置
|
||||
[database]
|
||||
[[database.default]]
|
||||
host = "127.0.0.1"
|
||||
port = "3306"
|
||||
user = "root"
|
||||
pass = ""
|
||||
# pass = "12345678"
|
||||
name = "test"
|
||||
type = "mysql"
|
||||
role = "master"
|
||||
charset = "utf8"
|
||||
priority = "1"
|
||||
[[database.test]]
|
||||
host = "127.0.0.1"
|
||||
port = "3306"
|
||||
user = "root"
|
||||
pass = ""
|
||||
# pass = "12345678"
|
||||
name = "test"
|
||||
type = "mysql"
|
||||
role = "master"
|
||||
charset = "utf8"
|
||||
priority = "1"
|
||||
# Redis数据库配置
|
||||
[redis]
|
||||
default = "127.0.0.1:6379,0"
|
||||
cache = "127.0.0.1:6379,1"
|
||||
`
|
||||
path := "config.toml"
|
||||
err := gfile.PutContents(path, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(path)
|
||||
defer gins.Config().Reload()
|
||||
|
||||
// for gfsnotify callbacks to refresh cache of config file
|
||||
time.Sleep(500*time.Millisecond)
|
||||
|
||||
gtest.Case(t, func() {
|
||||
fmt.Println("gins Test_Redis", gins.Config().Get("test"))
|
||||
|
||||
redisDefault := gins.Redis()
|
||||
redisCache := gins.Redis("cache")
|
||||
gtest.AssertNE(redisDefault, nil)
|
||||
gtest.AssertNE(redisCache, nil)
|
||||
|
||||
r, err := redisDefault.Do("PING")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(r, "PONG")
|
||||
|
||||
r, err = redisCache.Do("PING")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(r, "PONG")
|
||||
})
|
||||
}
|
||||
|
||||
49
g/frame/gins/gins_view_test.go
Normal file
49
g/frame/gins/gins_view_test.go
Normal file
@ -0,0 +1,49 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gins_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/frame/gins"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_View(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.AssertNE(gins.View(), nil)
|
||||
b, e := gins.View().ParseContent(`{{"我是中国人" | substr 2 -1}}`, nil)
|
||||
gtest.Assert(e, nil)
|
||||
gtest.Assert(string(b), "中国人")
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
tpl := "t.tpl"
|
||||
err := gfile.PutContents(tpl, `{{"我是中国人" | substr 2 -1}}`)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(tpl)
|
||||
|
||||
b, e := gins.View().Parse("t.tpl", nil)
|
||||
gtest.Assert(e, nil)
|
||||
gtest.Assert(string(b), "中国人")
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.Nanosecond())
|
||||
tpl := fmt.Sprintf(`%s/%s`, path, "t.tpl")
|
||||
err := gfile.PutContents(tpl, `{{"我是中国人" | substr 2 -1}}`)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(tpl)
|
||||
err = gins.View().AddPath(path)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
b, e := gins.View().Parse("t.tpl", nil)
|
||||
gtest.Assert(e, nil)
|
||||
gtest.Assert(string(b), "中国人")
|
||||
})
|
||||
}
|
||||
|
||||
3
g/g.go
3
g/g.go
@ -16,7 +16,8 @@ type Var = gvar.Var
|
||||
// Frequently-used map type alias.
|
||||
//
|
||||
// 常用map数据结构(使用别名)
|
||||
type Map = map[interface{}]interface{}
|
||||
type Map = map[string]interface{}
|
||||
type MapAnyAny = map[interface{}]interface{}
|
||||
type MapAnyStr = map[interface{}]string
|
||||
type MapAnyInt = map[interface{}]int
|
||||
type MapStrAny = map[string]interface{}
|
||||
|
||||
@ -17,7 +17,7 @@ import (
|
||||
// 规则:
|
||||
// 1、命令行参数以小写字母格式,使用: gf.包名.变量名 传递;
|
||||
// 2、环境变量参数以大写字母格式,使用: GF_包名_变量名 传递;
|
||||
func Get(key string, def...interface{}) *gvar.Var {
|
||||
func Get(key string, def...interface{}) gvar.VarRead {
|
||||
value := interface{}(nil)
|
||||
if len(def) > 0 {
|
||||
value = def[0]
|
||||
|
||||
@ -4,5 +4,7 @@
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
// Package ghttp provides quite powerful HTTP server and simple client implementations.
|
||||
// Package ghttp provides a powerful http server and a simple client.
|
||||
//
|
||||
// ghttp是GF框架的核心模块,实现了一个强大的Web Server,并提供了一个简便的HTTP客户端。
|
||||
package ghttp
|
||||
|
||||
@ -45,13 +45,10 @@ func (r *Response) Write(content ... interface{}) {
|
||||
return
|
||||
}
|
||||
for _, v := range content {
|
||||
switch v.(type) {
|
||||
case []byte:
|
||||
// 如果是二进制数据,那么返回二进制数据
|
||||
r.buffer.Write(gconv.Bytes(v))
|
||||
|
||||
switch value := v.(type) {
|
||||
case []byte: r.buffer.Write(value)
|
||||
case string: r.buffer.WriteString(value)
|
||||
default:
|
||||
// 否则一律按照可显示的字符串进行转换
|
||||
r.buffer.WriteString(gconv.String(v))
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,10 +13,10 @@ import (
|
||||
)
|
||||
|
||||
// 展示模板,可以给定模板参数,及临时的自定义模板函数
|
||||
func (r *Response) WriteTpl(tpl string, params map[string]interface{}, funcmap...map[string]interface{}) error {
|
||||
func (r *Response) WriteTpl(tpl string, params map[string]interface{}, funcMap...map[string]interface{}) error {
|
||||
fmap := make(gview.FuncMap)
|
||||
if len(funcmap) > 0 {
|
||||
fmap = funcmap[0]
|
||||
if len(funcMap) > 0 {
|
||||
fmap = funcMap[0]
|
||||
}
|
||||
if b, err := r.ParseTpl(tpl, params, fmap); err != nil {
|
||||
r.Write("Tpl Parsing Error: " + err.Error())
|
||||
@ -28,10 +28,10 @@ func (r *Response) WriteTpl(tpl string, params map[string]interface{}, funcmap..
|
||||
}
|
||||
|
||||
// 展示模板内容,可以给定模板参数,及临时的自定义模板函数
|
||||
func (r *Response) WriteTplContent(content string, params map[string]interface{}, funcmap...map[string]interface{}) error {
|
||||
func (r *Response) WriteTplContent(content string, params map[string]interface{}, funcMap...map[string]interface{}) error {
|
||||
fmap := make(gview.FuncMap)
|
||||
if len(funcmap) > 0 {
|
||||
fmap = funcmap[0]
|
||||
if len(funcMap) > 0 {
|
||||
fmap = funcMap[0]
|
||||
}
|
||||
if b, err := r.ParseTplContent(content, params, fmap); err != nil {
|
||||
r.Write("Tpl Parsing Error: " + err.Error())
|
||||
@ -43,19 +43,19 @@ func (r *Response) WriteTplContent(content string, params map[string]interface{}
|
||||
}
|
||||
|
||||
// 解析模板文件,并返回模板内容
|
||||
func (r *Response) ParseTpl(tpl string, params gview.Params, funcmap...map[string]interface{}) ([]byte, error) {
|
||||
func (r *Response) ParseTpl(tpl string, params gview.Params, funcMap...map[string]interface{}) ([]byte, error) {
|
||||
fmap := make(gview.FuncMap)
|
||||
if len(funcmap) > 0 {
|
||||
fmap = funcmap[0]
|
||||
if len(funcMap) > 0 {
|
||||
fmap = funcMap[0]
|
||||
}
|
||||
return gins.View().Parse(tpl, r.buildInVars(params), r.buildInFuncs(fmap))
|
||||
}
|
||||
|
||||
// 解析并返回模板内容
|
||||
func (r *Response) ParseTplContent(content string, params gview.Params, funcmap...map[string]interface{}) ([]byte, error) {
|
||||
func (r *Response) ParseTplContent(content string, params gview.Params, funcMap...map[string]interface{}) ([]byte, error) {
|
||||
fmap := make(gview.FuncMap)
|
||||
if len(funcmap) > 0 {
|
||||
fmap = funcmap[0]
|
||||
if len(funcMap) > 0 {
|
||||
fmap = funcMap[0]
|
||||
}
|
||||
return gins.View().ParseContent(content, r.buildInVars(params), r.buildInFuncs(fmap))
|
||||
}
|
||||
@ -77,14 +77,14 @@ func (r *Response) buildInVars(params map[string]interface{}) map[string]interfa
|
||||
}
|
||||
|
||||
// 内置函数
|
||||
func (r *Response) buildInFuncs(funcmap map[string]interface{}) map[string]interface{} {
|
||||
if funcmap == nil {
|
||||
funcmap = make(map[string]interface{})
|
||||
func (r *Response) buildInFuncs(funcMap map[string]interface{}) map[string]interface{} {
|
||||
if funcMap == nil {
|
||||
funcMap = make(map[string]interface{})
|
||||
}
|
||||
funcmap["get"] = r.funcGet
|
||||
funcmap["post"] = r.funcPost
|
||||
funcmap["request"] = r.funcRequest
|
||||
return funcmap
|
||||
funcMap["get"] = r.funcGet
|
||||
funcMap["post"] = r.funcPost
|
||||
funcMap["request"] = r.funcRequest
|
||||
return funcMap
|
||||
}
|
||||
|
||||
// 模板内置函数: get
|
||||
|
||||
@ -24,7 +24,7 @@ const (
|
||||
NAME_TO_URI_TYPE_CAMEL = 3 // 采用驼峰命名方式
|
||||
gDEFAULT_COOKIE_PATH = "/" // 默认path
|
||||
gDEFAULT_COOKIE_MAX_AGE = 86400*365 // 默认cookie有效期(一年)
|
||||
gDEFAULT_SESSION_MAX_AGE = 600 // 默认session有效期(600秒)
|
||||
gDEFAULT_SESSION_MAX_AGE = 600000 // 默认session有效期(600秒)
|
||||
gDEFAULT_SESSION_ID_NAME = "gfsessionid" // 默认存放Cookie中的SessionId名称
|
||||
gCHANGE_CONFIG_WHILE_RUNNING_ERROR = "cannot be changed while running"
|
||||
)
|
||||
|
||||
@ -64,6 +64,7 @@ func (s *Server)SetServerRoot(root string) {
|
||||
if path == "" {
|
||||
glog.Fatal(fmt.Sprintf(`[ghttp] SetServerRoot failed: path "%s" does not exist`, root))
|
||||
}
|
||||
glog.Debug("[ghttp] SetServerRoot path:", path)
|
||||
s.config.SearchPaths = []string{strings.TrimRight(path, gfile.Separator)}
|
||||
s.config.FileServerEnabled = true
|
||||
}
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
//
|
||||
// HTTP Cookie管理对象,
|
||||
// 由于Cookie是和HTTP请求挂钩的,因此被包含到 ghttp 包中进行管理
|
||||
// 由于Cookie是和HTTP请求挂钩的,因此被包含到 ghttp 包中进行管理。
|
||||
|
||||
package ghttp
|
||||
|
||||
@ -87,6 +87,14 @@ func (c *Cookie) SessionId() string {
|
||||
return id
|
||||
}
|
||||
|
||||
// 获取SessionId,不存在时则创建
|
||||
func (c *Cookie) MakeSessionId() string {
|
||||
c.init()
|
||||
id := makeSessionId()
|
||||
c.SetSessionId(id)
|
||||
return id
|
||||
}
|
||||
|
||||
// 判断Cookie中是否存在制定键名(并且没有过期)
|
||||
func (c *Cookie) Contains(key string) bool {
|
||||
c.init()
|
||||
|
||||
@ -75,6 +75,10 @@ func (s *Server) setHandler(pattern string, handler *handlerItem, hook ... strin
|
||||
glog.Error("invalid pattern:", pattern)
|
||||
return
|
||||
}
|
||||
if len(uri) == 0 || uri[0] != '/' {
|
||||
glog.Error("invalid pattern:", pattern)
|
||||
return
|
||||
}
|
||||
// 注册地址记录及重复注册判断
|
||||
regkey := s.handlerKey(hookName, method, uri, domain)
|
||||
caller := s.getHandlerRegisterCallerLine(handler)
|
||||
|
||||
@ -26,9 +26,9 @@ type Session struct {
|
||||
request *Request // 关联的请求
|
||||
}
|
||||
|
||||
// 生成一个唯一的SessionId字符串,长度16位
|
||||
// 生成一个唯一的SessionId字符串,长度18位。
|
||||
func makeSessionId() string {
|
||||
return strings.ToUpper(strconv.FormatInt(gtime.Nanosecond(), 32) + grand.RandStr(3))
|
||||
return strings.ToUpper(strconv.FormatInt(gtime.Nanosecond(), 36) + grand.RandStr(6))
|
||||
}
|
||||
|
||||
// 获取或者生成一个session对象(延迟初始化)
|
||||
@ -41,14 +41,24 @@ func GetSession(r *Request) *Session {
|
||||
}
|
||||
}
|
||||
|
||||
// 执行初始化(用于延迟初始化)
|
||||
// 执行初始化(用于延迟初始化).
|
||||
func (s *Session) init() {
|
||||
if len(s.id) == 0 {
|
||||
s.id = s.request.Cookie.SessionId()
|
||||
s.server = s.request.Server
|
||||
s.data = s.server.sessions.GetOrSetFuncLock(s.id, func() interface{} {
|
||||
return gmap.NewStringInterfaceMap()
|
||||
}, s.server.GetSessionMaxAge()).(*gmap.StringInterfaceMap)
|
||||
// 根据提交的SESSION ID获取已存在SESSION
|
||||
id := s.request.Cookie.GetSessionId()
|
||||
if id != "" {
|
||||
data := s.server.sessions.Get(id)
|
||||
if data != nil {
|
||||
s.id = id
|
||||
s.data = data.(*gmap.StringInterfaceMap)
|
||||
return
|
||||
}
|
||||
}
|
||||
// 否则执行初始化创建
|
||||
s.id = s.request.Cookie.MakeSessionId()
|
||||
s.data = gmap.NewStringInterfaceMap()
|
||||
s.server.sessions.Set(s.id, s.data, s.server.GetSessionMaxAge())
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,8 +104,8 @@ func (s *Session) Contains (key string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// 获取SESSION
|
||||
func (s *Session) Get (key string) interface{} {
|
||||
// 获取SESSION变量
|
||||
func (s *Session) Get(key string) interface{} {
|
||||
if len(s.id) > 0 || s.request.Cookie.GetSessionId() != "" {
|
||||
s.init()
|
||||
return s.data.Get(key)
|
||||
@ -131,112 +141,95 @@ func (s *Session) UpdateExpire() {
|
||||
}
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetString(key string) string {
|
||||
return gconv.String(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetBool(key string) bool {
|
||||
return gconv.Bool(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetInt(key string) int {
|
||||
return gconv.Int(s.Get(key)) }
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
|
||||
func (s *Session) GetInt8(key string) int8 {
|
||||
return gconv.Int8(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetInt16(key string) int16 {
|
||||
return gconv.Int16(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetInt32(key string) int32 {
|
||||
return gconv.Int32(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetInt64(key string) int64 {
|
||||
return gconv.Int64(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetUint(key string) uint {
|
||||
return gconv.Uint(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetUint8(key string) uint8 {
|
||||
return gconv.Uint8(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetUint16(key string) uint16 {
|
||||
return gconv.Uint16(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetUint32(key string) uint32 {
|
||||
return gconv.Uint32(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetUint64(key string) uint64 {
|
||||
return gconv.Uint64(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetFloat32(key string) float32 {
|
||||
return gconv.Float32(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetFloat64(key string) float64 {
|
||||
return gconv.Float64(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetBytes(key string) []byte {
|
||||
return gconv.Bytes(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetInts(key string) []int {
|
||||
return gconv.Ints(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetFloats(key string) []float64 {
|
||||
return gconv.Floats(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetStrings(key string) []string {
|
||||
return gconv.Strings(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetInterfaces(key string) []interface{} {
|
||||
return gconv.Interfaces(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetTime(key string, format...string) time.Time {
|
||||
return gconv.Time(s.Get(key), format...)
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
func (s *Session) GetGTime(key string, format...string) *gtime.Time {
|
||||
return gconv.GTime(s.Get(key), format...)
|
||||
}
|
||||
|
||||
func (s *Session) GetTimeDuration(key string) time.Duration {
|
||||
return gconv.TimeDuration(s.Get(key))
|
||||
}
|
||||
|
||||
// Deprecated, use GetVar instead.
|
||||
// (已废弃, 请使用GetVar) 将变量转换为对象,注意 objPointer 参数必须为struct指针
|
||||
// 将变量转换为对象,注意 objPointer 参数必须为struct指针
|
||||
func (s *Session) GetStruct(key string, objPointer interface{}, attrMapping...map[string]string) error {
|
||||
return gconv.Struct(s.Get(key), objPointer, attrMapping...)
|
||||
}
|
||||
|
||||
@ -17,7 +17,7 @@ var (
|
||||
)
|
||||
|
||||
func init() {
|
||||
for i := 8000; i <= 8100; i++ {
|
||||
for i := 8000; i <= 9000; i++ {
|
||||
ports.Append(i)
|
||||
}
|
||||
}
|
||||
|
||||
126
g/net/ghttp/ghttp_unit_router_exit_test.go
Normal file
126
g/net/ghttp/ghttp_unit_router_exit_test.go
Normal file
@ -0,0 +1,126 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package ghttp_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_Router_Exit(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHookHandlerByMap("/*", map[string]ghttp.HandlerFunc{
|
||||
"BeforeServe" : func(r *ghttp.Request){ r.Response.Write("1") },
|
||||
"AfterServe" : func(r *ghttp.Request){ r.Response.Write("2") },
|
||||
"BeforeOutput" : func(r *ghttp.Request){ r.Response.Write("3") },
|
||||
"AfterOutput" : func(r *ghttp.Request){ r.Response.Write("4") },
|
||||
"BeforeClose" : func(r *ghttp.Request){ r.Response.Write("5") },
|
||||
"AfterClose" : func(r *ghttp.Request){ r.Response.Write("6") },
|
||||
})
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test-start")
|
||||
r.Exit()
|
||||
r.Response.Write("test-end")
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "123")
|
||||
gtest.Assert(client.GetContent("/test/test"), "1test-start23")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Router_ExitHook(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/priority/show", func(r *ghttp.Request) {
|
||||
r.Response.Write("show")
|
||||
})
|
||||
|
||||
s.BindHookHandlerByMap("/priority/:name", map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.ExitHook()
|
||||
},
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/priority/show"), "3show")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Router_ExitAll(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/priority/show", func(r *ghttp.Request) {
|
||||
r.Response.Write("show")
|
||||
})
|
||||
|
||||
s.BindHookHandlerByMap("/priority/:name", map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.ExitAll()
|
||||
},
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/priority/show"), "3")
|
||||
})
|
||||
}
|
||||
87
g/net/ghttp/ghttp_unit_router_hook_test.go
Normal file
87
g/net/ghttp/ghttp_unit_router_hook_test.go
Normal file
@ -0,0 +1,87 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package ghttp_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_Router_Hook_Basic(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHookHandlerByMap("/*", map[string]ghttp.HandlerFunc{
|
||||
"BeforeServe" : func(r *ghttp.Request){ r.Response.Write("1") },
|
||||
"AfterServe" : func(r *ghttp.Request){ r.Response.Write("2") },
|
||||
"BeforeOutput" : func(r *ghttp.Request){ r.Response.Write("3") },
|
||||
"AfterOutput" : func(r *ghttp.Request){ r.Response.Write("4") },
|
||||
"BeforeClose" : func(r *ghttp.Request){ r.Response.Write("5") },
|
||||
"AfterClose" : func(r *ghttp.Request){ r.Response.Write("6") },
|
||||
})
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "123")
|
||||
gtest.Assert(client.GetContent("/test/test"), "1test23")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Router_Hook_Priority(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/priority/show", func(r *ghttp.Request) {
|
||||
r.Response.Write("show")
|
||||
})
|
||||
|
||||
s.BindHookHandlerByMap("/priority/:name", map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
},
|
||||
})
|
||||
s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
},
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/priority/show"), "312show")
|
||||
gtest.Assert(client.GetContent("/priority/any/any"), "2")
|
||||
gtest.Assert(client.GetContent("/priority/name"), "12")
|
||||
})
|
||||
}
|
||||
|
||||
@ -6,3 +6,107 @@
|
||||
|
||||
package ghttp_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type NamesObject struct {}
|
||||
|
||||
func (o *NamesObject) ShowName(r *ghttp.Request) {
|
||||
r.Response.Write("Object Show Name")
|
||||
}
|
||||
|
||||
func Test_NameToUri_FullName(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_FULLNAME)
|
||||
s.BindObject("/{.struct}/{.method}", new(NamesObject))
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetBrowserMode(true)
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
gtest.Assert(client.GetContent("/"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/NamesObject"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/NamesObject/ShowName"), "Object Show Name")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func Test_NameToUri_AllLower(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_ALLLOWER)
|
||||
s.BindObject("/{.struct}/{.method}", new(NamesObject))
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetBrowserMode(true)
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
gtest.Assert(client.GetContent("/"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/NamesObject"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/namesobject/showname"), "Object Show Name")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_NameToUri_Camel(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_CAMEL)
|
||||
s.BindObject("/{.struct}/{.method}", new(NamesObject))
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetBrowserMode(true)
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
gtest.Assert(client.GetContent("/"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/NamesObject"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/namesObject/showName"), "Object Show Name")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_NameToUri_Default(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_DEFAULT)
|
||||
s.BindObject("/{.struct}/{.method}", new(NamesObject))
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetBrowserMode(true)
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
gtest.Assert(client.GetContent("/"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/NamesObject"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/names-object/show-name"), "Object Show Name")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
272
g/net/ghttp/ghttp_unit_static_test.go
Normal file
272
g/net/ghttp/ghttp_unit_static_test.go
Normal file
@ -0,0 +1,272 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
// 静态文件服务测试
|
||||
package ghttp_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_Static_ServerRoot(t *testing.T) {
|
||||
// SetServerRoot with absolute path
|
||||
gtest.Case(t, func() {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
|
||||
defer gfile.Remove(path)
|
||||
gfile.PutContents(path + "/index.htm", "index")
|
||||
s.SetServerRoot(path)
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "index")
|
||||
gtest.Assert(client.GetContent("/index.htm"), "index")
|
||||
})
|
||||
|
||||
// SetServerRoot with relative path
|
||||
gtest.Case(t, func() {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
path := fmt.Sprintf(`static/test/%d`, p)
|
||||
defer gfile.Remove(path)
|
||||
gfile.PutContents(path + "/index.htm", "index")
|
||||
s.SetServerRoot(path)
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "index")
|
||||
gtest.Assert(client.GetContent("/index.htm"), "index")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Static_Folder_Forbidden(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
|
||||
defer gfile.Remove(path)
|
||||
gfile.PutContents(path + "/test.html", "test")
|
||||
s.SetServerRoot(path)
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "Forbidden")
|
||||
gtest.Assert(client.GetContent("/index.html"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/test.html"), "test")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Static_IndexFolder(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
|
||||
defer gfile.Remove(path)
|
||||
gfile.PutContents(path + "/test.html", "test")
|
||||
s.SetIndexFolder(true)
|
||||
s.SetServerRoot(path)
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.AssertNE(client.GetContent("/"), "Forbidden")
|
||||
gtest.Assert(client.GetContent("/index.html"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/test.html"), "test")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Static_IndexFiles1(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
|
||||
defer gfile.Remove(path)
|
||||
gfile.PutContents(path + "/index.html", "index")
|
||||
gfile.PutContents(path + "/test.html", "test")
|
||||
s.SetServerRoot(path)
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "index")
|
||||
gtest.Assert(client.GetContent("/index.html"), "index")
|
||||
gtest.Assert(client.GetContent("/test.html"), "test")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Static_IndexFiles2(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
|
||||
defer gfile.Remove(path)
|
||||
gfile.PutContents(path + "/test.html", "test")
|
||||
s.SetIndexFiles([]string{"index.html", "test.html"})
|
||||
s.SetServerRoot(path)
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "test")
|
||||
gtest.Assert(client.GetContent("/index.html"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/test.html"), "test")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Static_AddSearchPath1(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
path1 := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
|
||||
path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d`, gfile.TempDir(), p, p)
|
||||
defer gfile.Remove(path1)
|
||||
defer gfile.Remove(path2)
|
||||
gfile.PutContents(path2 + "/test.html", "test")
|
||||
s.SetServerRoot(path1)
|
||||
s.AddSearchPath(path2)
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "Forbidden")
|
||||
gtest.Assert(client.GetContent("/test.html"), "test")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Static_AddSearchPath2(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
path1 := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
|
||||
path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d`, gfile.TempDir(), p, p)
|
||||
defer gfile.Remove(path1)
|
||||
defer gfile.Remove(path2)
|
||||
gfile.PutContents(path1 + "/test.html", "test1")
|
||||
gfile.PutContents(path2 + "/test.html", "test2")
|
||||
s.SetServerRoot(path1)
|
||||
s.AddSearchPath(path2)
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "Forbidden")
|
||||
gtest.Assert(client.GetContent("/test.html"), "test1")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Static_AddStaticPath(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
path1 := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
|
||||
path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d`, gfile.TempDir(), p, p)
|
||||
defer gfile.Remove(path1)
|
||||
defer gfile.Remove(path2)
|
||||
gfile.PutContents(path1 + "/test.html", "test1")
|
||||
gfile.PutContents(path2 + "/test.html", "test2")
|
||||
s.SetServerRoot(path1)
|
||||
s.AddStaticPath("/my-test", path2)
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "Forbidden")
|
||||
gtest.Assert(client.GetContent("/test.html"), "test1")
|
||||
gtest.Assert(client.GetContent("/my-test/test.html"), "test2")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Static_AddStaticPath_Priority(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
path1 := fmt.Sprintf(`%s/ghttp/static/test/%d/test`, gfile.TempDir(), p)
|
||||
path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d/test`, gfile.TempDir(), p, p)
|
||||
defer gfile.Remove(path1)
|
||||
defer gfile.Remove(path2)
|
||||
gfile.PutContents(path1 + "/test.html", "test1")
|
||||
gfile.PutContents(path2 + "/test.html", "test2")
|
||||
s.SetServerRoot(path1)
|
||||
s.AddStaticPath("/test", path2)
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "Forbidden")
|
||||
gtest.Assert(client.GetContent("/test.html"), "test1")
|
||||
gtest.Assert(client.GetContent("/test/test.html"), "test2")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Static_Rewrite(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
|
||||
defer gfile.Remove(path)
|
||||
gfile.PutContents(path + "/test1.html", "test1")
|
||||
gfile.PutContents(path + "/test2.html", "test2")
|
||||
s.SetServerRoot(path)
|
||||
s.SetRewrite("/test.html", "/test1.html")
|
||||
s.SetRewriteMap(g.MapStrStr{
|
||||
"/my-test1" : "/test1.html",
|
||||
"/my-test2" : "/test2.html",
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.Assert(client.GetContent("/"), "Forbidden")
|
||||
gtest.Assert(client.GetContent("/test.html"), "test1")
|
||||
gtest.Assert(client.GetContent("/test1.html"), "test1")
|
||||
gtest.Assert(client.GetContent("/test2.html"), "test2")
|
||||
gtest.Assert(client.GetContent("/my-test1"), "test1")
|
||||
gtest.Assert(client.GetContent("/my-test2"), "test2")
|
||||
})
|
||||
}
|
||||
@ -31,15 +31,15 @@ var serverMapping = gmap.NewStringInterfaceMap()
|
||||
// 获取/创建一个空配置的TCP Server
|
||||
// 单例模式,请保证name的唯一性
|
||||
func GetServer(name...interface{}) (*Server) {
|
||||
sname := gDEFAULT_SERVER
|
||||
serverName := gDEFAULT_SERVER
|
||||
if len(name) > 0 {
|
||||
sname = gconv.String(name[0])
|
||||
serverName = gconv.String(name[0])
|
||||
}
|
||||
if s := serverMapping.Get(sname); s != nil {
|
||||
if s := serverMapping.Get(serverName); s != nil {
|
||||
return s.(*Server)
|
||||
}
|
||||
s := NewServer("", nil)
|
||||
serverMapping.Set(sname, s)
|
||||
serverMapping.Set(serverName, s)
|
||||
return s
|
||||
}
|
||||
|
||||
@ -65,19 +65,24 @@ func (s *Server) SetHandler (handler func (*Conn)) {
|
||||
// 执行监听
|
||||
func (s *Server) Run() error {
|
||||
if s.handler == nil {
|
||||
return errors.New("start running failed: socket handler not defined")
|
||||
}
|
||||
tcpaddr, err := net.ResolveTCPAddr("tcp", s.address)
|
||||
if err != nil {
|
||||
err := errors.New("start running failed: socket handler not defined")
|
||||
glog.Error(err)
|
||||
return err
|
||||
}
|
||||
listen, err := net.ListenTCP("tcp", tcpaddr)
|
||||
addr, err := net.ResolveTCPAddr("tcp", s.address)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return err
|
||||
}
|
||||
listen, err := net.ListenTCP("tcp", addr)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return err
|
||||
}
|
||||
for {
|
||||
if conn, err := listen.Accept(); err != nil {
|
||||
glog.Error(err)
|
||||
return err
|
||||
} else if conn != nil {
|
||||
go s.handler(NewConnByNetConn(conn))
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
package gudp
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"net"
|
||||
"errors"
|
||||
"github.com/gogf/gf/g/container/gmap"
|
||||
@ -30,15 +31,15 @@ var serverMapping = gmap.NewStringInterfaceMap()
|
||||
// 获取/创建一个空配置的UDP Server
|
||||
// 单例模式,请保证name的唯一性
|
||||
func GetServer(name...interface{}) (*Server) {
|
||||
sname := gDEFAULT_SERVER
|
||||
serverName := gDEFAULT_SERVER
|
||||
if len(name) > 0 {
|
||||
sname = gconv.String(name[0])
|
||||
serverName = gconv.String(name[0])
|
||||
}
|
||||
if s := serverMapping.Get(sname); s != nil {
|
||||
if s := serverMapping.Get(serverName); s != nil {
|
||||
return s.(*Server)
|
||||
}
|
||||
s := NewServer("", nil)
|
||||
serverMapping.Set(sname, s)
|
||||
serverMapping.Set(serverName, s)
|
||||
return s
|
||||
}
|
||||
|
||||
@ -64,14 +65,18 @@ func (s *Server) SetHandler (handler func (*Conn)) {
|
||||
// 执行监听
|
||||
func (s *Server) Run() error {
|
||||
if s.handler == nil {
|
||||
return errors.New("start running failed: socket handler not defined")
|
||||
err := errors.New("start running failed: socket handler not defined")
|
||||
glog.Error(err)
|
||||
return err
|
||||
}
|
||||
addr, err := net.ResolveUDPAddr("udp", s.address)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return err
|
||||
}
|
||||
conn, err := net.ListenUDP("udp", addr)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return err
|
||||
}
|
||||
for {
|
||||
|
||||
@ -26,7 +26,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
DEFAULT_CONFIG_FILE = "config.toml" // 默认的配置管理文件名称
|
||||
// 默认的配置管理文件名称
|
||||
DEFAULT_CONFIG_FILE = "config.toml"
|
||||
)
|
||||
|
||||
// 配置管理对象
|
||||
@ -65,13 +66,7 @@ func (c *Config) filePath(file...string) (path string) {
|
||||
if len(file) > 0 {
|
||||
name = file[0]
|
||||
}
|
||||
c.paths.RLockFunc(func(array []string) {
|
||||
for _, v := range array {
|
||||
if path, _ = gspath.Search(v, name); path != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
path = c.GetFilePath(name)
|
||||
if path == "" {
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
if c.paths.Len() > 0 {
|
||||
@ -132,7 +127,8 @@ func (c *Config) AddPath(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取指定文件的绝对路径,默认获取默认的配置文件路径,当指定的配置文件不存在时,返回空字符串,并且不会报错。
|
||||
// 查找配置文件,获取指定配置文件的绝对路径,默认获取默认的配置文件路径;
|
||||
// 当指定的配置文件不存在时,返回空字符串,并且不会报错。
|
||||
func (c *Config) GetFilePath(file...string) (path string) {
|
||||
name := c.name.Val()
|
||||
if len(file) > 0 {
|
||||
@ -140,9 +136,14 @@ func (c *Config) GetFilePath(file...string) (path string) {
|
||||
}
|
||||
c.paths.RLockFunc(func(array []string) {
|
||||
for _, v := range array {
|
||||
// 查找当前目录
|
||||
if path, _ = gspath.Search(v, name); path != "" {
|
||||
break
|
||||
}
|
||||
// 查找当前目录下的config子目录
|
||||
if path, _ = gspath.Search(v, "config" + gfile.Separator + name); path != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
@ -190,6 +191,14 @@ func (c *Config) GetVar(pattern string, file...string) gvar.VarRead {
|
||||
return gvar.New(nil, true)
|
||||
}
|
||||
|
||||
// 判断指定的配置项是否存在
|
||||
func (c *Config) Contains(pattern string, file...string) bool {
|
||||
if j := c.getJson(file...); j != nil {
|
||||
return j.Contains(pattern)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 获得一个键值对关联数组/哈希表,方便操作,不需要自己做类型转换
|
||||
// 注意,如果获取的值不存在,或者类型与json类型不匹配,那么将会返回nil
|
||||
func (c *Config) GetMap(pattern string, file...string) map[string]interface{} {
|
||||
|
||||
@ -39,14 +39,14 @@ func TestCron_Add_Close(t *testing.T) {
|
||||
gtest.AssertNE(err3, nil)
|
||||
gtest.Assert(err4, nil)
|
||||
gtest.Assert(cron.Size(), 3)
|
||||
time.Sleep(1100*time.Millisecond)
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
time.Sleep(1100*time.Millisecond)
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
gtest.Assert(array.Len(), 5)
|
||||
cron.Close()
|
||||
time.Sleep(1100*time.Millisecond)
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
fixedLength := array.Len()
|
||||
time.Sleep(1100*time.Millisecond)
|
||||
time.Sleep(1200*time.Millisecond)
|
||||
gtest.Assert(array.Len(), fixedLength)
|
||||
})
|
||||
}
|
||||
|
||||
@ -10,52 +10,52 @@
|
||||
package gfcache
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/container/gmap"
|
||||
"github.com/gogf/gf/g/container/gtype"
|
||||
"github.com/gogf/gf/g/internal/cmdenv"
|
||||
"github.com/gogf/gf/g/os/gcache"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/gfsnotify"
|
||||
)
|
||||
|
||||
type Cache struct {
|
||||
cap *gtype.Int // 缓存容量(byte),设置为0表示不限制
|
||||
size *gtype.Int // 缓存大小(Byte)
|
||||
cache *gmap.StringInterfaceMap // 缓存对象
|
||||
}
|
||||
|
||||
const (
|
||||
// 默认的缓存容量(10MB)
|
||||
gDEFAULT_CACHE_CAP = 10*1024*1024
|
||||
// 默认的缓存超时时间(60秒)
|
||||
gDEFAULT_CACHE_EXPIRE = 60
|
||||
)
|
||||
|
||||
var (
|
||||
// 默认的缓存容量
|
||||
cacheCap = cmdenv.Get("gf.gfcache.cap", gDEFAULT_CACHE_CAP).Int()
|
||||
// 默认的文件缓存对象
|
||||
cache = New()
|
||||
// 默认的缓存时间(秒)
|
||||
cacheExpire = cmdenv.Get("gf.gfcache.expire", gDEFAULT_CACHE_EXPIRE).Int()*1000
|
||||
)
|
||||
|
||||
func New(cap ... int) *Cache {
|
||||
c := cacheCap
|
||||
if len(cap) > 0 {
|
||||
c = cap[0]
|
||||
// 获得文件内容 string,expire参数为缓存过期时间,单位为秒。
|
||||
func GetContents(path string, expire...int) string {
|
||||
return string(GetBinContents(path, expire...))
|
||||
}
|
||||
|
||||
// 获得文件内容 []byte,expire参数为缓存过期时间,单位为秒。
|
||||
func GetBinContents(path string, expire...int) []byte {
|
||||
k := cacheKey(path)
|
||||
e := cacheExpire
|
||||
if len(expire) > 0 {
|
||||
e = expire[0]
|
||||
}
|
||||
return &Cache {
|
||||
cap : gtype.NewInt(c),
|
||||
size : gtype.NewInt(),
|
||||
cache : gmap.NewStringInterfaceMap(),
|
||||
r := gcache.GetOrSetFuncLock(k, func() interface{} {
|
||||
b := gfile.GetBinContents(path)
|
||||
if b != nil {
|
||||
// 添加文件监控,如果文件有任何变化,立即清空缓存
|
||||
gfsnotify.Add(path, func(event *gfsnotify.Event) {
|
||||
gcache.Remove(k)
|
||||
gfsnotify.Exit()
|
||||
})
|
||||
}
|
||||
return b
|
||||
}, e*1000)
|
||||
if r != nil {
|
||||
return r.([]byte)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获得已缓存的文件大小(byte)
|
||||
func GetSize() int {
|
||||
return cache.GetSize()
|
||||
}
|
||||
|
||||
// 获得文件内容 string
|
||||
func GetContents(path string) string {
|
||||
return cache.GetContents(path)
|
||||
}
|
||||
|
||||
// 获得文件内容 []byte
|
||||
func GetBinContents(path string) []byte {
|
||||
return cache.GetBinContents(path)
|
||||
// 生成缓存键名
|
||||
func cacheKey(path string) string {
|
||||
return "gf.gfcache:" + path
|
||||
}
|
||||
@ -1,62 +0,0 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
// 文件缓存.
|
||||
package gfcache
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/gfsnotify"
|
||||
)
|
||||
|
||||
// 设置容量大小(byte)
|
||||
func (c *Cache) SetCap(cap int) {
|
||||
c.cap.Set(cap)
|
||||
}
|
||||
|
||||
// 获得缓存容量大小(byte)
|
||||
func (c *Cache) GetCap() int {
|
||||
return c.cap.Val()
|
||||
}
|
||||
|
||||
// 获得已缓存的文件大小(byte)
|
||||
func (c *Cache) GetSize() int {
|
||||
return c.size.Val()
|
||||
}
|
||||
|
||||
// 获得文件内容 string
|
||||
func (c *Cache) GetContents(path string) string {
|
||||
return string(c.GetBinContents(path))
|
||||
}
|
||||
|
||||
// 获得文件内容 []byte
|
||||
func (c *Cache) GetBinContents(path string) []byte {
|
||||
if v := c.cache.Get(path); v != nil {
|
||||
return v.([]byte)
|
||||
}
|
||||
b := gfile.GetBinContents(path)
|
||||
// 读取到内容,并且没有超过缓存容量限制时才会执行缓存
|
||||
if len(b) > 0 && (c.cap.Val() == 0 || c.size.Val() < c.cap.Val()) {
|
||||
c.size.Add(len(b))
|
||||
c.addMonitor(path)
|
||||
c.cache.Set(path, b)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// 添加文件监控,一旦文件有变化立即清除缓存,下一次读取的时候再执行缓存。
|
||||
func (c *Cache) addMonitor(path string) {
|
||||
// 防止多goroutine同时调用
|
||||
if c.cache.Contains(path) {
|
||||
return
|
||||
}
|
||||
gfsnotify.Add(path, func(event *gfsnotify.Event) {
|
||||
if r := c.cache.Get(path); r != nil {
|
||||
c.cache.Remove(path)
|
||||
c.size.Add(-len(r.([]byte)))
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -122,11 +122,12 @@ func IsDir(path string) bool {
|
||||
return s.IsDir()
|
||||
}
|
||||
|
||||
// Get current working absolute directory path.
|
||||
// Get current working directory absolute path.
|
||||
//
|
||||
// 获取当前工作目录(SelfDir()方法的别名)
|
||||
// 获取当前工作目录(注意与SelfDir的区别).
|
||||
func Pwd() string {
|
||||
return SelfDir()
|
||||
path, _ := os.Getwd()
|
||||
return path
|
||||
}
|
||||
|
||||
// Check whether given path a file(not a directory).
|
||||
@ -140,11 +141,18 @@ func IsFile(path string) bool {
|
||||
return !s.IsDir()
|
||||
}
|
||||
|
||||
// Info returns a FileInfo describing the named file.
|
||||
// See Stat.
|
||||
//
|
||||
// Stat 方法的别名。
|
||||
func Info(path string) (os.FileInfo, error) {
|
||||
return Stat(path)
|
||||
}
|
||||
|
||||
// Stat returns a FileInfo describing the named file.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
//
|
||||
// 获取文件或目录信息.
|
||||
func Info(path string) (os.FileInfo, error) {
|
||||
func Stat(path string) (os.FileInfo, error) {
|
||||
return os.Stat(path)
|
||||
}
|
||||
|
||||
|
||||
@ -24,7 +24,7 @@ func GetContents(path string) string {
|
||||
return string(GetBinContents(path))
|
||||
}
|
||||
|
||||
// (二进制)读取文件内容
|
||||
// (二进制)读取文件内容,如果文件不存在或者读取失败,返回nil。
|
||||
func GetBinContents(path string) []byte {
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
|
||||
@ -59,7 +59,8 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
REPEAT_EVENT_FILTER_INTERVAL = 1 // (毫秒)重复事件过滤间隔
|
||||
REPEAT_EVENT_FILTER_INTERVAL = 1 // (毫秒)重复事件过滤间隔
|
||||
gFSNOTIFY_EVENT_EXIT = "exit" // 是否退出回调执行
|
||||
)
|
||||
|
||||
var (
|
||||
@ -113,3 +114,8 @@ func RemoveCallback(callbackId int) error {
|
||||
defaultWatcher.RemoveCallback(callbackId)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 在回调方法中调用该方法退出回调注册
|
||||
func Exit() {
|
||||
panic(gFSNOTIFY_EVENT_EXIT)
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@ func (w *Watcher) startWatchLoop() {
|
||||
// 关闭事件
|
||||
case <- w.closeChan: return
|
||||
|
||||
// 监听事件
|
||||
// 监听事件
|
||||
case ev := <- w.watcher.Events:
|
||||
//fmt.Println("ev:", ev.String())
|
||||
w.cache.SetIfNotExist(ev.String(), func() interface{} {
|
||||
@ -74,7 +74,7 @@ func (w *Watcher) getCallbacks(path string) (callbacks []*Callback) {
|
||||
return
|
||||
}
|
||||
|
||||
// 事件循环
|
||||
// 事件循环(核心逻辑)
|
||||
func (w *Watcher) startEventLoop() {
|
||||
go func() {
|
||||
for {
|
||||
@ -126,10 +126,22 @@ func (w *Watcher) startEventLoop() {
|
||||
|
||||
}
|
||||
// 执行回调处理,异步处理
|
||||
for _, callback := range callbacks {
|
||||
go callback.Func(event)
|
||||
for _, v := range callbacks {
|
||||
go func(callback *Callback) {
|
||||
defer func() {
|
||||
// 是否退出监控
|
||||
if err := recover(); err != nil {
|
||||
switch err {
|
||||
case gFSNOTIFY_EVENT_EXIT:
|
||||
w.RemoveCallback(callback.Id)
|
||||
default:
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
callback.Func(event)
|
||||
}(v)
|
||||
}
|
||||
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
155
g/os/gfsnotify/gfsnotify_z_unit_test.go
Normal file
155
g/os/gfsnotify/gfsnotify_z_unit_test.go
Normal file
@ -0,0 +1,155 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
// go test *.go -bench=".*" -benchmem
|
||||
|
||||
package gfsnotify_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/container/gtype"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/gfsnotify"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestWatcher_AddRemove(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
path1 := gconv.String(gtime.Nanosecond())
|
||||
path2 := gconv.String(gtime.Nanosecond()) + "2"
|
||||
gfile.PutContents(path1, "1")
|
||||
defer func() {
|
||||
gfile.Remove(path1)
|
||||
gfile.Remove(path2)
|
||||
}()
|
||||
v := gtype.NewInt(1)
|
||||
callback, err := gfsnotify.Add(path1, func(event *gfsnotify.Event) {
|
||||
if event.IsWrite() {
|
||||
v.Set(2)
|
||||
return
|
||||
}
|
||||
if event.IsRename() {
|
||||
v.Set(3)
|
||||
gfsnotify.Exit()
|
||||
return
|
||||
}
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
gtest.AssertNE(callback, nil)
|
||||
|
||||
gfile.PutContents(path1, "2")
|
||||
time.Sleep(100*time.Millisecond)
|
||||
gtest.Assert(v.Val(), 2)
|
||||
|
||||
gfile.Rename(path1, path2)
|
||||
time.Sleep(100*time.Millisecond)
|
||||
gtest.Assert(v.Val(), 3)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
path1 := gconv.String(gtime.Nanosecond())
|
||||
gfile.PutContents(path1, "1")
|
||||
defer func() {
|
||||
gfile.Remove(path1)
|
||||
}()
|
||||
v := gtype.NewInt(1)
|
||||
callback, err := gfsnotify.Add(path1, func(event *gfsnotify.Event) {
|
||||
if event.IsWrite() {
|
||||
v.Set(2)
|
||||
return
|
||||
}
|
||||
if event.IsRemove() {
|
||||
v.Set(4)
|
||||
return
|
||||
}
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
gtest.AssertNE(callback, nil)
|
||||
|
||||
gfile.PutContents(path1, "2")
|
||||
time.Sleep(100*time.Millisecond)
|
||||
gtest.Assert(v.Val(), 2)
|
||||
|
||||
gfile.Remove(path1)
|
||||
time.Sleep(100*time.Millisecond)
|
||||
gtest.Assert(v.Val(), 4)
|
||||
|
||||
gfile.PutContents(path1, "1")
|
||||
time.Sleep(100*time.Millisecond)
|
||||
gtest.Assert(v.Val(), 4)
|
||||
})
|
||||
}
|
||||
|
||||
func TestWatcher_Callback(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
path1 := gconv.String(gtime.Nanosecond())
|
||||
gfile.PutContents(path1, "1")
|
||||
defer func() {
|
||||
gfile.Remove(path1)
|
||||
}()
|
||||
v := gtype.NewInt(1)
|
||||
callback, err := gfsnotify.Add(path1, func(event *gfsnotify.Event) {
|
||||
if event.IsWrite() {
|
||||
v.Set(2)
|
||||
return
|
||||
}
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
gtest.AssertNE(callback, nil)
|
||||
|
||||
gfile.PutContents(path1, "2")
|
||||
time.Sleep(100*time.Millisecond)
|
||||
gtest.Assert(v.Val(), 2)
|
||||
|
||||
v.Set(3)
|
||||
gfsnotify.RemoveCallback(callback.Id)
|
||||
gfile.PutContents(path1, "3")
|
||||
time.Sleep(100*time.Millisecond)
|
||||
gtest.Assert(v.Val(), 3)
|
||||
})
|
||||
// multiple callbacks
|
||||
gtest.Case(t, func() {
|
||||
path1 := gconv.String(gtime.Nanosecond())
|
||||
gfile.PutContents(path1, "1")
|
||||
defer func() {
|
||||
gfile.Remove(path1)
|
||||
}()
|
||||
v1 := gtype.NewInt(1)
|
||||
v2 := gtype.NewInt(1)
|
||||
callback1, err1 := gfsnotify.Add(path1, func(event *gfsnotify.Event) {
|
||||
if event.IsWrite() {
|
||||
v1.Set(2)
|
||||
return
|
||||
}
|
||||
})
|
||||
callback2, err2 := gfsnotify.Add(path1, func(event *gfsnotify.Event) {
|
||||
if event.IsWrite() {
|
||||
v2.Set(2)
|
||||
return
|
||||
}
|
||||
})
|
||||
gtest.Assert(err1, nil)
|
||||
gtest.Assert(err2, nil)
|
||||
gtest.AssertNE(callback1, nil)
|
||||
gtest.AssertNE(callback2, nil)
|
||||
|
||||
gfile.PutContents(path1, "2")
|
||||
time.Sleep(100*time.Millisecond)
|
||||
gtest.Assert(v1.Val(), 2)
|
||||
gtest.Assert(v2.Val(), 2)
|
||||
|
||||
v1.Set(3)
|
||||
v2.Set(3)
|
||||
gfsnotify.RemoveCallback(callback1.Id)
|
||||
gfile.PutContents(path1, "3")
|
||||
time.Sleep(100*time.Millisecond)
|
||||
gtest.Assert(v1.Val(), 3)
|
||||
gtest.Assert(v2.Val(), 2)
|
||||
})
|
||||
}
|
||||
@ -7,7 +7,8 @@
|
||||
// Package gspath implements file index and search for folders.
|
||||
//
|
||||
// 搜索目录管理,
|
||||
// 可以添加搜索目录,按照添加的优先级进行文件检索,并在内部进行高效缓存处理。
|
||||
// 可以添加搜索目录,按照添加的优先级进行文件检索,并在内部进行高效缓存处理(可选)。
|
||||
// 注意:当开启缓存功能后,在新增/删除文件时,会存在检索延迟。
|
||||
package gspath
|
||||
|
||||
import (
|
||||
@ -16,9 +17,8 @@ import (
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
"github.com/gogf/gf/g/container/gmap"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/gfsnotify"
|
||||
"github.com/gogf/gf/g/text/gstr"
|
||||
"runtime"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
@ -26,7 +26,7 @@ import (
|
||||
// 文件目录搜索管理对象
|
||||
type SPath struct {
|
||||
paths *garray.StringArray // 搜索路径,按照优先级进行排序
|
||||
cache *gmap.StringStringMap // 搜索结果缓存map
|
||||
cache *gmap.StringStringMap // 搜索结果缓存map(如果未nil表示未启用缓存功能)
|
||||
}
|
||||
|
||||
// 文件搜索缓存项
|
||||
@ -37,17 +37,20 @@ type SPathCacheItem struct {
|
||||
|
||||
var (
|
||||
// 单个目录路径对应的SPath对象指针,用于路径检索对象复用
|
||||
pathsMap = gmap.NewStringInterfaceMap()
|
||||
pathsMap = gmap.NewStringInterfaceMap()
|
||||
pathsCacheMap = gmap.NewStringInterfaceMap()
|
||||
)
|
||||
|
||||
// 创建一个搜索对象
|
||||
func New(path...string) *SPath {
|
||||
func New(path string, cache bool) *SPath {
|
||||
sp := &SPath {
|
||||
paths : garray.NewStringArray(),
|
||||
cache : gmap.NewStringStringMap(),
|
||||
}
|
||||
if cache {
|
||||
sp.cache = gmap.NewStringStringMap()
|
||||
}
|
||||
if len(path) > 0 {
|
||||
if _, err := sp.Add(path[0]); err != nil {
|
||||
if _, err := sp.Add(path); err != nil {
|
||||
//fmt.Errorf(err.Error())
|
||||
}
|
||||
}
|
||||
@ -55,17 +58,21 @@ func New(path...string) *SPath {
|
||||
}
|
||||
|
||||
// 创建/获取一个单例的搜索对象, root必须为目录的绝对路径
|
||||
func Get(root string) *SPath {
|
||||
func Get(root string, cache bool) *SPath {
|
||||
return pathsMap.GetOrSetFuncLock(root, func() interface{} {
|
||||
return New(root)
|
||||
return New(root, cache)
|
||||
}).(*SPath)
|
||||
}
|
||||
|
||||
// 检索root目录(必须为绝对路径)下面的name文件的绝对路径,indexFiles用于指定当检索到的结果为目录时,同时检索是否存在这些indexFiles文件
|
||||
func Search(root string, name string, indexFiles...string) (filePath string, isDir bool) {
|
||||
return Get(root).Search(name, indexFiles...)
|
||||
return Get(root, false).Search(name, indexFiles...)
|
||||
}
|
||||
|
||||
// 检索root目录(必须为绝对路径)下面的name文件的绝对路径,indexFiles用于指定当检索到的结果为目录时,同时检索是否存在这些indexFiles文件
|
||||
func SearchWithCache(root string, name string, indexFiles...string) (filePath string, isDir bool) {
|
||||
return Get(root, true).Search(name, indexFiles...)
|
||||
}
|
||||
|
||||
// 设置搜索路径,只保留当前设置项,其他搜索路径被清空
|
||||
func (sp *SPath) Set(path string) (realPath string, err error) {
|
||||
@ -88,8 +95,9 @@ func (sp *SPath) Set(path string) (realPath string, err error) {
|
||||
}
|
||||
}
|
||||
sp.paths.Clear()
|
||||
sp.cache.Clear()
|
||||
|
||||
if sp.cache != nil {
|
||||
sp.cache.Clear()
|
||||
}
|
||||
sp.paths.Append(realPath)
|
||||
sp.updateCacheByPath(realPath)
|
||||
sp.addMonitorByPath(realPath)
|
||||
@ -128,9 +136,39 @@ func (sp *SPath) Add(path string) (realPath string, err error) {
|
||||
}
|
||||
|
||||
// 给定的name只是相对文件路径,找不到该文件时,返回空字符串;
|
||||
// 当给定indexFiles时,如果name时一个目录,那么会进一步检索其下对应的indexFiles文件是否存在,存在则返回indexFile绝对路径;
|
||||
// 当给定indexFiles时,如果name是一个目录,那么会进一步检索其下对应的indexFiles文件是否存在,存在则返回indexFile绝对路径;
|
||||
// 否则返回name目录绝对路径。
|
||||
func (sp *SPath) Search(name string, indexFiles...string) (filePath string, isDir bool) {
|
||||
// 不使用缓存
|
||||
if sp.cache == nil {
|
||||
sp.paths.LockFunc(func(array []string) {
|
||||
path := ""
|
||||
for _, v := range array {
|
||||
path = v + gfile.Separator + name
|
||||
if stat, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
filePath = path
|
||||
isDir = stat.IsDir()
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
if len(indexFiles) > 0 && isDir {
|
||||
if name == "/" {
|
||||
name = ""
|
||||
}
|
||||
path := ""
|
||||
for _, file := range indexFiles {
|
||||
path = filePath + gfile.Separator + file
|
||||
if gfile.Exists(path) {
|
||||
filePath = path
|
||||
isDir = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
// 使用缓存功能
|
||||
name = sp.formatCacheName(name)
|
||||
if v := sp.cache.Get(name); v != "" {
|
||||
filePath, isDir = sp.parseCacheValue(v)
|
||||
@ -151,6 +189,9 @@ func (sp *SPath) Search(name string, indexFiles...string) (filePath string, isDi
|
||||
// 从搜索路径中移除指定的文件,这样该文件无法给搜索。
|
||||
// path可以是绝对路径,也可以相对路径。
|
||||
func (sp *SPath) Remove(path string) {
|
||||
if sp.cache == nil {
|
||||
return
|
||||
}
|
||||
if gfile.Exists(path) {
|
||||
for _, v := range sp.paths.Slice() {
|
||||
name := gstr.Replace(path, v, "")
|
||||
@ -170,6 +211,9 @@ func (sp *SPath) Paths() []string {
|
||||
|
||||
// 返回当前对象缓存的所有路径列表
|
||||
func (sp *SPath) AllPaths() []string {
|
||||
if sp.cache == nil {
|
||||
return nil
|
||||
}
|
||||
paths := sp.cache.Keys()
|
||||
if len(paths) > 0 {
|
||||
sort.Strings(paths)
|
||||
@ -181,82 +225,3 @@ func (sp *SPath) AllPaths() []string {
|
||||
func (sp *SPath) Size() int {
|
||||
return sp.paths.Len()
|
||||
}
|
||||
|
||||
// 递归添加目录下的文件
|
||||
func (sp *SPath) updateCacheByPath(path string) {
|
||||
sp.addToCache(path, path)
|
||||
}
|
||||
|
||||
// 格式化name返回符合规范的缓存名称,分隔符号统一为'/',且前缀必须以'/'开头(类似HTTP URI).
|
||||
func (sp *SPath) formatCacheName(name string) string {
|
||||
if runtime.GOOS != "linux" {
|
||||
name = gstr.Replace(name, "\\", "/")
|
||||
}
|
||||
return "/" + strings.Trim(name, "./")
|
||||
}
|
||||
|
||||
// 根据path计算出对应的缓存name, dirPath为检索根目录路径
|
||||
func (sp *SPath) nameFromPath(filePath, rootPath string) string {
|
||||
name := gstr.Replace(filePath, rootPath, "")
|
||||
name = sp.formatCacheName(name)
|
||||
return name
|
||||
}
|
||||
|
||||
// 按照一定数据结构生成缓存的数据项字符串
|
||||
func (sp *SPath) makeCacheValue(filePath string, isDir bool) string {
|
||||
if isDir {
|
||||
return filePath + "_D_"
|
||||
}
|
||||
return filePath + "_F_"
|
||||
}
|
||||
|
||||
// 按照一定数据结构解析数据项字符串
|
||||
func (sp *SPath) parseCacheValue(value string) (filePath string, isDir bool) {
|
||||
if value[len(value) - 2 : len(value) - 1][0] == 'F' {
|
||||
return value[: len(value) - 3], false
|
||||
}
|
||||
return value[: len(value) - 3], true
|
||||
}
|
||||
|
||||
// 添加path到缓存中(递归)
|
||||
func (sp *SPath) addToCache(filePath, rootPath string) {
|
||||
// 首先添加自身
|
||||
idDir := gfile.IsDir(filePath)
|
||||
sp.cache.SetIfNotExist(sp.nameFromPath(filePath, rootPath), sp.makeCacheValue(filePath, idDir))
|
||||
// 如果添加的是目录,那么需要递归添加
|
||||
if idDir {
|
||||
if files, err := gfile.ScanDir(filePath, "*", true); err == nil {
|
||||
//fmt.Println("gspath add to cache:", filePath, files)
|
||||
for _, path := range files {
|
||||
sp.cache.SetIfNotExist(sp.nameFromPath(path, rootPath), sp.makeCacheValue(path, gfile.IsDir(path)))
|
||||
}
|
||||
} else {
|
||||
//fmt.Errorf(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 添加文件目录监控(递归),当目录下的文件有更新时,会同时更新缓存。
|
||||
// 这里需要注意的点是,由于添加监听是递归添加的,那么假如删除一个目录,那么该目录下的文件(包括目录)也会产生一条删除事件,总共会产生N条事件。
|
||||
func (sp *SPath) addMonitorByPath(path string) {
|
||||
gfsnotify.Add(path, func(event *gfsnotify.Event) {
|
||||
//glog.Debug(event.String())
|
||||
switch {
|
||||
case event.IsRemove():
|
||||
sp.cache.Remove(sp.nameFromPath(event.Path, path))
|
||||
|
||||
case event.IsRename():
|
||||
if !gfile.Exists(event.Path) {
|
||||
sp.cache.Remove(sp.nameFromPath(event.Path, path))
|
||||
}
|
||||
|
||||
case event.IsCreate():
|
||||
sp.addToCache(event.Path, path)
|
||||
}
|
||||
}, true)
|
||||
}
|
||||
|
||||
// 删除监听(递归)
|
||||
func (sp *SPath) removeMonitorByPath(path string) {
|
||||
gfsnotify.Remove(path)
|
||||
}
|
||||
109
g/os/gspath/gspath_cache.go
Normal file
109
g/os/gspath/gspath_cache.go
Normal file
@ -0,0 +1,109 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
// Package gspath implements file index and search for folders.
|
||||
//
|
||||
|
||||
package gspath
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/gfsnotify"
|
||||
"github.com/gogf/gf/g/text/gstr"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 递归添加目录下的文件
|
||||
func (sp *SPath) updateCacheByPath(path string) {
|
||||
if sp.cache == nil {
|
||||
return
|
||||
}
|
||||
sp.addToCache(path, path)
|
||||
}
|
||||
|
||||
// 格式化name返回符合规范的缓存名称,分隔符号统一为'/',且前缀必须以'/'开头(类似HTTP URI).
|
||||
func (sp *SPath) formatCacheName(name string) string {
|
||||
if runtime.GOOS != "linux" {
|
||||
name = gstr.Replace(name, "\\", "/")
|
||||
}
|
||||
return "/" + strings.Trim(name, "./")
|
||||
}
|
||||
|
||||
// 根据path计算出对应的缓存name, dirPath为检索根目录路径
|
||||
func (sp *SPath) nameFromPath(filePath, rootPath string) string {
|
||||
name := gstr.Replace(filePath, rootPath, "")
|
||||
name = sp.formatCacheName(name)
|
||||
return name
|
||||
}
|
||||
|
||||
// 按照一定数据结构生成缓存的数据项字符串
|
||||
func (sp *SPath) makeCacheValue(filePath string, isDir bool) string {
|
||||
if isDir {
|
||||
return filePath + "_D_"
|
||||
}
|
||||
return filePath + "_F_"
|
||||
}
|
||||
|
||||
// 按照一定数据结构解析数据项字符串
|
||||
func (sp *SPath) parseCacheValue(value string) (filePath string, isDir bool) {
|
||||
if value[len(value) - 2 : len(value) - 1][0] == 'F' {
|
||||
return value[: len(value) - 3], false
|
||||
}
|
||||
return value[: len(value) - 3], true
|
||||
}
|
||||
|
||||
// 添加path到缓存中(递归)
|
||||
func (sp *SPath) addToCache(filePath, rootPath string) {
|
||||
if sp.cache == nil {
|
||||
return
|
||||
}
|
||||
// 首先添加自身
|
||||
idDir := gfile.IsDir(filePath)
|
||||
sp.cache.SetIfNotExist(sp.nameFromPath(filePath, rootPath), sp.makeCacheValue(filePath, idDir))
|
||||
// 如果添加的是目录,那么需要递归添加
|
||||
if idDir {
|
||||
if files, err := gfile.ScanDir(filePath, "*", true); err == nil {
|
||||
//fmt.Println("gspath add to cache:", filePath, files)
|
||||
for _, path := range files {
|
||||
sp.cache.SetIfNotExist(sp.nameFromPath(path, rootPath), sp.makeCacheValue(path, gfile.IsDir(path)))
|
||||
}
|
||||
} else {
|
||||
//fmt.Errorf(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 添加文件目录监控(递归),当目录下的文件有更新时,会同时更新缓存。
|
||||
// 这里需要注意的点是,由于添加监听是递归添加的,那么假如删除一个目录,那么该目录下的文件(包括目录)也会产生一条删除事件,总共会产生N条事件。
|
||||
func (sp *SPath) addMonitorByPath(path string) {
|
||||
if sp.cache == nil {
|
||||
return
|
||||
}
|
||||
gfsnotify.Add(path, func(event *gfsnotify.Event) {
|
||||
//glog.Debug(event.String())
|
||||
switch {
|
||||
case event.IsRemove():
|
||||
sp.cache.Remove(sp.nameFromPath(event.Path, path))
|
||||
|
||||
case event.IsRename():
|
||||
if !gfile.Exists(event.Path) {
|
||||
sp.cache.Remove(sp.nameFromPath(event.Path, path))
|
||||
}
|
||||
|
||||
case event.IsCreate():
|
||||
sp.addToCache(event.Path, path)
|
||||
}
|
||||
}, true)
|
||||
}
|
||||
|
||||
// 删除监听(递归)
|
||||
func (sp *SPath) removeMonitorByPath(path string) {
|
||||
if sp.cache == nil {
|
||||
return
|
||||
}
|
||||
gfsnotify.Remove(path)
|
||||
}
|
||||
@ -1,37 +0,0 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gspath
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
sp = New()
|
||||
)
|
||||
|
||||
func init() {
|
||||
sp.Add("/Users/john/Temp")
|
||||
}
|
||||
|
||||
func Benchmark_Search(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
sp.Search("1")
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_Search_None(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
sp.Search("1000")
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_Search_IndexFiles(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
sp.Search("1", "index.html")
|
||||
}
|
||||
}
|
||||
@ -54,7 +54,7 @@ func checkAndInitDefaultView() {
|
||||
// gfile.MainPkgPath() 用以判断是否开发环境
|
||||
mainPkgPath := gfile.MainPkgPath()
|
||||
if gfile.MainPkgPath() == "" {
|
||||
viewObj = New(gfile.SelfDir())
|
||||
viewObj = New(gfile.Pwd())
|
||||
} else {
|
||||
viewObj = New(mainPkgPath)
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
// from golang-1.11.2
|
||||
// from golang-1.11.2 text/template
|
||||
// 1. remove "<no value>" when template variable does not exist;
|
||||
package text
|
||||
|
||||
@ -232,7 +232,7 @@ func AssertNI(value, expect interface{}) {
|
||||
|
||||
// 提示错误不退出进程执行
|
||||
func Error(message...interface{}) {
|
||||
fmt.Fprintf(os.Stderr, "[ERROR] %s\n%s", fmt.Sprint(message...), getBacktrace())
|
||||
panic(fmt.Sprintf("[ERROR] %s", fmt.Sprint(message...)))
|
||||
}
|
||||
|
||||
// 提示错误并退出进程执行
|
||||
|
||||
@ -59,6 +59,20 @@ func ReplaceI(origin, search, replace string, count...int) string {
|
||||
return origin
|
||||
}
|
||||
|
||||
// Count counts the number of <substr> appears in <s>. It returns 0 if no <substr> found in <s>.
|
||||
//
|
||||
// 计算字符串substr在字符串s中出现的次数,如果没有在s中找到substr,那么返回0。
|
||||
func Count(s, substr string) int {
|
||||
return strings.Count(s, substr)
|
||||
}
|
||||
|
||||
// Count counts the number of <substr> appears in <s>, case-insensitive. It returns 0 if no <substr> found in <s>.
|
||||
//
|
||||
// (非大小写敏感)计算字符串substr在字符串s中出现的次数,如果没有在s中找到substr,那么返回0。
|
||||
func CountI(s, substr string) int {
|
||||
return strings.Count(ToLower(s), ToLower(substr))
|
||||
}
|
||||
|
||||
// Replace string by array/slice.
|
||||
//
|
||||
// 使用map进行字符串替换(大小写敏感)
|
||||
|
||||
@ -235,39 +235,6 @@ func Test_Shuffle(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Trim(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gstr.Trim(" 123456\n "), "123456")
|
||||
gtest.Assert(gstr.Trim("#123456#;", "#;"), "123456")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_TrimRight(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gstr.TrimRight(" 123456\n "), " 123456")
|
||||
gtest.Assert(gstr.TrimRight("#123456#;", "#;"), "#123456")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_TrimRightStr(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gstr.TrimRightStr("gogo我爱gogo", "go"), "gogo我爱")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_TrimLeft(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gstr.TrimLeft(" \r123456\n "), "123456\n ")
|
||||
gtest.Assert(gstr.TrimLeft("#;123456#;", "#;"), "123456#;")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_TrimLeftStr(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gstr.TrimLeftStr("gogo我爱gogo", "go"), "我爱gogo")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Split(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gstr.Split("1.2", "."), []string{"1", "2"})
|
||||
@ -335,4 +302,24 @@ func Test_QuoteMeta(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gstr.QuoteMeta(`.\+*?[^]($)`), `\.\\\+\*\?\[\^\]\(\$\)`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Count(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
s := "abcdaAD"
|
||||
gtest.Assert(gstr.Count(s, "0"), 0)
|
||||
gtest.Assert(gstr.Count(s, "a"), 2)
|
||||
gtest.Assert(gstr.Count(s, "b"), 1)
|
||||
gtest.Assert(gstr.Count(s, "d"), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_CountI(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
s := "abcdaAD"
|
||||
gtest.Assert(gstr.CountI(s, "0"), 0)
|
||||
gtest.Assert(gstr.CountI(s, "a"), 3)
|
||||
gtest.Assert(gstr.CountI(s, "b"), 1)
|
||||
gtest.Assert(gstr.CountI(s, "d"), 2)
|
||||
})
|
||||
}
|
||||
48
g/text/gstr/gstr_z_unit_trim_test.go
Normal file
48
g/text/gstr/gstr_z_unit_trim_test.go
Normal file
@ -0,0 +1,48 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
// go test *.go -bench=".*"
|
||||
|
||||
package gstr_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"github.com/gogf/gf/g/text/gstr"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Trim(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gstr.Trim(" 123456\n "), "123456")
|
||||
gtest.Assert(gstr.Trim("#123456#;", "#;"), "123456")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_TrimRight(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gstr.TrimRight(" 123456\n "), " 123456")
|
||||
gtest.Assert(gstr.TrimRight("#123456#;", "#;"), "#123456")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_TrimRightStr(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gstr.TrimRightStr("gogo我爱gogo", "go"), "gogo我爱")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_TrimLeft(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gstr.TrimLeft(" \r123456\n "), "123456\n ")
|
||||
gtest.Assert(gstr.TrimLeft("#;123456#;", "#;"), "123456#;")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_TrimLeftStr(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gstr.TrimLeftStr("gogo我爱gogo", "go"), "我爱gogo")
|
||||
})
|
||||
}
|
||||
@ -13,6 +13,7 @@ package gconv
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/g/encoding/gbinary"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@ -22,6 +23,16 @@ type apiString interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
var (
|
||||
// 为空的字符串
|
||||
emptyStringMap = map[string]struct{}{
|
||||
"" : struct {}{},
|
||||
"0" : struct {}{},
|
||||
"off" : struct {}{},
|
||||
"false" : struct {}{},
|
||||
}
|
||||
)
|
||||
|
||||
// 将变量i转换为字符串指定的类型t,非必须参数extraParams用以额外的参数传递
|
||||
func Convert(i interface{}, t string, extraParams...interface{}) interface{} {
|
||||
switch t {
|
||||
@ -63,6 +74,7 @@ func Convert(i interface{}, t string, extraParams...interface{}) interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
// 转换为二进制[]byte
|
||||
func Bytes(i interface{}) []byte {
|
||||
if i == nil {
|
||||
return nil
|
||||
@ -80,11 +92,11 @@ func String(i interface{}) string {
|
||||
return ""
|
||||
}
|
||||
switch value := i.(type) {
|
||||
case int: return strconv.Itoa(value)
|
||||
case int: return strconv.FormatInt(int64(value), 10)
|
||||
case int8: return strconv.Itoa(int(value))
|
||||
case int16: return strconv.Itoa(int(value))
|
||||
case int32: return strconv.Itoa(int(value))
|
||||
case int64: return strconv.Itoa(int(value))
|
||||
case int64: return strconv.FormatInt(int64(value), 10)
|
||||
case uint: return strconv.FormatUint(uint64(value), 10)
|
||||
case uint8: return strconv.FormatUint(uint64(value), 10)
|
||||
case uint16: return strconv.FormatUint(uint64(value), 10)
|
||||
@ -107,7 +119,7 @@ func String(i interface{}) string {
|
||||
}
|
||||
}
|
||||
|
||||
//false: "", 0, false, off
|
||||
//false: false, "", 0, "false", "off", empty slice/map
|
||||
func Bool(i interface{}) bool {
|
||||
if i == nil {
|
||||
return false
|
||||
@ -115,10 +127,27 @@ func Bool(i interface{}) bool {
|
||||
if v, ok := i.(bool); ok {
|
||||
return v
|
||||
}
|
||||
if s := String(i); s != "" && s != "0" && s != "false" && s != "off" {
|
||||
if s, ok := i.(string); ok {
|
||||
if _, ok := emptyStringMap[s]; ok {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
rv := reflect.ValueOf(i)
|
||||
switch rv.Kind() {
|
||||
case reflect.Ptr: return !rv.IsNil()
|
||||
case reflect.Map: fallthrough
|
||||
case reflect.Array: fallthrough
|
||||
case reflect.Slice: return rv.Len() != 0
|
||||
case reflect.Struct: return true
|
||||
default:
|
||||
s := String(i)
|
||||
if _, ok := emptyStringMap[s]; ok {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func Int(i interface{}) int {
|
||||
|
||||
@ -320,4 +320,24 @@ func Interfaces(i interface{}) []interface{} {
|
||||
}
|
||||
return array
|
||||
}
|
||||
}
|
||||
|
||||
// 将类型转换为[]map[string]interface{}类型.
|
||||
func Maps(i interface{}) []map[string]interface{} {
|
||||
if i == nil {
|
||||
return nil
|
||||
}
|
||||
if r, ok := i.([]map[string]interface{}); ok {
|
||||
return r
|
||||
} else {
|
||||
array := Interfaces(i)
|
||||
if len(array) == 0 {
|
||||
return nil
|
||||
}
|
||||
list := make([]map[string]interface{}, len(array))
|
||||
for k, v := range array {
|
||||
list[k] = Map(v)
|
||||
}
|
||||
return list
|
||||
}
|
||||
}
|
||||
@ -15,20 +15,23 @@ import (
|
||||
|
||||
func Test_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
value := 123.456
|
||||
gtest.AssertEQ(gconv.Int(value), int(123))
|
||||
gtest.AssertEQ(gconv.Int8(value), int8(123))
|
||||
gtest.AssertEQ(gconv.Int16(value), int16(123))
|
||||
gtest.AssertEQ(gconv.Int32(value), int32(123))
|
||||
gtest.AssertEQ(gconv.Int64(value), int64(123))
|
||||
gtest.AssertEQ(gconv.Uint(value), uint(123))
|
||||
gtest.AssertEQ(gconv.Uint8(value), uint8(123))
|
||||
gtest.AssertEQ(gconv.Uint16(value), uint16(123))
|
||||
gtest.AssertEQ(gconv.Uint32(value), uint32(123))
|
||||
gtest.AssertEQ(gconv.Uint64(value), uint64(123))
|
||||
gtest.AssertEQ(gconv.Float32(value), float32(123.456))
|
||||
gtest.AssertEQ(gconv.Float64(value), float64(123.456))
|
||||
gtest.AssertEQ(gconv.Bool(value), true)
|
||||
gtest.AssertEQ(gconv.String(value), "123.456")
|
||||
vint := float32(123.456)
|
||||
vint64 := int64(1552578474888)
|
||||
gtest.AssertEQ(gconv.Int(vint), int(123))
|
||||
gtest.AssertEQ(gconv.Int8(vint), int8(123))
|
||||
gtest.AssertEQ(gconv.Int16(vint), int16(123))
|
||||
gtest.AssertEQ(gconv.Int32(vint), int32(123))
|
||||
gtest.AssertEQ(gconv.Int64(vint), int64(123))
|
||||
gtest.AssertEQ(gconv.Int64(vint), int64(123))
|
||||
gtest.AssertEQ(gconv.Uint(vint), uint(123))
|
||||
gtest.AssertEQ(gconv.Uint8(vint), uint8(123))
|
||||
gtest.AssertEQ(gconv.Uint16(vint), uint16(123))
|
||||
gtest.AssertEQ(gconv.Uint32(vint), uint32(123))
|
||||
gtest.AssertEQ(gconv.Uint64(vint), uint64(123))
|
||||
gtest.AssertEQ(gconv.Float32(vint), float32(123.456))
|
||||
gtest.AssertEQ(gconv.Float64(vint), float64(123.456))
|
||||
gtest.AssertEQ(gconv.Bool(vint), true)
|
||||
gtest.AssertEQ(gconv.String(vint), "123.456")
|
||||
gtest.AssertEQ(gconv.String(vint64), "1552578474888")
|
||||
})
|
||||
}
|
||||
|
||||
42
g/util/gconv/gconv_z_unit_bool_test.go
Normal file
42
g/util/gconv/gconv_z_unit_bool_test.go
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gconv_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type boolStruct struct {
|
||||
|
||||
}
|
||||
|
||||
func Test_Bool(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
var i interface{} = nil
|
||||
gtest.AssertEQ(gconv.Bool(i), false)
|
||||
gtest.AssertEQ(gconv.Bool(false), false)
|
||||
gtest.AssertEQ(gconv.Bool(nil), false)
|
||||
gtest.AssertEQ(gconv.Bool(0), false)
|
||||
gtest.AssertEQ(gconv.Bool("0"), false)
|
||||
gtest.AssertEQ(gconv.Bool(""), false)
|
||||
gtest.AssertEQ(gconv.Bool("false"), false)
|
||||
gtest.AssertEQ(gconv.Bool("off"), false)
|
||||
gtest.AssertEQ(gconv.Bool([]byte{}), false)
|
||||
gtest.AssertEQ(gconv.Bool([]string{}), false)
|
||||
gtest.AssertEQ(gconv.Bool([]interface{}{}), false)
|
||||
gtest.AssertEQ(gconv.Bool([]map[int]int{}), false)
|
||||
|
||||
gtest.AssertEQ(gconv.Bool("1"), true)
|
||||
gtest.AssertEQ(gconv.Bool("on"), true)
|
||||
gtest.AssertEQ(gconv.Bool(1), true)
|
||||
gtest.AssertEQ(gconv.Bool(123.456), true)
|
||||
gtest.AssertEQ(gconv.Bool(boolStruct{}), true)
|
||||
gtest.AssertEQ(gconv.Bool(&boolStruct{}), true)
|
||||
})
|
||||
}
|
||||
65
g/util/gconv/gconv_z_unit_string_test.go
Normal file
65
g/util/gconv/gconv_z_unit_string_test.go
Normal file
@ -0,0 +1,65 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gconv_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
||||
type stringStruct1 struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type stringStruct2 struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (s *stringStruct1) String() string {
|
||||
return s.Name
|
||||
}
|
||||
|
||||
func Test_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.AssertEQ(gconv.String(int(123)), "123")
|
||||
gtest.AssertEQ(gconv.String(int(-123)), "-123")
|
||||
gtest.AssertEQ(gconv.String(int8(123)), "123")
|
||||
gtest.AssertEQ(gconv.String(int8(-123)), "-123")
|
||||
gtest.AssertEQ(gconv.String(int16(123)), "123")
|
||||
gtest.AssertEQ(gconv.String(int16(-123)), "-123")
|
||||
gtest.AssertEQ(gconv.String(int32(123)), "123")
|
||||
gtest.AssertEQ(gconv.String(int32(-123)), "-123")
|
||||
gtest.AssertEQ(gconv.String(int64(123)), "123")
|
||||
gtest.AssertEQ(gconv.String(int64(-123)), "-123")
|
||||
gtest.AssertEQ(gconv.String(int64(1552578474888)), "1552578474888")
|
||||
gtest.AssertEQ(gconv.String(int64(-1552578474888)), "-1552578474888")
|
||||
|
||||
gtest.AssertEQ(gconv.String(uint(123)), "123")
|
||||
gtest.AssertEQ(gconv.String(uint8(123)), "123")
|
||||
gtest.AssertEQ(gconv.String(uint16(123)), "123")
|
||||
gtest.AssertEQ(gconv.String(uint32(123)), "123")
|
||||
gtest.AssertEQ(gconv.String(uint64(155257847488898765)), "155257847488898765")
|
||||
|
||||
gtest.AssertEQ(gconv.String(float32(123.456)), "123.456")
|
||||
gtest.AssertEQ(gconv.String(float32(-123.456)), "-123.456")
|
||||
gtest.AssertEQ(gconv.String(float64(1552578474888.456)), "1552578474888.456")
|
||||
gtest.AssertEQ(gconv.String(float64(-1552578474888.456)), "-1552578474888.456")
|
||||
|
||||
gtest.AssertEQ(gconv.String(true), "true")
|
||||
gtest.AssertEQ(gconv.String(false), "false")
|
||||
|
||||
gtest.AssertEQ(gconv.String([]byte("bytes")), "bytes")
|
||||
|
||||
gtest.AssertEQ(gconv.String(stringStruct1{"john"}), `{"Name":"john"}`)
|
||||
gtest.AssertEQ(gconv.String(&stringStruct1{"john"}), "john")
|
||||
|
||||
gtest.AssertEQ(gconv.String(stringStruct2{"john"}), `{"Name":"john"}`)
|
||||
gtest.AssertEQ(gconv.String(&stringStruct2{"john"}), `{"Name":"john"}`)
|
||||
})
|
||||
}
|
||||
@ -109,11 +109,8 @@ func Test_Struct_Attr_Slice(t *testing.T) {
|
||||
type User struct {
|
||||
Scores []int
|
||||
}
|
||||
|
||||
user := new(User)
|
||||
scores := []interface{}{99, 100, 60, 140}
|
||||
|
||||
// 通过map映射转换
|
||||
user := new(User)
|
||||
if err := gconv.Struct(g.Map{"Scores" : scores}, user); err != nil {
|
||||
gtest.Error(err)
|
||||
} else {
|
||||
@ -121,15 +118,6 @@ func Test_Struct_Attr_Slice(t *testing.T) {
|
||||
Scores : []int{99, 100, 60, 140},
|
||||
})
|
||||
}
|
||||
|
||||
// 通过变量映射转换,直接slice赋值
|
||||
if err := gconv.Struct(scores, user); err != nil {
|
||||
gtest.Error(err)
|
||||
} else {
|
||||
gtest.Assert(user, &User{
|
||||
Scores : []int{99, 100, 60, 140},
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -67,35 +67,4 @@ type CustomMsg = map[string]interface{}
|
||||
func parseSequenceTag(tag string) (name, rule, msg string) {
|
||||
match, _ := gregex.MatchString(`\s*((\w+)\s*@){0,1}\s*([^#]+)\s*(#\s*(.*)){0,1}\s*`, tag)
|
||||
return strings.TrimSpace(match[2]), strings.TrimSpace(match[3]), strings.TrimSpace(match[5])
|
||||
}
|
||||
|
||||
// 解析sequence tag为标准校验规则及自定义错误
|
||||
func parseSequenceTags(tags []string) (rules map[string]string, msgs map[string]interface{}) {
|
||||
rules = make(map[string]string)
|
||||
msgs = make(map[string]interface{})
|
||||
for _, tag := range tags {
|
||||
name, rule, msg := parseSequenceTag(tag)
|
||||
// 校验规则
|
||||
if len(name) == 0 {
|
||||
continue
|
||||
}
|
||||
rules[name] = rule
|
||||
// 错误提示
|
||||
if len(msg) > 0 {
|
||||
ruleArray := strings.Split(rule, "|")
|
||||
msgArray := strings.Split(msg, "|")
|
||||
for k, v := range ruleArray {
|
||||
if len(msgArray[k]) == 0 {
|
||||
continue
|
||||
}
|
||||
// 关联校验会有":"符号
|
||||
array := strings.Split(v, ":")
|
||||
if _, ok := msgs[name]; !ok {
|
||||
msgs[name] = make(map[string]string)
|
||||
}
|
||||
msgs[name].(map[string]string)[strings.TrimSpace(array[0])] = strings.TrimSpace(msgArray[k])
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -130,7 +130,7 @@ func Check(value interface{}, rules string, msgs interface{}, params...interface
|
||||
msgArray = strings.Split(v, "|")
|
||||
default:
|
||||
for k, v := range gconv.Map(msgs) {
|
||||
data[k] = gconv.String(v)
|
||||
customMsgMap[k] = gconv.String(v)
|
||||
}
|
||||
}
|
||||
ruleItems := strings.Split(strings.TrimSpace(rules), "|")
|
||||
|
||||
@ -400,6 +400,9 @@ func Test_QQ(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_Ip(t *testing.T) {
|
||||
if m := gvalid.Check("10.0.0.1", "ip", nil); m != nil {
|
||||
t.Error(m)
|
||||
}
|
||||
if m := gvalid.Check("10.0.0.1", "ipv4", nil); m != nil {
|
||||
t.Error(m)
|
||||
}
|
||||
@ -409,12 +412,21 @@ func Test_Ip(t *testing.T) {
|
||||
if m := gvalid.Check("1920.0.0.0", "ipv4", nil); m == nil {
|
||||
t.Error("ipv4校验失败")
|
||||
}
|
||||
if m := gvalid.Check("1920.0.0.0", "ip", nil); m == nil {
|
||||
t.Error("ipv4校验失败")
|
||||
}
|
||||
if m := gvalid.Check("fe80::5484:7aff:fefe:9799", "ipv6", nil); m != nil {
|
||||
t.Error(m)
|
||||
}
|
||||
if m := gvalid.Check("fe80::5484:7aff:fefe:9799123", "ipv6", nil); m == nil {
|
||||
t.Error(m)
|
||||
}
|
||||
if m := gvalid.Check("fe80::5484:7aff:fefe:9799", "ip", nil); m != nil {
|
||||
t.Error(m)
|
||||
}
|
||||
if m := gvalid.Check("fe80::5484:7aff:fefe:9799123", "ip", nil); m == nil {
|
||||
t.Error(m)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_IPv4(t *testing.T) {
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
package gvalid_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"github.com/gogf/gf/g/util/gvalid"
|
||||
"testing"
|
||||
)
|
||||
@ -65,3 +66,35 @@ func Test_CheckMapWithNilAndNotRequiredField(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Sequence(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
params := map[string]interface{} {
|
||||
"passport" : "",
|
||||
"password" : "123456",
|
||||
"password2" : "1234567",
|
||||
}
|
||||
rules := []string {
|
||||
"passport@required|length:6,16#账号不能为空|账号长度应当在:min到:max之间",
|
||||
"password@required|length:6,16|same:password2#密码不能为空|密码长度应当在:min到:max之间|两次密码输入不相等",
|
||||
"password2@required|length:6,16#",
|
||||
}
|
||||
err := gvalid.CheckMap(params, rules)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.Assert(len(err.Map()), 2)
|
||||
gtest.Assert(err.Map()["required"], "账号不能为空")
|
||||
gtest.Assert(err.Map()["length"], "账号长度应当在6到16之间")
|
||||
gtest.Assert(len(err.Maps()), 2)
|
||||
|
||||
gtest.Assert(err.String(), "账号不能为空; 账号长度应当在6到16之间; 两次密码输入不相等")
|
||||
gtest.Assert(err.Strings(), []string{"账号不能为空", "账号长度应当在6到16之间", "两次密码输入不相等"})
|
||||
|
||||
k, m := err.FirstItem()
|
||||
gtest.Assert(k, "passport")
|
||||
gtest.Assert(m, err.Map())
|
||||
|
||||
r, s := err.FirstRule()
|
||||
gtest.Assert(r, "required")
|
||||
gtest.Assert(s, "账号不能为空")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -7,28 +7,66 @@
|
||||
package gvalid_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"github.com/gogf/gf/g/util/gvalid"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_CheckStruct(t *testing.T) {
|
||||
type Object struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
rules := map[string]string {
|
||||
"Name" : "required|length:6,16",
|
||||
"Age" : "between:18,30",
|
||||
}
|
||||
msgs := map[string]interface{} {
|
||||
"Name" : map[string]string {
|
||||
"required" : "名称不能为空",
|
||||
"length" : "名称长度为:min到:max个字符",
|
||||
},
|
||||
"Age" : "年龄为18到30周岁",
|
||||
}
|
||||
obj := &Object{"john", 16}
|
||||
if m := gvalid.CheckStruct(obj, rules, msgs); m == nil {
|
||||
t.Error("CheckObject校验失败")
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
type Object struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
rules := map[string]string {
|
||||
"Name" : "required|length:6,16",
|
||||
"Age" : "between:18,30",
|
||||
}
|
||||
msgs := map[string]interface{} {
|
||||
"Name" : map[string]string {
|
||||
"required" : "名称不能为空",
|
||||
"length" : "名称长度为:min到:max个字符",
|
||||
},
|
||||
"Age" : "年龄为18到30周岁",
|
||||
}
|
||||
obj := &Object{"john", 16}
|
||||
err := gvalid.CheckStruct(obj, rules, msgs)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.Assert(len(err.Maps()), 2)
|
||||
gtest.Assert(err.Maps()["Name"]["required"], "")
|
||||
gtest.Assert(err.Maps()["Name"]["length"], "名称长度为6到16个字符")
|
||||
gtest.Assert(err.Maps()["Age"]["between"], "年龄为18到30周岁")
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
type LoginRequest struct {
|
||||
Username string `json:"username" gvalid:"username@required#用户名不能为空"`
|
||||
Password string `json:"password" gvalid:"password@required#登录密码不能为空"`
|
||||
}
|
||||
var login LoginRequest
|
||||
err := gvalid.CheckStruct(login, nil)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.Assert(len(err.Maps()), 2)
|
||||
gtest.Assert(err.Maps()["username"]["required"], "用户名不能为空")
|
||||
gtest.Assert(err.Maps()["password"]["required"], "登录密码不能为空")
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
type User struct {
|
||||
Id int `gvalid:"uid@required|min:10#|ID不能为空"`
|
||||
Age int `gvalid:"age@required#年龄不能为空"`
|
||||
Username string `json:"username" gvalid:"username@required#用户名不能为空"`
|
||||
Password string `json:"password" gvalid:"password@required#登录密码不能为空"`
|
||||
}
|
||||
user := &User{
|
||||
Id : 1,
|
||||
Username : "john",
|
||||
Password : "123456",
|
||||
}
|
||||
err := gvalid.CheckStruct(user, nil)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.Assert(len(err.Maps()), 1)
|
||||
gtest.Assert(err.Maps()["uid"]["min"], "ID不能为空")
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
@ -2,10 +2,11 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
t := gconv.GTime("2010-10-10 00:00:01")
|
||||
fmt.Println(t.String())
|
||||
//t := gconv.GTime("2010-10-10 00:00:01")
|
||||
fmt.Println(gconv.String(gtime.Millisecond()))
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
package gf
|
||||
|
||||
const VERSION = "v1.5.12"
|
||||
const VERSION = "v1.5.16"
|
||||
const AUTHORS = "john<john@goframe.org>"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user