mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 11:18:50 +08:00
replace char <xxx> to ;add GetWithEnv/GetWithCmd fuctions for package gcfg
This commit is contained in:
parent
6bde7c61b4
commit
fa5499373a
@ -50,7 +50,7 @@ func NewFrom(array []interface{}, safe ...bool) *List {
|
||||
}
|
||||
}
|
||||
|
||||
// PushFront inserts a new element <e> with value <v> at the front of list <l> and returns `e`.
|
||||
// PushFront inserts a new element `e` with value `v` at the front of list `l` and returns `e`.
|
||||
func (l *List) PushFront(v interface{}) (e *Element) {
|
||||
l.mu.Lock()
|
||||
if l.list == nil {
|
||||
@ -61,7 +61,7 @@ func (l *List) PushFront(v interface{}) (e *Element) {
|
||||
return
|
||||
}
|
||||
|
||||
// PushBack inserts a new element <e> with value <v> at the back of list <l> and returns `e`.
|
||||
// PushBack inserts a new element `e` with value `v` at the back of list `l` and returns `e`.
|
||||
func (l *List) PushBack(v interface{}) (e *Element) {
|
||||
l.mu.Lock()
|
||||
if l.list == nil {
|
||||
@ -72,7 +72,7 @@ func (l *List) PushBack(v interface{}) (e *Element) {
|
||||
return
|
||||
}
|
||||
|
||||
// PushFronts inserts multiple new elements with values <values> at the front of list `l`.
|
||||
// PushFronts inserts multiple new elements with values `values` at the front of list `l`.
|
||||
func (l *List) PushFronts(values []interface{}) {
|
||||
l.mu.Lock()
|
||||
if l.list == nil {
|
||||
@ -84,7 +84,7 @@ func (l *List) PushFronts(values []interface{}) {
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
// PushBacks inserts multiple new elements with values <values> at the back of list `l`.
|
||||
// PushBacks inserts multiple new elements with values `values` at the back of list `l`.
|
||||
func (l *List) PushBacks(values []interface{}) {
|
||||
l.mu.Lock()
|
||||
if l.list == nil {
|
||||
@ -124,7 +124,7 @@ func (l *List) PopFront() (value interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// PopBacks removes <max> elements from back of `l`
|
||||
// PopBacks removes `max` elements from back of `l`
|
||||
// and returns values of the removed elements as slice.
|
||||
func (l *List) PopBacks(max int) (values []interface{}) {
|
||||
l.mu.Lock()
|
||||
@ -146,7 +146,7 @@ func (l *List) PopBacks(max int) (values []interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// PopFronts removes <max> elements from front of `l`
|
||||
// PopFronts removes `max` elements from front of `l`
|
||||
// and returns values of the removed elements as slice.
|
||||
func (l *List) PopFronts(max int) (values []interface{}) {
|
||||
l.mu.Lock()
|
||||
@ -279,8 +279,8 @@ func (l *List) Size() int {
|
||||
return l.Len()
|
||||
}
|
||||
|
||||
// MoveBefore moves element <e> to its new position before `p`.
|
||||
// If <e> or <p> is not an element of <l>, or <e> == `p`, the list is not modified.
|
||||
// MoveBefore moves element `e` to its new position before `p`.
|
||||
// If `e` or `p` is not an element of `l`, or `e` == `p`, the list is not modified.
|
||||
// The element and `p` must not be nil.
|
||||
func (l *List) MoveBefore(e, p *Element) {
|
||||
l.mu.Lock()
|
||||
@ -291,8 +291,8 @@ func (l *List) MoveBefore(e, p *Element) {
|
||||
l.list.MoveBefore(e, p)
|
||||
}
|
||||
|
||||
// MoveAfter moves element <e> to its new position after `p`.
|
||||
// If <e> or <p> is not an element of <l>, or <e> == `p`, the list is not modified.
|
||||
// MoveAfter moves element `e` to its new position after `p`.
|
||||
// If `e` or `p` is not an element of `l`, or `e` == `p`, the list is not modified.
|
||||
// The element and `p` must not be nil.
|
||||
func (l *List) MoveAfter(e, p *Element) {
|
||||
l.mu.Lock()
|
||||
@ -303,8 +303,8 @@ func (l *List) MoveAfter(e, p *Element) {
|
||||
l.list.MoveAfter(e, p)
|
||||
}
|
||||
|
||||
// MoveToFront moves element <e> to the front of list `l`.
|
||||
// If <e> is not an element of `l`, the list is not modified.
|
||||
// MoveToFront moves element `e` to the front of list `l`.
|
||||
// If `e` is not an element of `l`, the list is not modified.
|
||||
// The element must not be nil.
|
||||
func (l *List) MoveToFront(e *Element) {
|
||||
l.mu.Lock()
|
||||
@ -315,8 +315,8 @@ func (l *List) MoveToFront(e *Element) {
|
||||
l.list.MoveToFront(e)
|
||||
}
|
||||
|
||||
// MoveToBack moves element <e> to the back of list `l`.
|
||||
// If <e> is not an element of `l`, the list is not modified.
|
||||
// MoveToBack moves element `e` to the back of list `l`.
|
||||
// If `e` is not an element of `l`, the list is not modified.
|
||||
// The element must not be nil.
|
||||
func (l *List) MoveToBack(e *Element) {
|
||||
l.mu.Lock()
|
||||
@ -328,7 +328,7 @@ func (l *List) MoveToBack(e *Element) {
|
||||
}
|
||||
|
||||
// PushBackList inserts a copy of an other list at the back of list `l`.
|
||||
// The lists <l> and `other` may be the same, but they must not be nil.
|
||||
// The lists `l` and `other` may be the same, but they must not be nil.
|
||||
func (l *List) PushBackList(other *List) {
|
||||
if l != other {
|
||||
other.mu.RLock()
|
||||
@ -343,7 +343,7 @@ func (l *List) PushBackList(other *List) {
|
||||
}
|
||||
|
||||
// PushFrontList inserts a copy of an other list at the front of list `l`.
|
||||
// The lists <l> and `other` may be the same, but they must not be nil.
|
||||
// The lists `l` and `other` may be the same, but they must not be nil.
|
||||
func (l *List) PushFrontList(other *List) {
|
||||
if l != other {
|
||||
other.mu.RLock()
|
||||
@ -357,8 +357,8 @@ func (l *List) PushFrontList(other *List) {
|
||||
l.list.PushFrontList(other.list)
|
||||
}
|
||||
|
||||
// InsertAfter inserts a new element <e> with value <v> immediately after <p> and returns `e`.
|
||||
// If <p> is not an element of `l`, the list is not modified.
|
||||
// InsertAfter inserts a new element `e` with value `v` immediately after `p` and returns `e`.
|
||||
// If `p` is not an element of `l`, the list is not modified.
|
||||
// The `p` must not be nil.
|
||||
func (l *List) InsertAfter(p *Element, v interface{}) (e *Element) {
|
||||
l.mu.Lock()
|
||||
@ -370,8 +370,8 @@ func (l *List) InsertAfter(p *Element, v interface{}) (e *Element) {
|
||||
return
|
||||
}
|
||||
|
||||
// InsertBefore inserts a new element <e> with value <v> immediately before <p> and returns `e`.
|
||||
// If <p> is not an element of `l`, the list is not modified.
|
||||
// InsertBefore inserts a new element `e` with value `v` immediately before `p` and returns `e`.
|
||||
// If `p` is not an element of `l`, the list is not modified.
|
||||
// The `p` must not be nil.
|
||||
func (l *List) InsertBefore(p *Element, v interface{}) (e *Element) {
|
||||
l.mu.Lock()
|
||||
@ -383,7 +383,7 @@ func (l *List) InsertBefore(p *Element, v interface{}) (e *Element) {
|
||||
return
|
||||
}
|
||||
|
||||
// Remove removes <e> from <l> if <e> is an element of list `l`.
|
||||
// Remove removes `e` from `l` if `e` is an element of list `l`.
|
||||
// It returns the element value e.Value.
|
||||
// The element must not be nil.
|
||||
func (l *List) Remove(e *Element) (value interface{}) {
|
||||
@ -396,7 +396,7 @@ func (l *List) Remove(e *Element) (value interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// Removes removes multiple elements <es> from <l> if <es> are elements of list `l`.
|
||||
// Removes removes multiple elements `es` from `l` if `es` are elements of list `l`.
|
||||
func (l *List) Removes(es []*Element) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
@ -205,7 +205,7 @@ func (m *AnyAnyMap) Pops(size int) map[interface{}]interface{} {
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// When setting value, if `value` is type of <func() interface {}>,
|
||||
// When setting value, if `value` is type of `func() interface {}`,
|
||||
// it will be executed with mutex.Lock of the hash map,
|
||||
// and its return value will be set to the map with `key`.
|
||||
//
|
||||
@ -287,8 +287,8 @@ func (m *AnyAnyMap) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *
|
||||
return gvar.New(m.GetOrSetFuncLock(key, f))
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *AnyAnyMap) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -298,7 +298,7 @@ func (m *AnyAnyMap) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *AnyAnyMap) SetIfNotExistFunc(key interface{}, f func() interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -308,7 +308,7 @@ func (m *AnyAnyMap) SetIfNotExistFunc(key interface{}, f func() interface{}) boo
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
@ -440,7 +440,7 @@ func (m *AnyAnyMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two hash maps.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
// The `other` map will be merged into the map `m`.
|
||||
func (m *AnyAnyMap) Merge(other *AnyAnyMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
@ -205,7 +205,7 @@ func (m *IntAnyMap) Pops(size int) map[int]interface{} {
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// When setting value, if `value` is type of <func() interface {}>,
|
||||
// When setting value, if `value` is type of `func() interface {}`,
|
||||
// it will be executed with mutex.Lock of the hash map,
|
||||
// and its return value will be set to the map with `key`.
|
||||
//
|
||||
@ -285,8 +285,8 @@ func (m *IntAnyMap) GetVarOrSetFuncLock(key int, f func() interface{}) *gvar.Var
|
||||
return gvar.New(m.GetOrSetFuncLock(key, f))
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *IntAnyMap) SetIfNotExist(key int, value interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -296,7 +296,7 @@ func (m *IntAnyMap) SetIfNotExist(key int, value interface{}) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *IntAnyMap) SetIfNotExistFunc(key int, f func() interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -306,7 +306,7 @@ func (m *IntAnyMap) SetIfNotExistFunc(key int, f func() interface{}) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
@ -438,7 +438,7 @@ func (m *IntAnyMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two hash maps.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
// The `other` map will be merged into the map `m`.
|
||||
func (m *IntAnyMap) Merge(other *IntAnyMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
@ -249,8 +249,8 @@ func (m *IntIntMap) GetOrSetFuncLock(key int, f func() int) int {
|
||||
}
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *IntIntMap) SetIfNotExist(key int, value int) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -260,7 +260,7 @@ func (m *IntIntMap) SetIfNotExist(key int, value int) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *IntIntMap) SetIfNotExistFunc(key int, f func() int) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -270,7 +270,7 @@ func (m *IntIntMap) SetIfNotExistFunc(key int, f func() int) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
@ -409,7 +409,7 @@ func (m *IntIntMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two hash maps.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
// The `other` map will be merged into the map `m`.
|
||||
func (m *IntIntMap) Merge(other *IntIntMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
@ -249,8 +249,8 @@ func (m *IntStrMap) GetOrSetFuncLock(key int, f func() string) string {
|
||||
}
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *IntStrMap) SetIfNotExist(key int, value string) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -260,7 +260,7 @@ func (m *IntStrMap) SetIfNotExist(key int, value string) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *IntStrMap) SetIfNotExistFunc(key int, f func() string) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -270,7 +270,7 @@ func (m *IntStrMap) SetIfNotExistFunc(key int, f func() string) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
@ -409,7 +409,7 @@ func (m *IntStrMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two hash maps.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
// The `other` map will be merged into the map `m`.
|
||||
func (m *IntStrMap) Merge(other *IntStrMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
@ -199,7 +199,7 @@ func (m *StrAnyMap) Pops(size int) map[string]interface{} {
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// When setting value, if `value` is type of <func() interface {}>,
|
||||
// When setting value, if `value` is type of `func() interface {}`,
|
||||
// it will be executed with mutex.Lock of the hash map,
|
||||
// and its return value will be set to the map with `key`.
|
||||
//
|
||||
@ -281,8 +281,8 @@ func (m *StrAnyMap) GetVarOrSetFuncLock(key string, f func() interface{}) *gvar.
|
||||
return gvar.New(m.GetOrSetFuncLock(key, f))
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *StrAnyMap) SetIfNotExist(key string, value interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -292,7 +292,7 @@ func (m *StrAnyMap) SetIfNotExist(key string, value interface{}) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *StrAnyMap) SetIfNotExistFunc(key string, f func() interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -302,7 +302,7 @@ func (m *StrAnyMap) SetIfNotExistFunc(key string, f func() interface{}) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
@ -434,7 +434,7 @@ func (m *StrAnyMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two hash maps.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
// The `other` map will be merged into the map `m`.
|
||||
func (m *StrAnyMap) Merge(other *StrAnyMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
@ -252,8 +252,8 @@ func (m *StrIntMap) GetOrSetFuncLock(key string, f func() int) int {
|
||||
}
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *StrIntMap) SetIfNotExist(key string, value int) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -263,7 +263,7 @@ func (m *StrIntMap) SetIfNotExist(key string, value int) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *StrIntMap) SetIfNotExistFunc(key string, f func() int) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -273,7 +273,7 @@ func (m *StrIntMap) SetIfNotExistFunc(key string, f func() int) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
@ -412,7 +412,7 @@ func (m *StrIntMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two hash maps.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
// The `other` map will be merged into the map `m`.
|
||||
func (m *StrIntMap) Merge(other *StrIntMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
@ -252,8 +252,8 @@ func (m *StrStrMap) GetOrSetFuncLock(key string, f func() string) string {
|
||||
}
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *StrStrMap) SetIfNotExist(key string, value string) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -263,7 +263,7 @@ func (m *StrStrMap) SetIfNotExist(key string, value string) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *StrStrMap) SetIfNotExistFunc(key string, f func() string) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -273,7 +273,7 @@ func (m *StrStrMap) SetIfNotExistFunc(key string, f func() string) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
@ -412,7 +412,7 @@ func (m *StrStrMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two hash maps.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
// The `other` map will be merged into the map `m`.
|
||||
func (m *StrStrMap) Merge(other *StrStrMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
@ -271,7 +271,7 @@ func (m *ListMap) Pops(size int) map[interface{}]interface{} {
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// When setting value, if `value` is type of <func() interface {}>,
|
||||
// When setting value, if `value` is type of `func() interface {}`,
|
||||
// it will be executed with mutex.Lock of the map,
|
||||
// and its return value will be set to the map with `key`.
|
||||
//
|
||||
@ -354,8 +354,8 @@ func (m *ListMap) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gv
|
||||
return gvar.New(m.GetOrSetFuncLock(key, f))
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *ListMap) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -365,7 +365,7 @@ func (m *ListMap) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (m *ListMap) SetIfNotExistFunc(key interface{}, f func() interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -375,7 +375,7 @@ func (m *ListMap) SetIfNotExistFunc(key interface{}, f func() interface{}) bool
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function `f` with mutex.Lock of the map.
|
||||
@ -486,7 +486,7 @@ func (m *ListMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two link maps.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
// The `other` map will be merged into the map `m`.
|
||||
func (m *ListMap) Merge(other *ListMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
@ -27,7 +27,8 @@ func New(safe ...bool) *Set {
|
||||
return NewSet(safe...)
|
||||
}
|
||||
|
||||
// See New.
|
||||
// NewSet create and returns a new set, which contains un-repeated items.
|
||||
// Also see New.
|
||||
func NewSet(safe ...bool) *Set {
|
||||
return &Set{
|
||||
data: make(map[interface{}]struct{}),
|
||||
@ -99,7 +100,7 @@ func (set *Set) AddIfNotExist(item interface{}) bool {
|
||||
// it adds the item to set and returns true if it does not exists in the set and
|
||||
// function `f` returns true, or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, if <item> is nil, it does nothing and returns false. The function `f`
|
||||
// Note that, if `item` is nil, it does nothing and returns false. The function `f`
|
||||
// is executed without writing lock.
|
||||
func (set *Set) AddIfNotExistFunc(item interface{}, f func() bool) bool {
|
||||
if item == nil {
|
||||
@ -121,11 +122,11 @@ func (set *Set) AddIfNotExistFunc(item interface{}, f func() bool) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// AddIfNotExistFunc checks whether item exists in the set,
|
||||
// AddIfNotExistFuncLock checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set and
|
||||
// function `f` returns true, or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, if <item> is nil, it does nothing and returns false. The function `f`
|
||||
// Note that, if `item` is nil, it does nothing and returns false. The function `f`
|
||||
// is executed within writing lock.
|
||||
func (set *Set) AddIfNotExistFuncLock(item interface{}, f func() bool) bool {
|
||||
if item == nil {
|
||||
@ -297,8 +298,8 @@ func (set *Set) IsSubsetOf(other *Set) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Union returns a new set which is the union of <set> and `others`.
|
||||
// Which means, all the items in <newSet> are in <set> or in `others`.
|
||||
// Union returns a new set which is the union of `set` and `others`.
|
||||
// Which means, all the items in `newSet` are in `set` or in `others`.
|
||||
func (set *Set) Union(others ...*Set) (newSet *Set) {
|
||||
newSet = NewSet()
|
||||
set.mu.RLock()
|
||||
@ -323,8 +324,8 @@ func (set *Set) Union(others ...*Set) (newSet *Set) {
|
||||
return
|
||||
}
|
||||
|
||||
// Diff returns a new set which is the difference set from <set> to `others`.
|
||||
// Which means, all the items in <newSet> are in <set> but not in `others`.
|
||||
// Diff returns a new set which is the difference set from `set` to `others`.
|
||||
// Which means, all the items in `newSet` are in `set` but not in `others`.
|
||||
func (set *Set) Diff(others ...*Set) (newSet *Set) {
|
||||
newSet = NewSet()
|
||||
set.mu.RLock()
|
||||
@ -344,8 +345,8 @@ func (set *Set) Diff(others ...*Set) (newSet *Set) {
|
||||
return
|
||||
}
|
||||
|
||||
// Intersect returns a new set which is the intersection from <set> to `others`.
|
||||
// Which means, all the items in <newSet> are in <set> and also in `others`.
|
||||
// Intersect returns a new set which is the intersection from `set` to `others`.
|
||||
// Which means, all the items in `newSet` are in `set` and also in `others`.
|
||||
func (set *Set) Intersect(others ...*Set) (newSet *Set) {
|
||||
newSet = NewSet()
|
||||
set.mu.RLock()
|
||||
@ -366,11 +367,11 @@ func (set *Set) Intersect(others ...*Set) (newSet *Set) {
|
||||
return
|
||||
}
|
||||
|
||||
// Complement returns a new set which is the complement from <set> to `full`.
|
||||
// Which means, all the items in <newSet> are in <full> and not in `set`.
|
||||
// Complement returns a new set which is the complement from `set` to `full`.
|
||||
// Which means, all the items in `newSet` are in `full` and not in `set`.
|
||||
//
|
||||
// It returns the difference between <full> and `set`
|
||||
// if the given set <full> is not the full set of `set`.
|
||||
// It returns the difference between `full` and `set`
|
||||
// if the given set `full` is not the full set of `set`.
|
||||
func (set *Set) Complement(full *Set) (newSet *Set) {
|
||||
newSet = NewSet()
|
||||
set.mu.RLock()
|
||||
@ -387,7 +388,7 @@ func (set *Set) Complement(full *Set) (newSet *Set) {
|
||||
return
|
||||
}
|
||||
|
||||
// Merge adds items from <others> sets into `set`.
|
||||
// Merge adds items from `others` sets into `set`.
|
||||
func (set *Set) Merge(others ...*Set) *Set {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
@ -417,7 +418,7 @@ func (set *Set) Sum() (sum int) {
|
||||
return
|
||||
}
|
||||
|
||||
// Pops randomly pops an item from set.
|
||||
// Pop randomly pops an item from set.
|
||||
func (set *Set) Pop() interface{} {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
|
@ -19,7 +19,7 @@ type IntSet struct {
|
||||
data map[int]struct{}
|
||||
}
|
||||
|
||||
// New create and returns a new set, which contains un-repeated items.
|
||||
// NewIntSet create and returns a new set, which contains un-repeated items.
|
||||
// The parameter `safe` is used to specify whether using set in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewIntSet(safe ...bool) *IntSet {
|
||||
@ -107,7 +107,7 @@ func (set *IntSet) AddIfNotExistFunc(item int, f func() bool) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// AddIfNotExistFunc checks whether item exists in the set,
|
||||
// AddIfNotExistFuncLock checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set and
|
||||
// function `f` returns true, or else it does nothing and returns false.
|
||||
//
|
||||
@ -257,8 +257,8 @@ func (set *IntSet) IsSubsetOf(other *IntSet) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Union returns a new set which is the union of <set> and `other`.
|
||||
// Which means, all the items in <newSet> are in <set> or in `other`.
|
||||
// Union returns a new set which is the union of `set` and `other`.
|
||||
// Which means, all the items in `newSet` are in `set` or in `other`.
|
||||
func (set *IntSet) Union(others ...*IntSet) (newSet *IntSet) {
|
||||
newSet = NewIntSet()
|
||||
set.mu.RLock()
|
||||
@ -283,8 +283,8 @@ func (set *IntSet) Union(others ...*IntSet) (newSet *IntSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Diff returns a new set which is the difference set from <set> to `other`.
|
||||
// Which means, all the items in <newSet> are in <set> but not in `other`.
|
||||
// Diff returns a new set which is the difference set from `set` to `other`.
|
||||
// Which means, all the items in `newSet` are in `set` but not in `other`.
|
||||
func (set *IntSet) Diff(others ...*IntSet) (newSet *IntSet) {
|
||||
newSet = NewIntSet()
|
||||
set.mu.RLock()
|
||||
@ -304,8 +304,8 @@ func (set *IntSet) Diff(others ...*IntSet) (newSet *IntSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Intersect returns a new set which is the intersection from <set> to `other`.
|
||||
// Which means, all the items in <newSet> are in <set> and also in `other`.
|
||||
// Intersect returns a new set which is the intersection from `set` to `other`.
|
||||
// Which means, all the items in `newSet` are in `set` and also in `other`.
|
||||
func (set *IntSet) Intersect(others ...*IntSet) (newSet *IntSet) {
|
||||
newSet = NewIntSet()
|
||||
set.mu.RLock()
|
||||
@ -326,11 +326,11 @@ func (set *IntSet) Intersect(others ...*IntSet) (newSet *IntSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Complement returns a new set which is the complement from <set> to `full`.
|
||||
// Which means, all the items in <newSet> are in <full> and not in `set`.
|
||||
// Complement returns a new set which is the complement from `set` to `full`.
|
||||
// Which means, all the items in `newSet` are in `full` and not in `set`.
|
||||
//
|
||||
// It returns the difference between <full> and `set`
|
||||
// if the given set <full> is not the full set of `set`.
|
||||
// It returns the difference between `full` and `set`
|
||||
// if the given set `full` is not the full set of `set`.
|
||||
func (set *IntSet) Complement(full *IntSet) (newSet *IntSet) {
|
||||
newSet = NewIntSet()
|
||||
set.mu.RLock()
|
||||
@ -347,7 +347,7 @@ func (set *IntSet) Complement(full *IntSet) (newSet *IntSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Merge adds items from <others> sets into `set`.
|
||||
// Merge adds items from `others` sets into `set`.
|
||||
func (set *IntSet) Merge(others ...*IntSet) *IntSet {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
@ -377,7 +377,7 @@ func (set *IntSet) Sum() (sum int) {
|
||||
return
|
||||
}
|
||||
|
||||
// Pops randomly pops an item from set.
|
||||
// Pop randomly pops an item from set.
|
||||
func (set *IntSet) Pop() int {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
|
@ -107,7 +107,7 @@ func (set *StrSet) AddIfNotExistFunc(item string, f func() bool) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// AddIfNotExistFunc checks whether item exists in the set,
|
||||
// AddIfNotExistFuncLock checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set and
|
||||
// function `f` returns true, or else it does nothing and returns false.
|
||||
//
|
||||
@ -285,8 +285,8 @@ func (set *StrSet) IsSubsetOf(other *StrSet) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Union returns a new set which is the union of <set> and `other`.
|
||||
// Which means, all the items in <newSet> are in <set> or in `other`.
|
||||
// Union returns a new set which is the union of `set` and `other`.
|
||||
// Which means, all the items in `newSet` are in `set` or in `other`.
|
||||
func (set *StrSet) Union(others ...*StrSet) (newSet *StrSet) {
|
||||
newSet = NewStrSet()
|
||||
set.mu.RLock()
|
||||
@ -311,8 +311,8 @@ func (set *StrSet) Union(others ...*StrSet) (newSet *StrSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Diff returns a new set which is the difference set from <set> to `other`.
|
||||
// Which means, all the items in <newSet> are in <set> but not in `other`.
|
||||
// Diff returns a new set which is the difference set from `set` to `other`.
|
||||
// Which means, all the items in `newSet` are in `set` but not in `other`.
|
||||
func (set *StrSet) Diff(others ...*StrSet) (newSet *StrSet) {
|
||||
newSet = NewStrSet()
|
||||
set.mu.RLock()
|
||||
@ -332,8 +332,8 @@ func (set *StrSet) Diff(others ...*StrSet) (newSet *StrSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Intersect returns a new set which is the intersection from <set> to `other`.
|
||||
// Which means, all the items in <newSet> are in <set> and also in `other`.
|
||||
// Intersect returns a new set which is the intersection from `set` to `other`.
|
||||
// Which means, all the items in `newSet` are in `set` and also in `other`.
|
||||
func (set *StrSet) Intersect(others ...*StrSet) (newSet *StrSet) {
|
||||
newSet = NewStrSet()
|
||||
set.mu.RLock()
|
||||
@ -354,11 +354,11 @@ func (set *StrSet) Intersect(others ...*StrSet) (newSet *StrSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Complement returns a new set which is the complement from <set> to `full`.
|
||||
// Which means, all the items in <newSet> are in <full> and not in `set`.
|
||||
// Complement returns a new set which is the complement from `set` to `full`.
|
||||
// Which means, all the items in `newSet` are in `full` and not in `set`.
|
||||
//
|
||||
// It returns the difference between <full> and `set`
|
||||
// if the given set <full> is not the full set of `set`.
|
||||
// It returns the difference between `full` and `set`
|
||||
// if the given set `full` is not the full set of `set`.
|
||||
func (set *StrSet) Complement(full *StrSet) (newSet *StrSet) {
|
||||
newSet = NewStrSet()
|
||||
set.mu.RLock()
|
||||
@ -375,7 +375,7 @@ func (set *StrSet) Complement(full *StrSet) (newSet *StrSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Merge adds items from <others> sets into `set`.
|
||||
// Merge adds items from `others` sets into `set`.
|
||||
func (set *StrSet) Merge(others ...*StrSet) *StrSet {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
@ -405,7 +405,7 @@ func (set *StrSet) Sum() (sum int) {
|
||||
return
|
||||
}
|
||||
|
||||
// Pops randomly pops an item from set.
|
||||
// Pop randomly pops an item from set.
|
||||
func (set *StrSet) Pop() string {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
|
@ -195,8 +195,8 @@ func (tree *AVLTree) GetVarOrSetFuncLock(key interface{}, f func() interface{})
|
||||
return gvar.New(tree.GetOrSetFuncLock(key, f))
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (tree *AVLTree) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, value)
|
||||
@ -206,7 +206,7 @@ func (tree *AVLTree) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (tree *AVLTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, f())
|
||||
@ -216,7 +216,7 @@ func (tree *AVLTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bo
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
@ -474,7 +474,7 @@ func (tree *AVLTree) IteratorAsc(f func(key, value interface{}) bool) {
|
||||
}
|
||||
|
||||
// IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`.
|
||||
// The parameter <key> specifies the start entry for iterating. The `match` specifies whether
|
||||
// The parameter `key` specifies the start entry for iterating. The `match` specifies whether
|
||||
// starting iterating if the `key` is fully matched, or else using index searching iterating.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *AVLTree) IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
||||
@ -508,7 +508,7 @@ func (tree *AVLTree) IteratorDesc(f func(key, value interface{}) bool) {
|
||||
}
|
||||
|
||||
// IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`.
|
||||
// The parameter <key> specifies the start entry for iterating. The `match` specifies whether
|
||||
// The parameter `key` specifies the start entry for iterating. The `match` specifies whether
|
||||
// starting iterating if the `key` is fully matched, or else using index searching iterating.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *AVLTree) IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
||||
|
@ -193,8 +193,8 @@ func (tree *BTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *g
|
||||
return gvar.New(tree.GetOrSetFuncLock(key, f))
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (tree *BTree) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, value)
|
||||
@ -204,7 +204,7 @@ func (tree *BTree) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (tree *BTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, f())
|
||||
@ -214,7 +214,7 @@ func (tree *BTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
@ -419,7 +419,7 @@ func (tree *BTree) IteratorAsc(f func(key, value interface{}) bool) {
|
||||
}
|
||||
|
||||
// IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`.
|
||||
// The parameter <key> specifies the start entry for iterating. The `match` specifies whether
|
||||
// The parameter `key` specifies the start entry for iterating. The `match` specifies whether
|
||||
// starting iterating if the `key` is fully matched, or else using index searching iterating.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *BTree) IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
||||
@ -494,7 +494,7 @@ func (tree *BTree) IteratorDesc(f func(key, value interface{}) bool) {
|
||||
}
|
||||
|
||||
// IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`.
|
||||
// The parameter <key> specifies the start entry for iterating. The `match` specifies whether
|
||||
// The parameter `key` specifies the start entry for iterating. The `match` specifies whether
|
||||
// starting iterating if the `key` is fully matched, or else using index searching iterating.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *BTree) IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
||||
|
@ -235,8 +235,8 @@ func (tree *RedBlackTree) GetVarOrSetFuncLock(key interface{}, f func() interfac
|
||||
return gvar.New(tree.GetOrSetFuncLock(key, f))
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (tree *RedBlackTree) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, value)
|
||||
@ -246,7 +246,7 @@ func (tree *RedBlackTree) SetIfNotExist(key interface{}, value interface{}) bool
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
func (tree *RedBlackTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, f())
|
||||
@ -256,7 +256,7 @@ func (tree *RedBlackTree) SetIfNotExistFunc(key interface{}, f func() interface{
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
// It returns false if `key` exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
@ -508,7 +508,7 @@ func (tree *RedBlackTree) IteratorAsc(f func(key, value interface{}) bool) {
|
||||
}
|
||||
|
||||
// IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`.
|
||||
// The parameter <key> specifies the start entry for iterating. The `match` specifies whether
|
||||
// The parameter `key` specifies the start entry for iterating. The `match` specifies whether
|
||||
// starting iterating if the `key` is fully matched, or else using index searching iterating.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *RedBlackTree) IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
||||
@ -559,7 +559,7 @@ func (tree *RedBlackTree) IteratorDesc(f func(key, value interface{}) bool) {
|
||||
}
|
||||
|
||||
// IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`.
|
||||
// The parameter <key> specifies the start entry for iterating. The `match` specifies whether
|
||||
// The parameter `key` specifies the start entry for iterating. The `match` specifies whether
|
||||
// starting iterating if the `key` is fully matched, or else using index searching iterating.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *RedBlackTree) IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
||||
|
@ -225,7 +225,7 @@ func (m *Model) getPrimaryKey() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// mergeArguments creates and returns new arguments by merging <m.extraArgs> and given `args`.
|
||||
// mergeArguments creates and returns new arguments by merging `m.extraArgs` and given `args`.
|
||||
func (m *Model) mergeArguments(args []interface{}) []interface{} {
|
||||
if len(m.extraArgs) > 0 {
|
||||
newArgs := make([]interface{}, len(m.extraArgs)+len(args))
|
||||
|
@ -17,7 +17,7 @@ var (
|
||||
)
|
||||
|
||||
// Instance returns an instance of redis client with specified group.
|
||||
// The <name> param is unnecessary, if `name` is not passed,
|
||||
// The `name` param is unnecessary, if `name` is not passed,
|
||||
// it returns a redis instance with default configuration group.
|
||||
func Instance(name ...string) *Redis {
|
||||
group := DefaultGroupName
|
||||
|
@ -86,7 +86,7 @@ func CallerWithFilter(filter string, skip ...int) (function string, path string,
|
||||
// callerFromIndex returns the caller position and according information exclusive of the
|
||||
// debug package.
|
||||
//
|
||||
// VERY NOTE THAT, the returned index value should be <index - 1> as the caller's start point.
|
||||
// VERY NOTE THAT, the returned index value should be `index - 1` as the caller's start point.
|
||||
func callerFromIndex(filters []string) (pc uintptr, file string, line int, index int) {
|
||||
var filtered, ok bool
|
||||
for index = 0; index < maxCallerDepth; index++ {
|
||||
|
@ -29,7 +29,7 @@ func EncodeToString(src []byte) string {
|
||||
return string(Encode(src))
|
||||
}
|
||||
|
||||
// EncryptFile encodes file content of <path> using BASE64 algorithms.
|
||||
// EncryptFile encodes file content of `path` using BASE64 algorithms.
|
||||
func EncodeFile(path string) ([]byte, error) {
|
||||
content, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
@ -38,7 +38,7 @@ func EncodeFile(path string) ([]byte, error) {
|
||||
return Encode(content), nil
|
||||
}
|
||||
|
||||
// MustEncodeFile encodes file content of <path> using BASE64 algorithms.
|
||||
// MustEncodeFile encodes file content of `path` using BASE64 algorithms.
|
||||
// It panics if any error occurs.
|
||||
func MustEncodeFile(path string) []byte {
|
||||
result, err := EncodeFile(path)
|
||||
@ -48,7 +48,7 @@ func MustEncodeFile(path string) []byte {
|
||||
return result
|
||||
}
|
||||
|
||||
// EncodeFileToString encodes file content of <path> to string using BASE64 algorithms.
|
||||
// EncodeFileToString encodes file content of `path` to string using BASE64 algorithms.
|
||||
func EncodeFileToString(path string) (string, error) {
|
||||
content, err := EncodeFile(path)
|
||||
if err != nil {
|
||||
@ -57,7 +57,7 @@ func EncodeFileToString(path string) (string, error) {
|
||||
return string(content), nil
|
||||
}
|
||||
|
||||
// MustEncodeFileToString encodes file content of <path> to string using BASE64 algorithms.
|
||||
// MustEncodeFileToString encodes file content of `path` to string using BASE64 algorithms.
|
||||
// It panics if any error occurs.
|
||||
func MustEncodeFileToString(path string) string {
|
||||
result, err := EncodeFileToString(path)
|
||||
|
@ -13,8 +13,8 @@ import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// BeEncode encodes one or multiple <values> into bytes using BigEndian.
|
||||
// It uses type asserting checking the type of each value of <values> and internally
|
||||
// BeEncode encodes one or multiple `values` into bytes using BigEndian.
|
||||
// It uses type asserting checking the type of each value of `values` and internally
|
||||
// calls corresponding converting function do the bytes converting.
|
||||
//
|
||||
// It supports common variable type asserting, and finally it uses fmt.Sprintf converting
|
||||
@ -269,7 +269,7 @@ func BeDecodeToFloat64(b []byte) float64 {
|
||||
return math.Float64frombits(binary.BigEndian.Uint64(BeFillUpSize(b, 8)))
|
||||
}
|
||||
|
||||
// BeFillUpSize fills up the bytes <b> to given length <l> using big BigEndian.
|
||||
// BeFillUpSize fills up the bytes `b` to given length `l` using big BigEndian.
|
||||
//
|
||||
// Note that it creates a new bytes slice by copying the original one to avoid changing
|
||||
// the original parameter bytes.
|
||||
|
@ -13,8 +13,8 @@ import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// LeEncode encodes one or multiple <values> into bytes using LittleEndian.
|
||||
// It uses type asserting checking the type of each value of <values> and internally
|
||||
// LeEncode encodes one or multiple `values` into bytes using LittleEndian.
|
||||
// It uses type asserting checking the type of each value of `values` and internally
|
||||
// calls corresponding converting function do the bytes converting.
|
||||
//
|
||||
// It supports common variable type asserting, and finally it uses fmt.Sprintf converting
|
||||
|
@ -40,20 +40,20 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
// Supported returns whether charset <charset> is supported.
|
||||
// Supported returns whether charset `charset` is supported.
|
||||
func Supported(charset string) bool {
|
||||
return getEncoding(charset) != nil
|
||||
}
|
||||
|
||||
// Convert converts <src> charset encoding from <srcCharset> to <dstCharset>,
|
||||
// Convert converts `src` charset encoding from `srcCharset` to `dstCharset`,
|
||||
// and returns the converted string.
|
||||
// It returns <src> as <dst> if it fails converting.
|
||||
// It returns `src` as `dst` if it fails converting.
|
||||
func Convert(dstCharset string, srcCharset string, src string) (dst string, err error) {
|
||||
if dstCharset == srcCharset {
|
||||
return src, nil
|
||||
}
|
||||
dst = src
|
||||
// Converting <src> to UTF-8.
|
||||
// Converting `src` to UTF-8.
|
||||
if srcCharset != "UTF-8" {
|
||||
if e := getEncoding(srcCharset); e != nil {
|
||||
tmp, err := ioutil.ReadAll(
|
||||
@ -67,7 +67,7 @@ func Convert(dstCharset string, srcCharset string, src string) (dst string, err
|
||||
return dst, gerror.NewCodef(gcode.CodeInvalidParameter, "unsupported srcCharset: %s", srcCharset)
|
||||
}
|
||||
}
|
||||
// Do the converting from UTF-8 to <dstCharset>.
|
||||
// Do the converting from UTF-8 to `dstCharset`.
|
||||
if dstCharset != "UTF-8" {
|
||||
if e := getEncoding(dstCharset); e != nil {
|
||||
tmp, err := ioutil.ReadAll(
|
||||
@ -86,20 +86,20 @@ func Convert(dstCharset string, srcCharset string, src string) (dst string, err
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
// ToUTF8 converts <src> charset encoding from <srcCharset> to UTF-8 ,
|
||||
// ToUTF8 converts `src` charset encoding from `srcCharset` to UTF-8 ,
|
||||
// and returns the converted string.
|
||||
func ToUTF8(srcCharset string, src string) (dst string, err error) {
|
||||
return Convert("UTF-8", srcCharset, src)
|
||||
}
|
||||
|
||||
// UTF8To converts <src> charset encoding from UTF-8 to <dstCharset>,
|
||||
// UTF8To converts `src` charset encoding from UTF-8 to `dstCharset`,
|
||||
// and returns the converted string.
|
||||
func UTF8To(dstCharset string, src string) (dst string, err error) {
|
||||
return Convert(dstCharset, "UTF-8", src)
|
||||
}
|
||||
|
||||
// getEncoding returns the encoding.Encoding interface object for <charset>.
|
||||
// It returns nil if <charset> is not supported.
|
||||
// getEncoding returns the encoding.Encoding interface object for `charset`.
|
||||
// It returns nil if `charset` is not supported.
|
||||
func getEncoding(charset string) encoding.Encoding {
|
||||
if c, ok := charsetAlias[charset]; ok {
|
||||
charset = c
|
||||
|
@ -13,11 +13,11 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// Gzip compresses <data> using gzip algorithm.
|
||||
// The optional parameter <level> specifies the compression level from
|
||||
// Gzip compresses `data` using gzip algorithm.
|
||||
// The optional parameter `level` specifies the compression level from
|
||||
// 1 to 9 which means from none to the best compression.
|
||||
//
|
||||
// Note that it returns error if given <level> is invalid.
|
||||
// Note that it returns error if given `level` is invalid.
|
||||
func Gzip(data []byte, level ...int) ([]byte, error) {
|
||||
var (
|
||||
writer *gzip.Writer
|
||||
@ -41,7 +41,7 @@ func Gzip(data []byte, level ...int) ([]byte, error) {
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// GzipFile compresses the file <src> to <dst> using gzip algorithm.
|
||||
// GzipFile compresses the file `src` to `dst` using gzip algorithm.
|
||||
func GzipFile(src, dst string, level ...int) error {
|
||||
var (
|
||||
writer *gzip.Writer
|
||||
@ -75,7 +75,7 @@ func GzipFile(src, dst string, level ...int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnGzip decompresses <data> with gzip algorithm.
|
||||
// UnGzip decompresses `data` with gzip algorithm.
|
||||
func UnGzip(data []byte) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
reader, err := gzip.NewReader(bytes.NewReader(data))
|
||||
@ -91,7 +91,7 @@ func UnGzip(data []byte) ([]byte, error) {
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// UnGzip decompresses file <src> to <dst> using gzip algorithm.
|
||||
// UnGzip decompresses file `src` to `dst` using gzip algorithm.
|
||||
func UnGzipFile(src, dst string) error {
|
||||
srcFile, err := gfile.Open(src)
|
||||
if err != nil {
|
||||
|
@ -19,10 +19,10 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ZipPath compresses <paths> to <dest> using zip compressing algorithm.
|
||||
// The unnecessary parameter <prefix> indicates the path prefix for zip file.
|
||||
// ZipPath compresses `paths` to `dest` using zip compressing algorithm.
|
||||
// The unnecessary parameter `prefix` indicates the path prefix for zip file.
|
||||
//
|
||||
// Note that the parameter <paths> can be either a directory or a file, which
|
||||
// Note that the parameter `paths` can be either a directory or a file, which
|
||||
// supports multiple paths join with ','.
|
||||
func ZipPath(paths, dest string, prefix ...string) error {
|
||||
writer, err := os.Create(dest)
|
||||
@ -41,10 +41,10 @@ func ZipPath(paths, dest string, prefix ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ZipPathWriter compresses <paths> to <writer> using zip compressing algorithm.
|
||||
// The unnecessary parameter <prefix> indicates the path prefix for zip file.
|
||||
// ZipPathWriter compresses `paths` to `writer` using zip compressing algorithm.
|
||||
// The unnecessary parameter `prefix` indicates the path prefix for zip file.
|
||||
//
|
||||
// Note that the parameter <paths> can be either a directory or a file, which
|
||||
// Note that the parameter `paths` can be either a directory or a file, which
|
||||
// supports multiple paths join with ','.
|
||||
func ZipPathWriter(paths string, writer io.Writer, prefix ...string) error {
|
||||
zipWriter := zip.NewWriter(writer)
|
||||
@ -58,10 +58,10 @@ func ZipPathWriter(paths string, writer io.Writer, prefix ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// doZipPathWriter compresses the file of given <path> and writes the content to <zipWriter>.
|
||||
// The parameter <exclude> specifies the exclusive file path that is not compressed to <zipWriter>,
|
||||
// doZipPathWriter compresses the file of given `path` and writes the content to `zipWriter`.
|
||||
// The parameter `exclude` specifies the exclusive file path that is not compressed to `zipWriter`,
|
||||
// commonly the destination zip file path.
|
||||
// The unnecessary parameter <prefix> indicates the path prefix for zip file.
|
||||
// The unnecessary parameter `prefix` indicates the path prefix for zip file.
|
||||
func doZipPathWriter(path string, exclude string, zipWriter *zip.Writer, prefix ...string) error {
|
||||
var err error
|
||||
var files []string
|
||||
@ -108,11 +108,11 @@ func doZipPathWriter(path string, exclude string, zipWriter *zip.Writer, prefix
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnZipFile decompresses <archive> to <dest> using zip compressing algorithm.
|
||||
// The optional parameter <path> specifies the unzipped path of <archive>,
|
||||
// UnZipFile decompresses `archive` to `dest` using zip compressing algorithm.
|
||||
// The optional parameter `path` specifies the unzipped path of `archive`,
|
||||
// which can be used to specify part of the archive file to unzip.
|
||||
//
|
||||
// Note that the parameter <dest> should be a directory.
|
||||
// Note that the parameter `dest` should be a directory.
|
||||
func UnZipFile(archive, dest string, path ...string) error {
|
||||
readerCloser, err := zip.OpenReader(archive)
|
||||
if err != nil {
|
||||
@ -122,11 +122,11 @@ func UnZipFile(archive, dest string, path ...string) error {
|
||||
return unZipFileWithReader(&readerCloser.Reader, dest, path...)
|
||||
}
|
||||
|
||||
// UnZipContent decompresses <data> to <dest> using zip compressing algorithm.
|
||||
// The parameter <path> specifies the unzipped path of <archive>,
|
||||
// UnZipContent decompresses `data` to `dest` using zip compressing algorithm.
|
||||
// The parameter `path` specifies the unzipped path of `archive`,
|
||||
// which can be used to specify part of the archive file to unzip.
|
||||
//
|
||||
// Note that the parameter <dest> should be a directory.
|
||||
// Note that the parameter `dest` should be a directory.
|
||||
func UnZipContent(data []byte, dest string, path ...string) error {
|
||||
reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
|
||||
if err != nil {
|
||||
@ -186,8 +186,8 @@ func unZipFileWithReader(reader *zip.Reader, dest string, path ...string) error
|
||||
return nil
|
||||
}
|
||||
|
||||
// zipFile compresses the file of given <path> and writes the content to <zw>.
|
||||
// The parameter <prefix> indicates the path prefix for zip file.
|
||||
// zipFile compresses the file of given `path` and writes the content to `zw`.
|
||||
// The parameter `prefix` indicates the path prefix for zip file.
|
||||
func zipFile(path string, prefix string, zw *zip.Writer) error {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
|
@ -13,7 +13,7 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// Zlib compresses <data> with zlib algorithm.
|
||||
// Zlib compresses `data` with zlib algorithm.
|
||||
func Zlib(data []byte) ([]byte, error) {
|
||||
if data == nil || len(data) < 13 {
|
||||
return data, nil
|
||||
@ -30,7 +30,7 @@ func Zlib(data []byte) ([]byte, error) {
|
||||
return in.Bytes(), nil
|
||||
}
|
||||
|
||||
// UnZlib decompresses <data> with zlib algorithm.
|
||||
// UnZlib decompresses `data` with zlib algorithm.
|
||||
func UnZlib(data []byte) ([]byte, error) {
|
||||
if data == nil || len(data) < 13 {
|
||||
return data, nil
|
||||
|
@ -43,7 +43,7 @@ type iInterface interface {
|
||||
Interface() interface{}
|
||||
}
|
||||
|
||||
// setValue sets <value> to <j> by <pattern>.
|
||||
// setValue sets `value` to `j` by `pattern`.
|
||||
// Note:
|
||||
// 1. If value is nil and removed is true, means deleting this value;
|
||||
// 2. It's quite complicated in hierarchical data search, node creating and data assignment;
|
||||
@ -206,7 +206,7 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
// If the variable pointed to by the <pointer> is not of a reference type,
|
||||
// If the variable pointed to by the `pointer` is not of a reference type,
|
||||
// then it modifies the variable via its the parent, ie: pparent.
|
||||
default:
|
||||
if removed && value == nil {
|
||||
@ -250,7 +250,7 @@ done:
|
||||
return nil
|
||||
}
|
||||
|
||||
// convertValue converts <value> to map[string]interface{} or []interface{},
|
||||
// convertValue converts `value` to map[string]interface{} or []interface{},
|
||||
// which can be supported for hierarchical data access.
|
||||
func (j *Json) convertValue(value interface{}) interface{} {
|
||||
switch value.(type) {
|
||||
@ -283,7 +283,7 @@ func (j *Json) convertValue(value interface{}) interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
// setPointerWithValue sets <key>:<value> to <pointer>, the <key> may be a map key or slice index.
|
||||
// setPointerWithValue sets `key`:`value` to `pointer`, the `key` may be a map key or slice index.
|
||||
// It returns the pointer to the new value set.
|
||||
func (j *Json) setPointerWithValue(pointer *interface{}, key string, value interface{}) *interface{} {
|
||||
switch (*pointer).(type) {
|
||||
@ -308,7 +308,7 @@ func (j *Json) setPointerWithValue(pointer *interface{}, key string, value inter
|
||||
return pointer
|
||||
}
|
||||
|
||||
// getPointerByPattern returns a pointer to the value by specified <pattern>.
|
||||
// getPointerByPattern returns a pointer to the value by specified `pattern`.
|
||||
func (j *Json) getPointerByPattern(pattern string) *interface{} {
|
||||
if j.vc {
|
||||
return j.getPointerByPatternWithViolenceCheck(pattern)
|
||||
@ -317,7 +317,7 @@ func (j *Json) getPointerByPattern(pattern string) *interface{} {
|
||||
}
|
||||
}
|
||||
|
||||
// getPointerByPatternWithViolenceCheck returns a pointer to the value of specified <pattern> with violence check.
|
||||
// getPointerByPatternWithViolenceCheck returns a pointer to the value of specified `pattern` with violence check.
|
||||
func (j *Json) getPointerByPatternWithViolenceCheck(pattern string) *interface{} {
|
||||
if !j.vc {
|
||||
return j.getPointerByPatternWithoutViolenceCheck(pattern)
|
||||
@ -367,7 +367,7 @@ func (j *Json) getPointerByPatternWithViolenceCheck(pattern string) *interface{}
|
||||
return nil
|
||||
}
|
||||
|
||||
// getPointerByPatternWithoutViolenceCheck returns a pointer to the value of specified <pattern>, with no violence check.
|
||||
// getPointerByPatternWithoutViolenceCheck returns a pointer to the value of specified `pattern`, with no violence check.
|
||||
func (j *Json) getPointerByPatternWithoutViolenceCheck(pattern string) *interface{} {
|
||||
if j.vc {
|
||||
return j.getPointerByPatternWithViolenceCheck(pattern)
|
||||
@ -401,7 +401,7 @@ func (j *Json) getPointerByPatternWithoutViolenceCheck(pattern string) *interfac
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkPatternByPointer checks whether there's value by <key> in specified <pointer>.
|
||||
// checkPatternByPointer checks whether there's value by `key` in specified `pointer`.
|
||||
// It returns a pointer to the value.
|
||||
func (j *Json) checkPatternByPointer(key string, pointer *interface{}) *interface{} {
|
||||
switch (*pointer).(type) {
|
||||
|
@ -27,21 +27,21 @@ func (j *Json) Var() *gvar.Var {
|
||||
return gvar.New(j.Interface())
|
||||
}
|
||||
|
||||
// IsNil checks whether the value pointed by <j> is nil.
|
||||
// IsNil checks whether the value pointed by `j` is nil.
|
||||
func (j *Json) IsNil() bool {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
return j.p == nil || *(j.p) == nil
|
||||
}
|
||||
|
||||
// Get retrieves and returns value by specified <pattern>.
|
||||
// It returns all values of current Json object if <pattern> is given empty or string ".".
|
||||
// It returns nil if no value found by <pattern>.
|
||||
// Get retrieves and returns value by specified `pattern`.
|
||||
// It returns all values of current Json object if `pattern` is given empty or string ".".
|
||||
// It returns nil if no value found by `pattern`.
|
||||
//
|
||||
// We can also access slice item by its index number in <pattern> like:
|
||||
// We can also access slice item by its index number in `pattern` like:
|
||||
// "list.10", "array.0.name", "array.0.1.id".
|
||||
//
|
||||
// It returns a default value specified by <def> if value for <pattern> is not found.
|
||||
// It returns a default value specified by `def` if value for `pattern` is not found.
|
||||
func (j *Json) Get(pattern string, def ...interface{}) *gvar.Var {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
@ -66,13 +66,13 @@ func (j *Json) Get(pattern string, def ...interface{}) *gvar.Var {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetJson gets the value by specified <pattern>,
|
||||
// GetJson gets the value by specified `pattern`,
|
||||
// and converts it to a un-concurrent-safe Json object.
|
||||
func (j *Json) GetJson(pattern string, def ...interface{}) *Json {
|
||||
return New(j.Get(pattern, def...).Val())
|
||||
}
|
||||
|
||||
// GetJsons gets the value by specified <pattern>,
|
||||
// GetJsons gets the value by specified `pattern`,
|
||||
// and converts it to a slice of un-concurrent-safe Json object.
|
||||
func (j *Json) GetJsons(pattern string, def ...interface{}) []*Json {
|
||||
array := j.Get(pattern, def...).Array()
|
||||
@ -86,7 +86,7 @@ func (j *Json) GetJsons(pattern string, def ...interface{}) []*Json {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetJsonMap gets the value by specified <pattern>,
|
||||
// GetJsonMap gets the value by specified `pattern`,
|
||||
// and converts it to a map of un-concurrent-safe Json object.
|
||||
func (j *Json) GetJsonMap(pattern string, def ...interface{}) map[string]*Json {
|
||||
m := j.Get(pattern, def...).Map()
|
||||
@ -100,25 +100,25 @@ func (j *Json) GetJsonMap(pattern string, def ...interface{}) map[string]*Json {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Set sets value with specified <pattern>.
|
||||
// Set sets value with specified `pattern`.
|
||||
// It supports hierarchical data access by char separator, which is '.' in default.
|
||||
func (j *Json) Set(pattern string, value interface{}) error {
|
||||
return j.setValue(pattern, value, false)
|
||||
}
|
||||
|
||||
// Remove deletes value with specified <pattern>.
|
||||
// Remove deletes value with specified `pattern`.
|
||||
// It supports hierarchical data access by char separator, which is '.' in default.
|
||||
func (j *Json) Remove(pattern string) error {
|
||||
return j.setValue(pattern, nil, true)
|
||||
}
|
||||
|
||||
// Contains checks whether the value by specified <pattern> exist.
|
||||
// Contains checks whether the value by specified `pattern` exist.
|
||||
func (j *Json) Contains(pattern string) bool {
|
||||
return j.Get(pattern) != nil
|
||||
}
|
||||
|
||||
// Len returns the length/size of the value by specified <pattern>.
|
||||
// The target value by <pattern> should be type of slice or map.
|
||||
// Len returns the length/size of the value by specified `pattern`.
|
||||
// The target value by `pattern` should be type of slice or map.
|
||||
// It returns -1 if the target value is not found, or its type is invalid.
|
||||
func (j *Json) Len(pattern string) int {
|
||||
p := j.getPointerByPattern(pattern)
|
||||
@ -135,8 +135,8 @@ func (j *Json) Len(pattern string) int {
|
||||
return -1
|
||||
}
|
||||
|
||||
// Append appends value to the value by specified <pattern>.
|
||||
// The target value by <pattern> should be type of slice.
|
||||
// Append appends value to the value by specified `pattern`.
|
||||
// The target value by `pattern` should be type of slice.
|
||||
func (j *Json) Append(pattern string, value interface{}) error {
|
||||
p := j.getPointerByPattern(pattern)
|
||||
if p == nil || *p == nil {
|
||||
@ -168,7 +168,7 @@ func (j *Json) Array() []interface{} {
|
||||
}
|
||||
|
||||
// Scan automatically calls Struct or Structs function according to the type of parameter
|
||||
// <pointer> to implement the converting.
|
||||
// `pointer` to implement the converting.
|
||||
func (j *Json) Scan(pointer interface{}, mapping ...map[string]string) error {
|
||||
return j.Var().Scan(pointer, mapping...)
|
||||
}
|
||||
@ -180,7 +180,7 @@ func (j *Json) Dump() {
|
||||
gutil.Dump(*j.p)
|
||||
}
|
||||
|
||||
// Export returns <j> as a string with more manually readable.
|
||||
// Export returns `j` as a string with more manually readable.
|
||||
func (j *Json) Export() string {
|
||||
j.mu.RLock()
|
||||
defer j.mu.RUnlock()
|
||||
|
@ -24,22 +24,22 @@ import (
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// New creates a Json object with any variable type of <data>, but <data> should be a map
|
||||
// New creates a Json object with any variable type of `data`, but `data` should be a map
|
||||
// or slice for data access reason, or it will make no sense.
|
||||
//
|
||||
// The parameter <safe> specifies whether using this Json object in concurrent-safe context,
|
||||
// The parameter `safe` specifies whether using this Json object in concurrent-safe context,
|
||||
// which is false in default.
|
||||
func New(data interface{}, safe ...bool) *Json {
|
||||
return NewWithTag(data, "json", safe...)
|
||||
}
|
||||
|
||||
// NewWithTag creates a Json object with any variable type of <data>, but <data> should be a map
|
||||
// NewWithTag creates a Json object with any variable type of `data`, but `data` should be a map
|
||||
// or slice for data access reason, or it will make no sense.
|
||||
//
|
||||
// The parameter <tags> specifies priority tags for struct conversion to map, multiple tags joined
|
||||
// The parameter `tags` specifies priority tags for struct conversion to map, multiple tags joined
|
||||
// with char ','.
|
||||
//
|
||||
// The parameter <safe> specifies whether using this Json object in concurrent-safe context, which
|
||||
// The parameter `safe` specifies whether using this Json object in concurrent-safe context, which
|
||||
// is false in default.
|
||||
func NewWithTag(data interface{}, tags string, safe ...bool) *Json {
|
||||
option := Options{
|
||||
@ -51,7 +51,7 @@ func NewWithTag(data interface{}, tags string, safe ...bool) *Json {
|
||||
return NewWithOptions(data, option)
|
||||
}
|
||||
|
||||
// NewWithOptions creates a Json object with any variable type of <data>, but <data> should be a map
|
||||
// NewWithOptions creates a Json object with any variable type of `data`, but `data` should be a map
|
||||
// or slice for data access reason, or it will make no sense.
|
||||
func NewWithOptions(data interface{}, options Options) *Json {
|
||||
var j *Json
|
||||
@ -104,7 +104,7 @@ func NewWithOptions(data interface{}, options Options) *Json {
|
||||
return j
|
||||
}
|
||||
|
||||
// Load loads content from specified file <path>, and creates a Json object from its content.
|
||||
// Load loads content from specified file `path`, and creates a Json object from its content.
|
||||
func Load(path string, safe ...bool) (*Json, error) {
|
||||
if p, err := gfile.Search(path); err != nil {
|
||||
return nil, err
|
||||
@ -163,7 +163,7 @@ func LoadToml(data interface{}, safe ...bool) (*Json, error) {
|
||||
return doLoadContentWithOptions("toml", gconv.Bytes(data), option)
|
||||
}
|
||||
|
||||
// LoadContent creates a Json object from given content, it checks the data type of <content>
|
||||
// LoadContent creates a Json object from given content, it checks the data type of `content`
|
||||
// automatically, supporting data content type as follows:
|
||||
// JSON, XML, INI, YAML and TOML.
|
||||
func LoadContent(data interface{}, safe ...bool) (*Json, error) {
|
||||
@ -193,7 +193,7 @@ func LoadContentType(dataType string, data interface{}, safe ...bool) (*Json, er
|
||||
return doLoadContentWithOptions(dataType, content, option)
|
||||
}
|
||||
|
||||
// IsValidDataType checks and returns whether given <dataType> a valid data type for loading.
|
||||
// IsValidDataType checks and returns whether given `dataType` a valid data type for loading.
|
||||
func IsValidDataType(dataType string) bool {
|
||||
if dataType == "" {
|
||||
return false
|
||||
@ -285,7 +285,7 @@ func doLoadContentWithOptions(dataType string, data []byte, options Options) (*J
|
||||
return NewWithOptions(result, options), nil
|
||||
}
|
||||
|
||||
// checkDataType automatically checks and returns the data type for <content>.
|
||||
// checkDataType automatically checks and returns the data type for `content`.
|
||||
// Note that it uses regular expression for loose checking, you can use LoadXXX/LoadContentType
|
||||
// functions to load the content for certain content type.
|
||||
func checkDataType(content []byte) string {
|
||||
|
@ -12,20 +12,20 @@ import (
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// Valid checks whether <data> is a valid JSON data type.
|
||||
// The parameter <data> specifies the json format data, which can be either
|
||||
// Valid checks whether `data` is a valid JSON data type.
|
||||
// The parameter `data` specifies the json format data, which can be either
|
||||
// bytes or string type.
|
||||
func Valid(data interface{}) bool {
|
||||
return json.Valid(gconv.Bytes(data))
|
||||
}
|
||||
|
||||
// Encode encodes any golang variable <value> to JSON bytes.
|
||||
// Encode encodes any golang variable `value` to JSON bytes.
|
||||
func Encode(value interface{}) ([]byte, error) {
|
||||
return json.Marshal(value)
|
||||
}
|
||||
|
||||
// Decode decodes json format <data> to golang variable.
|
||||
// The parameter <data> can be either bytes or string type.
|
||||
// Decode decodes json format `data` to golang variable.
|
||||
// The parameter `data` can be either bytes or string type.
|
||||
func Decode(data interface{}) (interface{}, error) {
|
||||
var value interface{}
|
||||
if err := DecodeTo(gconv.Bytes(data), &value); err != nil {
|
||||
@ -35,9 +35,9 @@ func Decode(data interface{}) (interface{}, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// DecodeTo decodes json format <data> to specified golang variable <v>.
|
||||
// The parameter <data> can be either bytes or string type.
|
||||
// The parameter <v> should be a pointer type.
|
||||
// DecodeTo decodes json format `data` to specified golang variable `v`.
|
||||
// The parameter `data` can be either bytes or string type.
|
||||
// The parameter `v` should be a pointer type.
|
||||
func DecodeTo(data interface{}, v interface{}) error {
|
||||
decoder := json.NewDecoder(bytes.NewReader(gconv.Bytes(data)))
|
||||
// Do not use number, it converts float64 to json.Number type,
|
||||
@ -47,8 +47,8 @@ func DecodeTo(data interface{}, v interface{}) error {
|
||||
return decoder.Decode(v)
|
||||
}
|
||||
|
||||
// DecodeToJson codes json format <data> to a Json object.
|
||||
// The parameter <data> can be either bytes or string type.
|
||||
// DecodeToJson codes json format `data` to a Json object.
|
||||
// The parameter `data` can be either bytes or string type.
|
||||
func DecodeToJson(data interface{}, safe ...bool) (*Json, error) {
|
||||
if v, err := Decode(gconv.Bytes(data)); err != nil {
|
||||
return nil, err
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
)
|
||||
|
||||
// Decode parses <content> into and returns as map.
|
||||
// Decode parses `content` into and returns as map.
|
||||
func Decode(content []byte) (map[string]interface{}, error) {
|
||||
res, err := convert(content)
|
||||
if err != nil {
|
||||
@ -24,7 +24,7 @@ func Decode(content []byte) (map[string]interface{}, error) {
|
||||
return mxj.NewMapXml(res)
|
||||
}
|
||||
|
||||
// DecodeWithoutRoot parses <content> into a map, and returns the map without root level.
|
||||
// DecodeWithoutRoot parses `content` into a map, and returns the map without root level.
|
||||
func DecodeWithoutRoot(content []byte) (map[string]interface{}, error) {
|
||||
res, err := convert(content)
|
||||
if err != nil {
|
||||
@ -42,19 +42,19 @@ func DecodeWithoutRoot(content []byte) (map[string]interface{}, error) {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Encode encodes map <m> to a XML format content as bytes.
|
||||
// The optional parameter <rootTag> is used to specify the XML root tag.
|
||||
// Encode encodes map `m` to an XML format content as bytes.
|
||||
// The optional parameter `rootTag` is used to specify the XML root tag.
|
||||
func Encode(m map[string]interface{}, rootTag ...string) ([]byte, error) {
|
||||
return mxj.Map(m).Xml(rootTag...)
|
||||
}
|
||||
|
||||
// EncodeWithIndent encodes map <m> to an XML format content as bytes with indent.
|
||||
// The optional parameter <rootTag> is used to specify the XML root tag.
|
||||
// EncodeWithIndent encodes map `m` to an XML format content as bytes with indent.
|
||||
// The optional parameter `rootTag` is used to specify the XML root tag.
|
||||
func EncodeWithIndent(m map[string]interface{}, rootTag ...string) ([]byte, error) {
|
||||
return mxj.Map(m).XmlIndent("", "\t", rootTag...)
|
||||
}
|
||||
|
||||
// ToJson converts <content> as XML format into JSON format bytes.
|
||||
// ToJson converts `content` as XML format into JSON format bytes.
|
||||
func ToJson(content []byte) ([]byte, error) {
|
||||
res, err := convert(content)
|
||||
if err != nil {
|
||||
|
@ -61,7 +61,7 @@ func TryCatch(try func(), catch ...func(exception error)) {
|
||||
}
|
||||
|
||||
// IsNil checks whether given `value` is nil.
|
||||
// Parameter <traceSource> is used for tracing to the source variable if given `value` is type
|
||||
// Parameter `traceSource` is used for tracing to the source variable if given `value` is type
|
||||
// of pinter that also points to a pointer. It returns nil if the source is nil when `traceSource`
|
||||
// is true.
|
||||
// Note that it might use reflect feature which affects performance a little.
|
||||
|
@ -49,8 +49,8 @@ func GetOrSetFuncLock(name string, f func() interface{}) interface{} {
|
||||
return localInstances.GetOrSetFuncLock(name, f)
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <instance> to the map if the `name` does not exist, then returns true.
|
||||
// It returns false if <name> exists, and `instance` would be ignored.
|
||||
// SetIfNotExist sets `instance` to the map if the `name` does not exist, then returns true.
|
||||
// It returns false if `name` exists, and `instance` would be ignored.
|
||||
func SetIfNotExist(name string, instance interface{}) bool {
|
||||
return localInstances.SetIfNotExist(name, instance)
|
||||
}
|
||||
|
@ -34,13 +34,13 @@ func Tf(ctx context.Context, format string, values ...interface{}) string {
|
||||
return Instance().TranslateFormat(ctx, format, values...)
|
||||
}
|
||||
|
||||
// TranslateFormat translates, formats and returns the <format> with configured language
|
||||
// and given <values>.
|
||||
// TranslateFormat translates, formats and returns the `format` with configured language
|
||||
// and given `values`.
|
||||
func TranslateFormat(ctx context.Context, format string, values ...interface{}) string {
|
||||
return Instance().TranslateFormat(ctx, format, values...)
|
||||
}
|
||||
|
||||
// Translate translates <content> with configured language and returns the translated content.
|
||||
// Translate translates `content` with configured language and returns the translated content.
|
||||
func Translate(ctx context.Context, content string) string {
|
||||
return Instance().Translate(ctx, content)
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ var (
|
||||
)
|
||||
|
||||
// Instance returns an instance of Resource.
|
||||
// The parameter <name> is the name for the instance.
|
||||
// The parameter `name` is the name for the instance.
|
||||
func Instance(name ...string) *Manager {
|
||||
key := DefaultName
|
||||
if len(name) > 0 && name[0] != "" {
|
||||
|
@ -48,7 +48,7 @@ var (
|
||||
)
|
||||
|
||||
// New creates and returns a new i18n manager.
|
||||
// The optional parameter <option> specifies the custom options for i18n manager.
|
||||
// The optional parameter `option` specifies the custom options for i18n manager.
|
||||
// It uses a default one if it's not passed.
|
||||
func New(options ...Options) *Manager {
|
||||
var opts Options
|
||||
@ -132,13 +132,13 @@ func (m *Manager) Tf(ctx context.Context, format string, values ...interface{})
|
||||
return m.TranslateFormat(ctx, format, values...)
|
||||
}
|
||||
|
||||
// TranslateFormat translates, formats and returns the <format> with configured language
|
||||
// and given <values>.
|
||||
// TranslateFormat translates, formats and returns the `format` with configured language
|
||||
// and given `values`.
|
||||
func (m *Manager) TranslateFormat(ctx context.Context, format string, values ...interface{}) string {
|
||||
return fmt.Sprintf(m.Translate(ctx, format), values...)
|
||||
}
|
||||
|
||||
// Translate translates <content> with configured language.
|
||||
// Translate translates `content` with configured language.
|
||||
func (m *Manager) Translate(ctx context.Context, content string) string {
|
||||
m.init(ctx)
|
||||
m.mu.RLock()
|
||||
|
@ -107,8 +107,8 @@ func GetArgAll() []string {
|
||||
// It returns the default value `def` if none of them exists.
|
||||
//
|
||||
// Fetching Rules:
|
||||
// 1. Command line arguments are in lowercase format, eg: gf.<package name>.<variable name>;
|
||||
// 2. Environment arguments are in uppercase format, eg: GF_<package name>_<variable name>;
|
||||
// 1. Command line arguments are in lowercase format, eg: gf.package.variable;
|
||||
// 2. Environment arguments are in uppercase format, eg: GF_PACKAGE_VARIABLE;
|
||||
func GetOptWithEnv(key string, def ...string) string {
|
||||
cmdKey := strings.ToLower(strings.Replace(key, "_", ".", -1))
|
||||
if ContainsOpt(cmdKey) {
|
||||
|
@ -108,7 +108,7 @@ func EqualFoldWithoutChars(s1, s2 string) bool {
|
||||
return strings.EqualFold(RemoveSymbols(s1), RemoveSymbols(s2))
|
||||
}
|
||||
|
||||
// SplitAndTrim splits string <str> by a string <delimiter> to an array,
|
||||
// SplitAndTrim splits string `str` by a string `delimiter` to an array,
|
||||
// and calls Trim to every element of this array. It ignores the elements
|
||||
// which are empty after Trim.
|
||||
func SplitAndTrim(str, delimiter string, characterMask ...string) []string {
|
||||
@ -123,7 +123,7 @@ func SplitAndTrim(str, delimiter string, characterMask ...string) []string {
|
||||
}
|
||||
|
||||
// Trim strips whitespace (or other characters) from the beginning and end of a string.
|
||||
// The optional parameter <characterMask> specifies the additional stripped characters.
|
||||
// The optional parameter `characterMask` specifies the additional stripped characters.
|
||||
func Trim(str string, characterMask ...string) string {
|
||||
trimChars := DefaultTrimChars
|
||||
if len(characterMask) > 0 {
|
||||
@ -131,3 +131,13 @@ func Trim(str string, characterMask ...string) string {
|
||||
}
|
||||
return strings.Trim(str, trimChars)
|
||||
}
|
||||
|
||||
// FormatCmdKey formats string `s` as command key using uniformed format.
|
||||
func FormatCmdKey(s string) string {
|
||||
return strings.ToLower(strings.Replace(s, "_", ".", -1))
|
||||
}
|
||||
|
||||
// FormatEnvKey formats string `s` as environment key using uniformed format.
|
||||
func FormatEnvKey(s string) string {
|
||||
return strings.ToUpper(strings.Replace(s, ".", "_", -1))
|
||||
}
|
||||
|
@ -12,15 +12,15 @@ import (
|
||||
"github.com/gogf/gf/v2/net/ghttp/internal/httputil"
|
||||
)
|
||||
|
||||
// BuildParams builds the request string for the http client. The <params> can be type of:
|
||||
// BuildParams builds the request string for the http client. The `params` can be type of:
|
||||
// string/[]byte/map/struct/*struct.
|
||||
//
|
||||
// The optional parameter <noUrlEncode> specifies whether ignore the url encoding for the data.
|
||||
// The optional parameter `noUrlEncode` specifies whether ignore the url encoding for the data.
|
||||
func BuildParams(params interface{}, noUrlEncode ...bool) (encodedParamStr string) {
|
||||
return httputil.BuildParams(params, noUrlEncode...)
|
||||
}
|
||||
|
||||
// niceCallFunc calls function <f> with exception capture logic.
|
||||
// niceCallFunc calls function `f` with exception capture logic.
|
||||
func niceCallFunc(f func()) {
|
||||
defer func() {
|
||||
if exception := recover(); exception != nil {
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
type DefaultHandlerResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
// MiddlewareHandlerResponse is the default middleware handling handler response object and its error.
|
||||
|
@ -154,7 +154,7 @@ func (r *Request) IsExited() bool {
|
||||
return r.exit
|
||||
}
|
||||
|
||||
// GetHeader retrieves and returns the header value with given <key>.
|
||||
// GetHeader retrieves and returns the header value with given `key`.
|
||||
func (r *Request) GetHeader(key string) string {
|
||||
return r.Header.Get(key)
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ var (
|
||||
// slice. It also automatically validates the struct or every element of the struct slice according
|
||||
// to the validation tag of the struct.
|
||||
//
|
||||
// The parameter <pointer> can be type of: *struct/**struct/*[]struct/*[]*struct.
|
||||
// The parameter `pointer` can be type of: *struct/**struct/*[]struct/*[]*struct.
|
||||
//
|
||||
// It supports single and multiple struct convertion:
|
||||
// 1. Single struct, post content like: {"id":1, "name":"john"} or ?id=1&name=john
|
||||
|
@ -44,7 +44,7 @@ func (r *Request) SetCtx(ctx context.Context) {
|
||||
}
|
||||
|
||||
// GetCtxVar retrieves and returns a Var with given key name.
|
||||
// The optional parameter <def> specifies the default value of the Var if given <key>
|
||||
// The optional parameter `def` specifies the default value of the Var if given `key`
|
||||
// does not exist in the context.
|
||||
func (r *Request) GetCtxVar(key interface{}, def ...interface{}) *gvar.Var {
|
||||
value := r.Context().Value(key)
|
||||
|
@ -31,7 +31,7 @@ type UploadFiles []*UploadFile
|
||||
|
||||
// Save saves the single uploading file to directory path and returns the saved file name.
|
||||
//
|
||||
// The parameter <dirPath> should be a directory path or it returns error.
|
||||
// The parameter `dirPath` should be a directory path or it returns error.
|
||||
//
|
||||
// Note that it will OVERWRITE the target file if there's already a same name file exist.
|
||||
func (f *UploadFile) Save(dirPath string, randomlyRename ...bool) (filename string, err error) {
|
||||
@ -75,9 +75,9 @@ func (f *UploadFile) Save(dirPath string, randomlyRename ...bool) (filename stri
|
||||
|
||||
// Save saves all uploading files to specified directory path and returns the saved file names.
|
||||
//
|
||||
// The parameter <dirPath> should be a directory path or it returns error.
|
||||
// The parameter `dirPath` should be a directory path or it returns error.
|
||||
//
|
||||
// The parameter <randomlyRename> specifies whether randomly renames all the file names.
|
||||
// The parameter `randomlyRename` specifies whether randomly renames all the file names.
|
||||
func (fs UploadFiles) Save(dirPath string, randomlyRename ...bool) (filenames []string, err error) {
|
||||
if len(fs) == 0 {
|
||||
return nil, gerror.NewCode(
|
||||
@ -101,7 +101,7 @@ func (fs UploadFiles) Save(dirPath string, randomlyRename ...bool) (filenames []
|
||||
//
|
||||
// It returns nil if retrieving failed or no form file with given name posted.
|
||||
//
|
||||
// Note that the <name> is the file field name of the multipart form from client.
|
||||
// Note that the `name` is the file field name of the multipart form from client.
|
||||
func (r *Request) GetUploadFile(name string) *UploadFile {
|
||||
uploadFiles := r.GetUploadFiles(name)
|
||||
if len(uploadFiles) > 0 {
|
||||
@ -116,7 +116,7 @@ func (r *Request) GetUploadFile(name string) *UploadFile {
|
||||
//
|
||||
// It returns nil if retrieving failed or no form file with given name posted.
|
||||
//
|
||||
// Note that the <name> is the file field name of the multipart form from client.
|
||||
// Note that the `name` is the file field name of the multipart form from client.
|
||||
func (r *Request) GetUploadFiles(name string) UploadFiles {
|
||||
multipartFiles := r.GetMultipartFiles(name)
|
||||
if len(multipartFiles) > 0 {
|
||||
|
@ -20,8 +20,8 @@ func (r *Request) SetForm(key string, value interface{}) {
|
||||
r.formMap[key] = value
|
||||
}
|
||||
|
||||
// GetForm retrieves and returns parameter <key> from form.
|
||||
// It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil.
|
||||
// GetForm retrieves and returns parameter `key` from form.
|
||||
// It returns `def` if `key` does not exist in the form and `def` is given, or else it returns nil.
|
||||
func (r *Request) GetForm(key string, def ...interface{}) *gvar.Var {
|
||||
r.parseForm()
|
||||
if len(r.formMap) > 0 {
|
||||
@ -36,7 +36,7 @@ func (r *Request) GetForm(key string, def ...interface{}) *gvar.Var {
|
||||
}
|
||||
|
||||
// GetFormMap retrieves and returns all form parameters passed from client as map.
|
||||
// The parameter <kvMap> specifies the keys retrieving from client parameters,
|
||||
// The parameter `kvMap` specifies the keys retrieving from client parameters,
|
||||
// the associated values are the default values if the client does not pass.
|
||||
func (r *Request) GetFormMap(kvMap ...map[string]interface{}) map[string]interface{} {
|
||||
r.parseForm()
|
||||
@ -59,7 +59,7 @@ func (r *Request) GetFormMap(kvMap ...map[string]interface{}) map[string]interfa
|
||||
}
|
||||
|
||||
// GetFormMapStrStr retrieves and returns all form parameters passed from client as map[string]string.
|
||||
// The parameter <kvMap> specifies the keys retrieving from client parameters, the associated values
|
||||
// The parameter `kvMap` specifies the keys retrieving from client parameters, the associated values
|
||||
// are the default values if the client does not pass.
|
||||
func (r *Request) GetFormMapStrStr(kvMap ...map[string]interface{}) map[string]string {
|
||||
formMap := r.GetFormMap(kvMap...)
|
||||
@ -74,7 +74,7 @@ func (r *Request) GetFormMapStrStr(kvMap ...map[string]interface{}) map[string]s
|
||||
}
|
||||
|
||||
// GetFormMapStrVar retrieves and returns all form parameters passed from client as map[string]*gvar.Var.
|
||||
// The parameter <kvMap> specifies the keys retrieving from client parameters, the associated values
|
||||
// The parameter `kvMap` specifies the keys retrieving from client parameters, the associated values
|
||||
// are the default values if the client does not pass.
|
||||
func (r *Request) GetFormMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
|
||||
formMap := r.GetFormMap(kvMap...)
|
||||
@ -89,8 +89,8 @@ func (r *Request) GetFormMapStrVar(kvMap ...map[string]interface{}) map[string]*
|
||||
}
|
||||
|
||||
// GetFormStruct retrieves all form parameters passed from client and converts them to
|
||||
// given struct object. Note that the parameter <pointer> is a pointer to the struct object.
|
||||
// The optional parameter <mapping> is used to specify the key to attribute mapping.
|
||||
// given struct object. Note that the parameter `pointer` is a pointer to the struct object.
|
||||
// The optional parameter `mapping` is used to specify the key to attribute mapping.
|
||||
func (r *Request) GetFormStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
_, err := r.doGetFormStruct(pointer, mapping...)
|
||||
return err
|
||||
|
@ -13,7 +13,7 @@ import (
|
||||
"github.com/gogf/gf/v2/util/gpage"
|
||||
)
|
||||
|
||||
// GetPage creates and returns the pagination object for given <totalSize> and <pageSize>.
|
||||
// GetPage creates and returns the pagination object for given `totalSize` and `pageSize`.
|
||||
// NOTE THAT the page parameter name from client is constantly defined as gpage.DefaultPageName
|
||||
// for simplification and convenience.
|
||||
func (r *Request) GetPage(totalSize, pageSize int) *gpage.Page {
|
||||
|
@ -16,9 +16,9 @@ func (r *Request) SetParam(key string, value interface{}) {
|
||||
r.paramsMap[key] = value
|
||||
}
|
||||
|
||||
// GetParam returns custom parameter with given name <key>.
|
||||
// It returns <def> if <key> does not exist.
|
||||
// It returns nil if <def> is not passed.
|
||||
// GetParam returns custom parameter with given name `key`.
|
||||
// It returns `def` if `key` does not exist.
|
||||
// It returns nil if `def` is not passed.
|
||||
func (r *Request) GetParam(key string, def ...interface{}) *gvar.Var {
|
||||
if r.paramsMap != nil {
|
||||
return gvar.New(r.paramsMap[key])
|
||||
|
@ -21,8 +21,8 @@ func (r *Request) SetQuery(key string, value interface{}) {
|
||||
r.queryMap[key] = value
|
||||
}
|
||||
|
||||
// GetQuery retrieves and returns parameter with given name <key> from query string
|
||||
// and request body. It returns <def> if <key> does not exist in the query and <def> is given,
|
||||
// GetQuery retrieves and returns parameter with given name `key` from query string
|
||||
// and request body. It returns `def` if `key` does not exist in the query and `def` is given,
|
||||
// or else it returns nil.
|
||||
//
|
||||
// Note that if there are multiple parameters with the same name, the parameters are retrieved
|
||||
@ -49,7 +49,7 @@ func (r *Request) GetQuery(key string, def ...interface{}) *gvar.Var {
|
||||
}
|
||||
|
||||
// GetQueryMap retrieves and returns all parameters passed from client using HTTP GET method
|
||||
// as map. The parameter <kvMap> specifies the keys retrieving from client parameters,
|
||||
// as map. The parameter `kvMap` specifies the keys retrieving from client parameters,
|
||||
// the associated values are the default values if the client does not pass.
|
||||
//
|
||||
// Note that if there are multiple parameters with the same name, the parameters are retrieved and overwrote
|
||||
@ -96,7 +96,7 @@ func (r *Request) GetQueryMap(kvMap ...map[string]interface{}) map[string]interf
|
||||
}
|
||||
|
||||
// GetQueryMapStrStr retrieves and returns all parameters passed from client using HTTP GET method
|
||||
// as map[string]string. The parameter <kvMap> specifies the keys
|
||||
// as map[string]string. The parameter `kvMap` specifies the keys
|
||||
// retrieving from client parameters, the associated values are the default values if the client
|
||||
// does not pass.
|
||||
func (r *Request) GetQueryMapStrStr(kvMap ...map[string]interface{}) map[string]string {
|
||||
@ -112,7 +112,7 @@ func (r *Request) GetQueryMapStrStr(kvMap ...map[string]interface{}) map[string]
|
||||
}
|
||||
|
||||
// GetQueryMapStrVar retrieves and returns all parameters passed from client using HTTP GET method
|
||||
// as map[string]*gvar.Var. The parameter <kvMap> specifies the keys
|
||||
// as map[string]*gvar.Var. The parameter `kvMap` specifies the keys
|
||||
// retrieving from client parameters, the associated values are the default values if the client
|
||||
// does not pass.
|
||||
func (r *Request) GetQueryMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
|
||||
@ -128,8 +128,8 @@ func (r *Request) GetQueryMapStrVar(kvMap ...map[string]interface{}) map[string]
|
||||
}
|
||||
|
||||
// GetQueryStruct retrieves all parameters passed from client using HTTP GET method
|
||||
// and converts them to given struct object. Note that the parameter <pointer> is a pointer
|
||||
// to the struct object. The optional parameter <mapping> is used to specify the key to
|
||||
// and converts them to given struct object. Note that the parameter `pointer` is a pointer
|
||||
// to the struct object. The optional parameter `mapping` is used to specify the key to
|
||||
// attribute mapping.
|
||||
func (r *Request) GetQueryStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
_, err := r.doGetQueryStruct(pointer, mapping...)
|
||||
|
@ -14,9 +14,9 @@ import (
|
||||
"github.com/gogf/gf/v2/util/gutil"
|
||||
)
|
||||
|
||||
// GetRequest retrieves and returns the parameter named <key> passed from client and
|
||||
// GetRequest retrieves and returns the parameter named `key` passed from client and
|
||||
// custom params as interface{}, no matter what HTTP method the client is using. The
|
||||
// parameter <def> specifies the default value if the <key> does not exist.
|
||||
// parameter `def` specifies the default value if the `key` does not exist.
|
||||
//
|
||||
// GetRequest is one of the most commonly used functions for retrieving parameters.
|
||||
//
|
||||
@ -51,7 +51,7 @@ func (r *Request) GetRequest(key string, def ...interface{}) *gvar.Var {
|
||||
}
|
||||
|
||||
// GetRequestMap retrieves and returns all parameters passed from client and custom params
|
||||
// as map, no matter what HTTP method the client is using. The parameter <kvMap> specifies
|
||||
// as map, no matter what HTTP method the client is using. The parameter `kvMap` specifies
|
||||
// the keys retrieving from client parameters, the associated values are the default values
|
||||
// if the client does not pass the according keys.
|
||||
//
|
||||
@ -127,7 +127,7 @@ func (r *Request) GetRequestMap(kvMap ...map[string]interface{}) map[string]inte
|
||||
|
||||
// GetRequestMapStrStr retrieves and returns all parameters passed from client and custom
|
||||
// params as map[string]string, no matter what HTTP method the client is using. The parameter
|
||||
// <kvMap> specifies the keys retrieving from client parameters, the associated values are the
|
||||
// `kvMap` specifies the keys retrieving from client parameters, the associated values are the
|
||||
// default values if the client does not pass.
|
||||
func (r *Request) GetRequestMapStrStr(kvMap ...map[string]interface{}) map[string]string {
|
||||
requestMap := r.GetRequestMap(kvMap...)
|
||||
@ -143,7 +143,7 @@ func (r *Request) GetRequestMapStrStr(kvMap ...map[string]interface{}) map[strin
|
||||
|
||||
// GetRequestMapStrVar retrieves and returns all parameters passed from client and custom
|
||||
// params as map[string]*gvar.Var, no matter what HTTP method the client is using. The parameter
|
||||
// <kvMap> specifies the keys retrieving from client parameters, the associated values are the
|
||||
// `kvMap` specifies the keys retrieving from client parameters, the associated values are the
|
||||
// default values if the client does not pass.
|
||||
func (r *Request) GetRequestMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
|
||||
requestMap := r.GetRequestMap(kvMap...)
|
||||
@ -159,8 +159,8 @@ func (r *Request) GetRequestMapStrVar(kvMap ...map[string]interface{}) map[strin
|
||||
|
||||
// GetRequestStruct retrieves all parameters passed from client and custom params no matter
|
||||
// what HTTP method the client is using, and converts them to given struct object. Note that
|
||||
// the parameter <pointer> is a pointer to the struct object.
|
||||
// The optional parameter <mapping> is used to specify the key to attribute mapping.
|
||||
// the parameter `pointer` is a pointer to the struct object.
|
||||
// The optional parameter `mapping` is used to specify the key to attribute mapping.
|
||||
func (r *Request) GetRequestStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
_, err := r.doGetRequestStruct(pointer, mapping...)
|
||||
return err
|
||||
|
@ -20,8 +20,8 @@ func (r *Request) GetRouterMap() map[string]string {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetRouter retrieves and returns the router value with given key name <key>.
|
||||
// It returns <def> if <key> does not exist.
|
||||
// GetRouter retrieves and returns the router value with given key name `key`.
|
||||
// It returns `def` if `key` does not exist.
|
||||
func (r *Request) GetRouter(key string, def ...interface{}) *gvar.Var {
|
||||
if r.routerMap != nil {
|
||||
if v, ok := r.routerMap[key]; ok {
|
||||
|
@ -95,7 +95,7 @@ func (r *Response) ServeFileDownload(path string, name ...string) {
|
||||
}
|
||||
|
||||
// RedirectTo redirects client to another location.
|
||||
// The optional parameter <code> specifies the http status code for redirecting,
|
||||
// The optional parameter `code` specifies the http status code for redirecting,
|
||||
// which commonly can be 301 or 302. It's 302 in default.
|
||||
func (r *Response) RedirectTo(location string, code ...int) {
|
||||
r.Header().Set("Location", location)
|
||||
@ -108,7 +108,7 @@ func (r *Response) RedirectTo(location string, code ...int) {
|
||||
}
|
||||
|
||||
// RedirectBack redirects client back to referer.
|
||||
// The optional parameter <code> specifies the http status code for redirecting,
|
||||
// The optional parameter `code` specifies the http status code for redirecting,
|
||||
// which commonly can be 301 or 302. It's 302 in default.
|
||||
func (r *Response) RedirectBack(code ...int) {
|
||||
r.RedirectTo(r.Request.GetReferer(), code...)
|
||||
@ -129,7 +129,7 @@ func (r *Response) BufferLength() int {
|
||||
return r.buffer.Len()
|
||||
}
|
||||
|
||||
// SetBuffer overwrites the buffer with <data>.
|
||||
// SetBuffer overwrites the buffer with `data`.
|
||||
func (r *Response) SetBuffer(data []byte) {
|
||||
r.buffer.Reset()
|
||||
r.buffer.Write(data)
|
||||
|
@ -16,7 +16,7 @@ import (
|
||||
)
|
||||
|
||||
// WriteTpl parses and responses given template file.
|
||||
// The parameter <params> specifies the template variables for parsing.
|
||||
// The parameter `params` specifies the template variables for parsing.
|
||||
func (r *Response) WriteTpl(tpl string, params ...gview.Params) error {
|
||||
if b, err := r.ParseTpl(tpl, params...); err != nil {
|
||||
if !gmode.IsProduct() {
|
||||
@ -30,7 +30,7 @@ func (r *Response) WriteTpl(tpl string, params ...gview.Params) error {
|
||||
}
|
||||
|
||||
// WriteTplDefault parses and responses the default template file.
|
||||
// The parameter <params> specifies the template variables for parsing.
|
||||
// The parameter `params` specifies the template variables for parsing.
|
||||
func (r *Response) WriteTplDefault(params ...gview.Params) error {
|
||||
if b, err := r.ParseTplDefault(params...); err != nil {
|
||||
if !gmode.IsProduct() {
|
||||
@ -44,7 +44,7 @@ func (r *Response) WriteTplDefault(params ...gview.Params) error {
|
||||
}
|
||||
|
||||
// WriteTplContent parses and responses the template content.
|
||||
// The parameter <params> specifies the template variables for parsing.
|
||||
// The parameter `params` specifies the template variables for parsing.
|
||||
func (r *Response) WriteTplContent(content string, params ...gview.Params) error {
|
||||
if b, err := r.ParseTplContent(content, params...); err != nil {
|
||||
if !gmode.IsProduct() {
|
||||
@ -57,7 +57,7 @@ func (r *Response) WriteTplContent(content string, params ...gview.Params) error
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseTpl parses given template file <tpl> with given template variables <params>
|
||||
// ParseTpl parses given template file `tpl` with given template variables `params`
|
||||
// and returns the parsed template content.
|
||||
func (r *Response) ParseTpl(tpl string, params ...gview.Params) (string, error) {
|
||||
return r.Request.GetView().Parse(r.Request.Context(), tpl, r.buildInVars(params...))
|
||||
@ -68,13 +68,13 @@ func (r *Response) ParseTplDefault(params ...gview.Params) (string, error) {
|
||||
return r.Request.GetView().ParseDefault(r.Request.Context(), r.buildInVars(params...))
|
||||
}
|
||||
|
||||
// ParseTplContent parses given template file <file> with given template parameters <params>
|
||||
// ParseTplContent parses given template file `file` with given template parameters `params`
|
||||
// and returns the parsed template content.
|
||||
func (r *Response) ParseTplContent(content string, params ...gview.Params) (string, error) {
|
||||
return r.Request.GetView().ParseContent(r.Request.Context(), content, r.buildInVars(params...))
|
||||
}
|
||||
|
||||
// buildInVars merges build-in variables into <params> and returns the new template variables.
|
||||
// buildInVars merges build-in variables into `params` and returns the new template variables.
|
||||
// TODO performance improving.
|
||||
func (r *Response) buildInVars(params ...map[string]interface{}) map[string]interface{} {
|
||||
m := gutil.MapMergeCopy(r.Request.viewParams)
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Write writes <content> to the response buffer.
|
||||
// Write writes `content` to the response buffer.
|
||||
func (r *Response) Write(content ...interface{}) {
|
||||
if r.hijacked || len(content) == 0 {
|
||||
return
|
||||
@ -35,7 +35,7 @@ func (r *Response) Write(content ...interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// WriteExit writes <content> to the response buffer and exits executing of current handler.
|
||||
// WriteExit writes `content` to the response buffer and exits executing of current handler.
|
||||
// The "Exit" feature is commonly used to replace usage of return statement in the handler,
|
||||
// for convenience.
|
||||
func (r *Response) WriteExit(content ...interface{}) {
|
||||
@ -43,13 +43,13 @@ func (r *Response) WriteExit(content ...interface{}) {
|
||||
r.Request.Exit()
|
||||
}
|
||||
|
||||
// WriteOver overwrites the response buffer with <content>.
|
||||
// WriteOver overwrites the response buffer with `content`.
|
||||
func (r *Response) WriteOver(content ...interface{}) {
|
||||
r.ClearBuffer()
|
||||
r.Write(content...)
|
||||
}
|
||||
|
||||
// WriteOverExit overwrites the response buffer with <content> and exits executing
|
||||
// WriteOverExit overwrites the response buffer with `content` and exits executing
|
||||
// of current handler. The "Exit" feature is commonly used to replace usage of return
|
||||
// statement in the handler, for convenience.
|
||||
func (r *Response) WriteOverExit(content ...interface{}) {
|
||||
@ -70,7 +70,7 @@ func (r *Response) WritefExit(format string, params ...interface{}) {
|
||||
r.Request.Exit()
|
||||
}
|
||||
|
||||
// Writeln writes the response with <content> and new line.
|
||||
// Writeln writes the response with `content` and new line.
|
||||
func (r *Response) Writeln(content ...interface{}) {
|
||||
if len(content) == 0 {
|
||||
r.Write("\n")
|
||||
@ -79,7 +79,7 @@ func (r *Response) Writeln(content ...interface{}) {
|
||||
r.Write(append(content, "\n")...)
|
||||
}
|
||||
|
||||
// WritelnExit writes the response with <content> and new line and exits executing
|
||||
// WritelnExit writes the response with `content` and new line and exits executing
|
||||
// of current handler. The "Exit" feature is commonly used to replace usage of return
|
||||
// statement in the handler, for convenience.
|
||||
func (r *Response) WritelnExit(content ...interface{}) {
|
||||
@ -100,7 +100,7 @@ func (r *Response) WriteflnExit(format string, params ...interface{}) {
|
||||
r.Request.Exit()
|
||||
}
|
||||
|
||||
// WriteJson writes <content> to the response with JSON format.
|
||||
// WriteJson writes `content` to the response with JSON format.
|
||||
func (r *Response) WriteJson(content interface{}) error {
|
||||
// If given string/[]byte, response it directly to client.
|
||||
switch content.(type) {
|
||||
@ -119,7 +119,7 @@ func (r *Response) WriteJson(content interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteJsonExit writes <content> to the response with JSON format and exits executing
|
||||
// WriteJsonExit writes `content` to the response with JSON format and exits executing
|
||||
// of current handler if success. The "Exit" feature is commonly used to replace usage of
|
||||
// return statement in the handler, for convenience.
|
||||
func (r *Response) WriteJsonExit(content interface{}) error {
|
||||
@ -130,7 +130,7 @@ func (r *Response) WriteJsonExit(content interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteJson writes <content> to the response with JSONP format.
|
||||
// WriteJson writes `content` to the response with JSONP format.
|
||||
//
|
||||
// Note that there should be a "callback" parameter in the request for JSONP format.
|
||||
func (r *Response) WriteJsonP(content interface{}) error {
|
||||
@ -159,7 +159,7 @@ func (r *Response) WriteJsonP(content interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteJsonPExit writes <content> to the response with JSONP format and exits executing
|
||||
// WriteJsonPExit writes `content` to the response with JSONP format and exits executing
|
||||
// of current handler if success. The "Exit" feature is commonly used to replace usage of
|
||||
// return statement in the handler, for convenience.
|
||||
//
|
||||
@ -172,7 +172,7 @@ func (r *Response) WriteJsonPExit(content interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteXml writes <content> to the response with XML format.
|
||||
// WriteXml writes `content` to the response with XML format.
|
||||
func (r *Response) WriteXml(content interface{}, rootTag ...string) error {
|
||||
// If given string/[]byte, response it directly to client.
|
||||
switch content.(type) {
|
||||
@ -190,7 +190,7 @@ func (r *Response) WriteXml(content interface{}, rootTag ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteXmlExit writes <content> to the response with XML format and exits executing
|
||||
// WriteXmlExit writes `content` to the response with XML format and exits executing
|
||||
// of current handler if success. The "Exit" feature is commonly used to replace usage
|
||||
// of return statement in the handler, for convenience.
|
||||
func (r *Response) WriteXmlExit(content interface{}, rootTag ...string) error {
|
||||
@ -201,7 +201,7 @@ func (r *Response) WriteXmlExit(content interface{}, rootTag ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteStatus writes HTTP <status> and <content> to the response.
|
||||
// WriteStatus writes HTTP `status` and `content` to the response.
|
||||
// Note that do not set Content-Type header here.
|
||||
func (r *Response) WriteStatus(status int, content ...interface{}) {
|
||||
r.WriteHeader(status)
|
||||
@ -212,7 +212,7 @@ func (r *Response) WriteStatus(status int, content ...interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// WriteStatusExit writes HTTP <status> and <content> to the response and exits executing
|
||||
// WriteStatusExit writes HTTP `status` and `content` to the response and exits executing
|
||||
// of current handler if success. The "Exit" feature is commonly used to replace usage of
|
||||
// return statement in the handler, for convenience.
|
||||
func (r *Response) WriteStatusExit(status int, content ...interface{}) {
|
||||
|
@ -55,7 +55,7 @@ func serverProcessInit() {
|
||||
}
|
||||
// This means it is a restart server, it should kill its parent before starting its listening,
|
||||
// to avoid duplicated port listening in two processes.
|
||||
if genv.Get(adminActionRestartEnvKey) != "" {
|
||||
if !genv.Get(adminActionRestartEnvKey).IsEmpty() {
|
||||
if p, err := os.FindProcess(gproc.PPid()); err == nil {
|
||||
if err = p.Kill(); err != nil {
|
||||
intlog.Error(ctx, err)
|
||||
@ -87,8 +87,8 @@ func serverProcessInit() {
|
||||
}
|
||||
|
||||
// GetServer creates and returns a server instance using given name and default configurations.
|
||||
// Note that the parameter <name> should be unique for different servers. It returns an existing
|
||||
// server instance if given <name> is already existing in the server mapping.
|
||||
// Note that the parameter `name` should be unique for different servers. It returns an existing
|
||||
// server instance if given `name` is already existing in the server mapping.
|
||||
func GetServer(name ...interface{}) *Server {
|
||||
serverName := DefaultServerName
|
||||
if len(name) > 0 && name[0] != "" {
|
||||
@ -226,7 +226,7 @@ func (s *Server) Start() error {
|
||||
|
||||
// Start the HTTP server.
|
||||
reloaded := false
|
||||
fdMapStr := genv.Get(adminActionReloadEnvKey)
|
||||
fdMapStr := genv.Get(adminActionReloadEnvKey).String()
|
||||
if len(fdMapStr) > 0 {
|
||||
sfm := bufferToServerFdMap([]byte(fdMapStr))
|
||||
if v, ok := sfm[s.name]; ok {
|
||||
|
@ -77,7 +77,7 @@ func (p *utilAdmin) Shutdown(r *Request) {
|
||||
}
|
||||
|
||||
// EnableAdmin enables the administration feature for the process.
|
||||
// The optional parameter <pattern> specifies the URI for the administration page.
|
||||
// The optional parameter `pattern` specifies the URI for the administration page.
|
||||
func (s *Server) EnableAdmin(pattern ...string) {
|
||||
p := "/debug/admin"
|
||||
if len(pattern) > 0 {
|
||||
|
@ -52,7 +52,7 @@ var (
|
||||
)
|
||||
|
||||
// RestartAllServer restarts all the servers of the process.
|
||||
// The optional parameter <newExeFilePath> specifies the new binary file for creating process.
|
||||
// The optional parameter `newExeFilePath` specifies the new binary file for creating process.
|
||||
func RestartAllServer(ctx context.Context, newExeFilePath ...string) error {
|
||||
if !gracefulEnabled {
|
||||
return gerror.NewCode(gcode.CodeInvalidOperation, "graceful reload feature is disabled")
|
||||
|
@ -383,7 +383,7 @@ func (s *Server) SetHTTPSPort(port ...int) {
|
||||
}
|
||||
|
||||
// EnableHTTPS enables HTTPS with given certification and key files for the server.
|
||||
// The optional parameter <tlsConfig> specifies custom TLS configuration.
|
||||
// The optional parameter `tlsConfig` specifies custom TLS configuration.
|
||||
func (s *Server) EnableHTTPS(certFile, keyFile string, tlsConfig ...*tls.Config) {
|
||||
var (
|
||||
ctx = context.TODO()
|
||||
|
@ -91,7 +91,7 @@ func (c *Cookie) Set(key, value string) {
|
||||
}
|
||||
|
||||
// SetCookie sets cookie item with given domain, path and expiration age.
|
||||
// The optional parameter <httpOnly> specifies if the cookie item is only available in HTTP,
|
||||
// The optional parameter `httpOnly` specifies if the cookie item is only available in HTTP,
|
||||
// which is usually empty.
|
||||
func (c *Cookie) SetCookie(key, value, domain, path string, maxAge time.Duration, httpOnly ...bool) {
|
||||
c.init()
|
||||
@ -139,7 +139,7 @@ func (c *Cookie) SetSessionId(id string) {
|
||||
}
|
||||
|
||||
// Get retrieves and returns the value with specified key.
|
||||
// It returns <def> if specified key does not exist and <def> is given.
|
||||
// It returns `def` if specified key does not exist and `def` is given.
|
||||
func (c *Cookie) Get(key string, def ...string) *gvar.Var {
|
||||
c.init()
|
||||
if r, ok := c.data[key]; ok {
|
||||
|
@ -33,7 +33,7 @@ type gracefulServer struct {
|
||||
}
|
||||
|
||||
// newGracefulServer creates and returns a graceful http server with given address.
|
||||
// The optional parameter <fd> specifies the file descriptor which is passed from parent server.
|
||||
// The optional parameter `fd` specifies the file descriptor which is passed from parent server.
|
||||
func (s *Server) newGracefulServer(address string, fd ...int) *gracefulServer {
|
||||
// Change port to address like: 80 -> :80
|
||||
if gstr.IsNumeric(address) {
|
||||
@ -94,8 +94,8 @@ func (s *gracefulServer) setFd(fd int) {
|
||||
}
|
||||
|
||||
// ListenAndServeTLS starts listening on configured address with HTTPS.
|
||||
// The parameter <certFile> and <keyFile> specify the necessary certification and key files for HTTPS.
|
||||
// The optional parameter <tlsConfig> specifies the custom TLS configuration.
|
||||
// The parameter `certFile` and `keyFile` specify the necessary certification and key files for HTTPS.
|
||||
// The optional parameter `tlsConfig` specifies the custom TLS configuration.
|
||||
func (s *gracefulServer) ListenAndServeTLS(certFile, keyFile string, tlsConfig ...*tls.Config) error {
|
||||
var (
|
||||
ctx = context.TODO()
|
||||
|
@ -250,7 +250,7 @@ func (s *Server) searchStaticFile(uri string) *staticFile {
|
||||
}
|
||||
|
||||
// serveFile serves the static file for client.
|
||||
// The optional parameter <allowIndex> specifies if allowing directory listing if <f> is directory.
|
||||
// The optional parameter `allowIndex` specifies if allowing directory listing if `f` is directory.
|
||||
func (s *Server) serveFile(r *Request, f *staticFile, allowIndex ...bool) {
|
||||
// Use resource file from memory.
|
||||
if f.File != nil {
|
||||
|
@ -169,7 +169,7 @@ func (s *Server) setHandler(ctx context.Context, pattern string, handler *handle
|
||||
}
|
||||
}
|
||||
}
|
||||
// It iterates the list array of <lists>, compares priorities and inserts the new router item in
|
||||
// It iterates the list array of `lists`, compares priorities and inserts the new router item in
|
||||
// the proper position of each list. The priority of the list is ordered from high to low.
|
||||
item := (*handlerItem)(nil)
|
||||
for _, l := range lists {
|
||||
@ -209,8 +209,8 @@ func (s *Server) setHandler(ctx context.Context, pattern string, handler *handle
|
||||
}
|
||||
}
|
||||
|
||||
// compareRouterPriority compares the priority between <newItem> and <oldItem>. It returns true
|
||||
// if <newItem>'s priority is higher than <oldItem>, else it returns false. The higher priority
|
||||
// compareRouterPriority compares the priority between `newItem` and `oldItem`. It returns true
|
||||
// if `newItem`'s priority is higher than `oldItem`, else it returns false. The higher priority
|
||||
// item will be insert into the router list before the other one.
|
||||
//
|
||||
// Comparison rules:
|
||||
|
@ -19,7 +19,7 @@ const (
|
||||
|
||||
// BindMiddleware registers one or more global middleware to the server.
|
||||
// Global middleware can be used standalone without service handler, which intercepts all dynamic requests
|
||||
// before or after service handler. The parameter <pattern> specifies what route pattern the middleware intercepts,
|
||||
// before or after service handler. The parameter `pattern` specifies what route pattern the middleware intercepts,
|
||||
// which is usually a "fuzzy" pattern like "/:name", "/*any" or "/{field}".
|
||||
func (s *Server) BindMiddleware(pattern string, handlers ...HandlerFunc) {
|
||||
var (
|
||||
|
@ -34,7 +34,7 @@ func (s *Server) BindHandler(pattern string, handler interface{}) {
|
||||
}
|
||||
|
||||
// doBindHandler registers a handler function to server with given pattern.
|
||||
// The parameter <pattern> is like:
|
||||
// The parameter `pattern` is like:
|
||||
// /user/list, put:/user, delete:/user, post:/user@goframe.org
|
||||
func (s *Server) doBindHandler(ctx context.Context, pattern string, funcInfo handlerFuncInfo, middleware []HandlerFunc, source string) {
|
||||
s.setHandler(ctx, pattern, &handlerItem{
|
||||
@ -59,7 +59,7 @@ func (s *Server) bindHandlerByMap(ctx context.Context, m map[string]*handlerItem
|
||||
// Rule 2: The URI in pattern contains the {.method} keyword, it then replaces the keyword with the method name;
|
||||
// Rule 2: If Rule 1 is not met, it then adds the method name directly to the URI in the pattern;
|
||||
//
|
||||
// The parameter <allowAppend> specifies whether allowing appending method name to the tail of pattern.
|
||||
// The parameter `allowAppend` specifies whether allowing appending method name to the tail of pattern.
|
||||
func (s *Server) mergeBuildInNameToPattern(pattern string, structName, methodName string, allowAppend bool) string {
|
||||
structName = s.nameToUri(structName)
|
||||
methodName = s.nameToUri(methodName)
|
||||
|
@ -19,7 +19,7 @@ import (
|
||||
|
||||
// BindObject registers object to server routes with given pattern.
|
||||
//
|
||||
// The optional parameter <method> is used to specify the method to be registered, which
|
||||
// The optional parameter `method` is used to specify the method to be registered, which
|
||||
// supports multiple method names, multiple methods are separated by char ',', case-sensitive.
|
||||
//
|
||||
// Note that the route method should be defined as ghttp.HandlerFunc.
|
||||
@ -35,7 +35,7 @@ func (s *Server) BindObject(pattern string, object interface{}, method ...string
|
||||
|
||||
// BindObjectMethod registers specified method of object to server routes with given pattern.
|
||||
//
|
||||
// The optional parameter <method> is used to specify the method to be registered, which
|
||||
// The optional parameter `method` is used to specify the method to be registered, which
|
||||
// does not supports multiple method names but only one, case-sensitive.
|
||||
//
|
||||
// Note that the route method should be defined as ghttp.HandlerFunc.
|
||||
|
@ -22,7 +22,7 @@ func (s *Server) getStatusHandler(status int, r *Request) []HandlerFunc {
|
||||
}
|
||||
|
||||
// addStatusHandler sets the handler for given status code.
|
||||
// The parameter <pattern> is like: domain#status
|
||||
// The parameter `pattern` is like: domain#status
|
||||
func (s *Server) addStatusHandler(pattern string, handler HandlerFunc) {
|
||||
if s.statusHandlerMap[pattern] == nil {
|
||||
s.statusHandlerMap[pattern] = make([]HandlerFunc, 0)
|
||||
|
@ -32,7 +32,7 @@ func (r *Response) initCookie() {
|
||||
}
|
||||
}
|
||||
|
||||
// GetCookie retrieves and returns the cookie value of specified <key>.
|
||||
// GetCookie retrieves and returns the cookie value of specified `key`.
|
||||
func (r *Response) GetCookie(key string) string {
|
||||
r.initCookie()
|
||||
return r.cookies[key]
|
||||
|
@ -12,62 +12,62 @@ import (
|
||||
)
|
||||
|
||||
// GetVar sends a GET request, retrieves and converts the result content to specified pointer.
|
||||
// The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
func (c *Client) GetVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
|
||||
return c.RequestVar(ctx, "GET", url, data...)
|
||||
}
|
||||
|
||||
// PutVar sends a PUT request, retrieves and converts the result content to specified pointer.
|
||||
// The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
func (c *Client) PutVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
|
||||
return c.RequestVar(ctx, "PUT", url, data...)
|
||||
}
|
||||
|
||||
// PostVar sends a POST request, retrieves and converts the result content to specified pointer.
|
||||
// The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
func (c *Client) PostVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
|
||||
return c.RequestVar(ctx, "POST", url, data...)
|
||||
}
|
||||
|
||||
// DeleteVar sends a DELETE request, retrieves and converts the result content to specified pointer.
|
||||
// The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
func (c *Client) DeleteVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
|
||||
return c.RequestVar(ctx, "DELETE", url, data...)
|
||||
}
|
||||
|
||||
// HeadVar sends a HEAD request, retrieves and converts the result content to specified pointer.
|
||||
// The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
func (c *Client) HeadVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
|
||||
return c.RequestVar(ctx, "HEAD", url, data...)
|
||||
}
|
||||
|
||||
// PatchVar sends a PATCH request, retrieves and converts the result content to specified pointer.
|
||||
// The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
func (c *Client) PatchVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
|
||||
return c.RequestVar(ctx, "PATCH", url, data...)
|
||||
}
|
||||
|
||||
// ConnectVar sends a CONNECT request, retrieves and converts the result content to specified pointer.
|
||||
// The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
func (c *Client) ConnectVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
|
||||
return c.RequestVar(ctx, "CONNECT", url, data...)
|
||||
}
|
||||
|
||||
// OptionsVar sends a OPTIONS request, retrieves and converts the result content to specified pointer.
|
||||
// The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
func (c *Client) OptionsVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
|
||||
return c.RequestVar(ctx, "OPTIONS", url, data...)
|
||||
}
|
||||
|
||||
// TraceVar sends a TRACE request, retrieves and converts the result content to specified pointer.
|
||||
// The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
func (c *Client) TraceVar(ctx context.Context, url string, data ...interface{}) *gvar.Var {
|
||||
return c.RequestVar(ctx, "TRACE", url, data...)
|
||||
}
|
||||
|
||||
// RequestVar sends request using given HTTP method and data, retrieves converts the result
|
||||
// to specified pointer. It reads and closes the response object internally automatically.
|
||||
// The parameter <pointer> can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
// The parameter `pointer` can be type of: struct/*struct/**struct/[]struct/[]*struct/*[]struct, etc.
|
||||
func (c *Client) RequestVar(ctx context.Context, method string, url string, data ...interface{}) *gvar.Var {
|
||||
response, err := c.DoRequest(ctx, method, url, data...)
|
||||
if err != nil {
|
||||
|
@ -19,10 +19,10 @@ const (
|
||||
fileUploadingKey = "@file:"
|
||||
)
|
||||
|
||||
// BuildParams builds the request string for the http client. The <params> can be type of:
|
||||
// BuildParams builds the request string for the http client. The `params` can be type of:
|
||||
// string/[]byte/map/struct/*struct.
|
||||
//
|
||||
// The optional parameter <noUrlEncode> specifies whether ignore the url encoding for the data.
|
||||
// The optional parameter `noUrlEncode` specifies whether ignore the url encoding for the data.
|
||||
func BuildParams(params interface{}, noUrlEncode ...bool) (encodedParamStr string) {
|
||||
// If given string/[]byte, converts and returns it directly as string.
|
||||
switch v := params.(type) {
|
||||
|
@ -32,12 +32,12 @@ func Long2ip(long uint32) string {
|
||||
return net.IP(ipByte).String()
|
||||
}
|
||||
|
||||
// Validate checks whether given <ip> a valid IPv4 address.
|
||||
// Validate checks whether given `ip` a valid IPv4 address.
|
||||
func Validate(ip string) bool {
|
||||
return gregex.IsMatchString(`^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$`, ip)
|
||||
}
|
||||
|
||||
// ParseAddress parses <address> to its ip and port.
|
||||
// ParseAddress parses `address` to its ip and port.
|
||||
// Eg: 192.168.1.1:80 -> 192.168.1.1, 80
|
||||
func ParseAddress(address string) (string, int) {
|
||||
match, err := gregex.MatchString(`^(.+):(\d+)$`, address)
|
||||
|
@ -9,7 +9,7 @@ package gipv6
|
||||
|
||||
import "github.com/gogf/gf/v2/text/gregex"
|
||||
|
||||
// Validate checks whether given <ip> a valid IPv6 address.
|
||||
// Validate checks whether given `ip` a valid IPv6 address.
|
||||
func Validate(ip string) bool {
|
||||
return gregex.IsMatchString(`^([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}$|^:((:[\da-fA-F]{1,4}){1,6}|:)$|^[\da-fA-F]{1,4}:((:[\da-fA-F]{1,4}){1,5}|:)$|^([\da-fA-F]{1,4}:){2}((:[\da-fA-F]{1,4}){1,4}|:)$|^([\da-fA-F]{1,4}:){3}((:[\da-fA-F]{1,4}){1,3}|:)$|^([\da-fA-F]{1,4}:){4}((:[\da-fA-F]{1,4}){1,2}|:)$|^([\da-fA-F]{1,4}:){5}:([\da-fA-F]{1,4})?$|^([\da-fA-F]{1,4}:){6}:$`, ip)
|
||||
}
|
||||
|
@ -43,10 +43,10 @@ var (
|
||||
|
||||
// SendMail connects to the server at addr, switches to TLS if
|
||||
// possible, authenticates with the optional mechanism an if possible,
|
||||
// and then sends an email from address <from>, to addresses <to>, with
|
||||
// and then sends an email from address `from`, to addresses `to`, with
|
||||
// message msg.
|
||||
//
|
||||
// The parameter <contentType> specifies the content type of the mail, eg: html.
|
||||
// The parameter `contentType` specifies the content type of the mail, eg: html.
|
||||
func (s *SMTP) SendMail(from, tos, subject, body string, contentType ...string) error {
|
||||
var (
|
||||
server = ""
|
||||
|
@ -127,7 +127,7 @@ func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error) {
|
||||
if size > 0 {
|
||||
index += size
|
||||
if length > 0 {
|
||||
// It reads til <length> size if <length> is specified.
|
||||
// It reads til `length` size if `length` is specified.
|
||||
if index == length {
|
||||
break
|
||||
}
|
||||
@ -201,8 +201,8 @@ func (c *Conn) RecvLine(retry ...Retry) ([]byte, error) {
|
||||
return data, err
|
||||
}
|
||||
|
||||
// RecvTil reads data from the connection until reads bytes <til>.
|
||||
// Note that the returned result contains the last bytes <til>.
|
||||
// RecvTil reads data from the connection until reads bytes `til`.
|
||||
// Note that the returned result contains the last bytes `til`.
|
||||
func (c *Conn) RecvTil(til []byte, retry ...Retry) ([]byte, error) {
|
||||
var err error
|
||||
var buffer []byte
|
||||
|
@ -27,7 +27,7 @@ type Retry struct {
|
||||
}
|
||||
|
||||
// NewNetConn creates and returns a net.Conn with given address like "127.0.0.1:80".
|
||||
// The optional parameter <timeout> specifies the timeout for dialing connection.
|
||||
// The optional parameter `timeout` specifies the timeout for dialing connection.
|
||||
func NewNetConn(addr string, timeout ...time.Duration) (net.Conn, error) {
|
||||
d := defaultConnTimeout
|
||||
if len(timeout) > 0 {
|
||||
@ -37,7 +37,7 @@ func NewNetConn(addr string, timeout ...time.Duration) (net.Conn, error) {
|
||||
}
|
||||
|
||||
// NewNetConnTLS creates and returns a TLS net.Conn with given address like "127.0.0.1:80".
|
||||
// The optional parameter <timeout> specifies the timeout for dialing connection.
|
||||
// The optional parameter `timeout` specifies the timeout for dialing connection.
|
||||
func NewNetConnTLS(addr string, tlsConfig *tls.Config, timeout ...time.Duration) (net.Conn, error) {
|
||||
dialer := &net.Dialer{
|
||||
Timeout: defaultConnTimeout,
|
||||
@ -49,7 +49,7 @@ func NewNetConnTLS(addr string, tlsConfig *tls.Config, timeout ...time.Duration)
|
||||
}
|
||||
|
||||
// NewNetConnKeyCrt creates and returns a TLS net.Conn with given TLS certificate and key files
|
||||
// and address like "127.0.0.1:80". The optional parameter <timeout> specifies the timeout for
|
||||
// and address like "127.0.0.1:80". The optional parameter `timeout` specifies the timeout for
|
||||
// dialing connection.
|
||||
func NewNetConnKeyCrt(addr, crtFile, keyFile string, timeout ...time.Duration) (net.Conn, error) {
|
||||
tlsConfig, err := LoadKeyCrt(crtFile, keyFile)
|
||||
@ -59,8 +59,8 @@ func NewNetConnKeyCrt(addr, crtFile, keyFile string, timeout ...time.Duration) (
|
||||
return NewNetConnTLS(addr, tlsConfig, timeout...)
|
||||
}
|
||||
|
||||
// Send creates connection to <address>, writes <data> to the connection and then closes the connection.
|
||||
// The optional parameter <retry> specifies the retry policy when fails in writing data.
|
||||
// Send creates connection to `address`, writes `data` to the connection and then closes the connection.
|
||||
// The optional parameter `retry` specifies the retry policy when fails in writing data.
|
||||
func Send(address string, data []byte, retry ...Retry) error {
|
||||
conn, err := NewConn(address)
|
||||
if err != nil {
|
||||
@ -70,13 +70,13 @@ func Send(address string, data []byte, retry ...Retry) error {
|
||||
return conn.Send(data, retry...)
|
||||
}
|
||||
|
||||
// SendRecv creates connection to <address>, writes <data> to the connection, receives response
|
||||
// SendRecv creates connection to `address`, writes `data` to the connection, receives response
|
||||
// and then closes the connection.
|
||||
//
|
||||
// The parameter <length> specifies the bytes count waiting to receive. It receives all buffer content
|
||||
// and returns if <length> is -1.
|
||||
// The parameter `length` specifies the bytes count waiting to receive. It receives all buffer content
|
||||
// and returns if `length` is -1.
|
||||
//
|
||||
// The optional parameter <retry> specifies the retry policy when fails in writing data.
|
||||
// The optional parameter `retry` specifies the retry policy when fails in writing data.
|
||||
func SendRecv(address string, data []byte, length int, retry ...Retry) ([]byte, error) {
|
||||
conn, err := NewConn(address)
|
||||
if err != nil {
|
||||
@ -106,7 +106,7 @@ func SendRecvWithTimeout(address string, data []byte, receive int, timeout time.
|
||||
return conn.SendRecvWithTimeout(data, receive, timeout, retry...)
|
||||
}
|
||||
|
||||
// isTimeout checks whether given <err> is a timeout error.
|
||||
// isTimeout checks whether given `err` is a timeout error.
|
||||
func isTimeout(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
|
@ -8,8 +8,8 @@ package gtcp
|
||||
|
||||
import "time"
|
||||
|
||||
// SendPkg sends a package containing <data> to <address> and closes the connection.
|
||||
// The optional parameter <option> specifies the package options for sending.
|
||||
// SendPkg sends a package containing `data` to `address` and closes the connection.
|
||||
// The optional parameter `option` specifies the package options for sending.
|
||||
func SendPkg(address string, data []byte, option ...PkgOption) error {
|
||||
conn, err := NewConn(address)
|
||||
if err != nil {
|
||||
@ -19,8 +19,8 @@ func SendPkg(address string, data []byte, option ...PkgOption) error {
|
||||
return conn.SendPkg(data, option...)
|
||||
}
|
||||
|
||||
// SendRecvPkg sends a package containing <data> to <address>, receives the response
|
||||
// and closes the connection. The optional parameter <option> specifies the package options for sending.
|
||||
// SendRecvPkg sends a package containing `data` to `address`, receives the response
|
||||
// and closes the connection. The optional parameter `option` specifies the package options for sending.
|
||||
func SendRecvPkg(address string, data []byte, option ...PkgOption) ([]byte, error) {
|
||||
conn, err := NewConn(address)
|
||||
if err != nil {
|
||||
@ -30,8 +30,8 @@ func SendRecvPkg(address string, data []byte, option ...PkgOption) ([]byte, erro
|
||||
return conn.SendRecvPkg(data, option...)
|
||||
}
|
||||
|
||||
// SendPkgWithTimeout sends a package containing <data> to <address> with timeout limitation
|
||||
// and closes the connection. The optional parameter <option> specifies the package options for sending.
|
||||
// SendPkgWithTimeout sends a package containing `data` to `address` with timeout limitation
|
||||
// and closes the connection. The optional parameter `option` specifies the package options for sending.
|
||||
func SendPkgWithTimeout(address string, data []byte, timeout time.Duration, option ...PkgOption) error {
|
||||
conn, err := NewConn(address)
|
||||
if err != nil {
|
||||
@ -41,8 +41,8 @@ func SendPkgWithTimeout(address string, data []byte, timeout time.Duration, opti
|
||||
return conn.SendPkgWithTimeout(data, timeout, option...)
|
||||
}
|
||||
|
||||
// SendRecvPkgWithTimeout sends a package containing <data> to <address>, receives the response with timeout limitation
|
||||
// and closes the connection. The optional parameter <option> specifies the package options for sending.
|
||||
// SendRecvPkgWithTimeout sends a package containing `data` to `address`, receives the response with timeout limitation
|
||||
// and closes the connection. The optional parameter `option` specifies the package options for sending.
|
||||
func SendRecvPkgWithTimeout(address string, data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error) {
|
||||
conn, err := NewConn(address)
|
||||
if err != nil {
|
||||
|
@ -56,7 +56,7 @@ func NewPoolConn(addr string, timeout ...time.Duration) (*PoolConn, error) {
|
||||
// Close puts back the connection to the pool if it's active,
|
||||
// or closes the connection if it's not active.
|
||||
//
|
||||
// Note that, if <c> calls Close function closing itself, <c> can not
|
||||
// Note that, if `c` calls Close function closing itself, `c` can not
|
||||
// be used again.
|
||||
func (c *PoolConn) Close() error {
|
||||
if c.pool != nil && c.status == connStatusActive {
|
||||
@ -111,8 +111,8 @@ func (c *PoolConn) RecvLine(retry ...Retry) ([]byte, error) {
|
||||
return data, err
|
||||
}
|
||||
|
||||
// RecvTil reads data from the connection until reads bytes <til>.
|
||||
// Note that the returned result contains the last bytes <til>.
|
||||
// RecvTil reads data from the connection until reads bytes `til`.
|
||||
// Note that the returned result contains the last bytes `til`.
|
||||
func (c *PoolConn) RecvTil(til []byte, retry ...Retry) ([]byte, error) {
|
||||
data, err := c.Conn.RecvTil(til, retry...)
|
||||
if err != nil {
|
||||
|
@ -10,8 +10,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// SendPkg sends a package containing <data> to the connection.
|
||||
// The optional parameter <option> specifies the package options for sending.
|
||||
// SendPkg sends a package containing `data` to the connection.
|
||||
// The optional parameter `option` specifies the package options for sending.
|
||||
func (c *PoolConn) SendPkg(data []byte, option ...PkgOption) (err error) {
|
||||
if err = c.Conn.SendPkg(data, option...); err != nil && c.status == connStatusUnknown {
|
||||
if v, e := c.pool.NewFunc(); e == nil {
|
||||
@ -30,7 +30,7 @@ func (c *PoolConn) SendPkg(data []byte, option ...PkgOption) (err error) {
|
||||
}
|
||||
|
||||
// RecvPkg receives package from connection using simple package protocol.
|
||||
// The optional parameter <option> specifies the package options for receiving.
|
||||
// The optional parameter `option` specifies the package options for receiving.
|
||||
func (c *PoolConn) RecvPkg(option ...PkgOption) ([]byte, error) {
|
||||
data, err := c.Conn.RecvPkg(option...)
|
||||
if err != nil {
|
||||
|
@ -36,9 +36,9 @@ type Server struct {
|
||||
// Map for name to server, for singleton purpose.
|
||||
var serverMapping = gmap.NewStrAnyMap(true)
|
||||
|
||||
// GetServer returns the TCP server with specified <name>,
|
||||
// or it returns a new normal TCP server named <name> if it does not exist.
|
||||
// The parameter <name> is used to specify the TCP server
|
||||
// GetServer returns the TCP server with specified `name`,
|
||||
// or it returns a new normal TCP server named `name` if it does not exist.
|
||||
// The parameter `name` is used to specify the TCP server
|
||||
func GetServer(name ...interface{}) *Server {
|
||||
serverName := defaultServer
|
||||
if len(name) > 0 && name[0] != "" {
|
||||
@ -50,7 +50,7 @@ func GetServer(name ...interface{}) *Server {
|
||||
}
|
||||
|
||||
// NewServer creates and returns a new normal TCP server.
|
||||
// The parameter <name> is optional, which is used to specify the instance name of the server.
|
||||
// The parameter `name` is optional, which is used to specify the instance name of the server.
|
||||
func NewServer(address string, handler func(*Conn), name ...string) *Server {
|
||||
s := &Server{
|
||||
address: address,
|
||||
@ -63,7 +63,7 @@ func NewServer(address string, handler func(*Conn), name ...string) *Server {
|
||||
}
|
||||
|
||||
// NewServerTLS creates and returns a new TCP server with TLS support.
|
||||
// The parameter <name> is optional, which is used to specify the instance name of the server.
|
||||
// The parameter `name` is optional, which is used to specify the instance name of the server.
|
||||
func NewServerTLS(address string, tlsConfig *tls.Config, handler func(*Conn), name ...string) *Server {
|
||||
s := NewServer(address, handler, name...)
|
||||
s.SetTLSConfig(tlsConfig)
|
||||
@ -71,7 +71,7 @@ func NewServerTLS(address string, tlsConfig *tls.Config, handler func(*Conn), na
|
||||
}
|
||||
|
||||
// NewServerKeyCrt creates and returns a new TCP server with TLS support.
|
||||
// The parameter <name> is optional, which is used to specify the instance name of the server.
|
||||
// The parameter `name` is optional, which is used to specify the instance name of the server.
|
||||
func NewServerKeyCrt(address, crtFile, keyFile string, handler func(*Conn), name ...string) *Server {
|
||||
s := NewServer(address, handler, name...)
|
||||
if err := s.SetTLSKeyCrt(crtFile, keyFile); err != nil {
|
||||
|
@ -32,8 +32,8 @@ type Retry struct {
|
||||
Interval time.Duration // Retry interval.
|
||||
}
|
||||
|
||||
// NewConn creates UDP connection to <remoteAddress>.
|
||||
// The optional parameter <localAddress> specifies the local address for connection.
|
||||
// NewConn creates UDP connection to `remoteAddress`.
|
||||
// The optional parameter `localAddress` specifies the local address for connection.
|
||||
func NewConn(remoteAddress string, localAddress ...string) (*Conn, error) {
|
||||
if conn, err := NewNetConn(remoteAddress, localAddress...); err == nil {
|
||||
return NewConnByNetConn(conn), nil
|
||||
@ -83,7 +83,7 @@ func (c *Conn) Send(data []byte, retry ...Retry) (err error) {
|
||||
}
|
||||
|
||||
// Recv receives and returns data from remote address.
|
||||
// The parameter <buffer> is used for customizing the receiving buffer size. If <buffer> <= 0,
|
||||
// The parameter `buffer` is used for customizing the receiving buffer size. If `buffer` <= 0,
|
||||
// it uses the default buffer size, which is 1024 byte.
|
||||
//
|
||||
// There's package border in UDP protocol, we can receive a complete package if specified
|
||||
|
@ -31,7 +31,7 @@ func NewNetConn(remoteAddress string, localAddress ...string) (*net.UDPConn, err
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
// Send writes data to <address> using UDP connection and then closes the connection.
|
||||
// Send writes data to `address` using UDP connection and then closes the connection.
|
||||
// Note that it is used for short connection usage.
|
||||
func Send(address string, data []byte, retry ...Retry) error {
|
||||
conn, err := NewConn(address)
|
||||
@ -42,7 +42,7 @@ func Send(address string, data []byte, retry ...Retry) error {
|
||||
return conn.Send(data, retry...)
|
||||
}
|
||||
|
||||
// SendRecv writes data to <address> using UDP connection, reads response and then closes the connection.
|
||||
// SendRecv writes data to `address` using UDP connection, reads response and then closes the connection.
|
||||
// Note that it is used for short connection usage.
|
||||
func SendRecv(address string, data []byte, receive int, retry ...Retry) ([]byte, error) {
|
||||
conn, err := NewConn(address)
|
||||
|
@ -48,7 +48,7 @@ func GetServer(name ...interface{}) *Server {
|
||||
}
|
||||
|
||||
// NewServer creates and returns a UDP server.
|
||||
// The optional parameter <name> is used to specify its name, which can be used for
|
||||
// The optional parameter `name` is used to specify its name, which can be used for
|
||||
// GetServer function to retrieve its instance.
|
||||
func NewServer(address string, handler func(*Conn), name ...string) *Server {
|
||||
s := &Server{
|
||||
|
@ -13,6 +13,9 @@ import (
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/internal/intlog"
|
||||
"github.com/gogf/gf/v2/internal/utils"
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
"github.com/gogf/gf/v2/os/genv"
|
||||
)
|
||||
|
||||
// Config is the configuration management object.
|
||||
@ -107,13 +110,60 @@ func (c *Config) Get(ctx context.Context, pattern string, def ...interface{}) (*
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if value == nil && len(def) > 0 {
|
||||
return gvar.New(def[0]), nil
|
||||
if value == nil {
|
||||
if len(def) > 0 {
|
||||
return gvar.New(def[0]), nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
return gvar.New(value), nil
|
||||
}
|
||||
|
||||
// GetWithEnv returns the configuration value specified by pattern `pattern`.
|
||||
// If the configuration value does not exist, then it retrieves and returns the environment value specified by `key`.
|
||||
// It returns the default value `def` if none of them exists.
|
||||
//
|
||||
// Fetching Rules: Environment arguments are in uppercase format, eg: GF_PACKAGE_VARIABLE.
|
||||
func (c *Config) GetWithEnv(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error) {
|
||||
value, err := c.Get(ctx, pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if value == nil {
|
||||
if v := genv.Get(utils.FormatEnvKey(pattern)); v != nil {
|
||||
return v, nil
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return gvar.New(def[0]), nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// GetWithCmd returns the configuration value specified by pattern `pattern`.
|
||||
// If the configuration value does not exist, then it retrieves and returns the command line option specified by `key`.
|
||||
// It returns the default value `def` if none of them exists.
|
||||
//
|
||||
// Fetching Rules: Command line arguments are in lowercase format, eg: gf.package.variable.
|
||||
func (c *Config) GetWithCmd(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error) {
|
||||
value, err := c.Get(ctx, pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if value == nil {
|
||||
if v := gcmd.GetOpt(utils.FormatCmdKey(pattern)); v != nil {
|
||||
return v, nil
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return gvar.New(def[0]), nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// Data retrieves and returns all configuration data as map type.
|
||||
func (c *Config) Data(ctx context.Context) (data map[string]interface{}, err error) {
|
||||
adapterData, err := c.adapter.Data(ctx)
|
||||
@ -137,7 +187,28 @@ func (c *Config) MustGet(ctx context.Context, pattern string, def ...interface{}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return gvar.New(v)
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// MustGetWithEnv acts as function GetWithEnv, but it panics if error occurs.
|
||||
func (c *Config) MustGetWithEnv(ctx context.Context, pattern string, def ...interface{}) *gvar.Var {
|
||||
v, err := c.GetWithEnv(ctx, pattern, def...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// MustGetWithCmd acts as function GetWithCmd, but it panics if error occurs.
|
||||
func (c *Config) MustGetWithCmd(ctx context.Context, pattern string, def ...interface{}) *gvar.Var {
|
||||
v, err := c.GetWithCmd(ctx, pattern, def...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// MustData acts as function Data, but it panics if error occurs.
|
||||
|
@ -9,6 +9,8 @@
|
||||
package gcfg_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
"github.com/gogf/gf/v2/os/genv"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
@ -191,3 +193,52 @@ func TestCfg_Get_WrongConfigFile(t *testing.T) {
|
||||
adapterFile.Clear()
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetWithEnv(t *testing.T) {
|
||||
content := `
|
||||
v1 = 1
|
||||
v2 = "true"
|
||||
v3 = "off"
|
||||
v4 = "1.23"
|
||||
array = [1,2,3]
|
||||
[redis]
|
||||
disk = "127.0.0.1:6379,0"
|
||||
cache = "127.0.0.1:6379,1"
|
||||
`
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
c, err := gcfg.New()
|
||||
t.AssertNil(err)
|
||||
c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
|
||||
defer c.GetAdapter().(*gcfg.AdapterFile).ClearContent()
|
||||
t.Assert(c.MustGet(ctx, "v1"), 1)
|
||||
t.Assert(c.MustGetWithEnv(ctx, `redis.user`), nil)
|
||||
t.Assert(genv.Set("REDIS_USER", `1`), nil)
|
||||
defer genv.Remove(`REDIS_USER`)
|
||||
t.Assert(c.MustGetWithEnv(ctx, `redis.user`), `1`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetWithCmd(t *testing.T) {
|
||||
content := `
|
||||
v1 = 1
|
||||
v2 = "true"
|
||||
v3 = "off"
|
||||
v4 = "1.23"
|
||||
array = [1,2,3]
|
||||
[redis]
|
||||
disk = "127.0.0.1:6379,0"
|
||||
cache = "127.0.0.1:6379,1"
|
||||
`
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
|
||||
c, err := gcfg.New()
|
||||
t.AssertNil(err)
|
||||
c.GetAdapter().(*gcfg.AdapterFile).SetContent(content)
|
||||
defer c.GetAdapter().(*gcfg.AdapterFile).ClearContent()
|
||||
t.Assert(c.MustGet(ctx, "v1"), 1)
|
||||
t.Assert(c.MustGetWithCmd(ctx, `redis.user`), nil)
|
||||
|
||||
gcmd.Init([]string{"gf", "--redis.user=2"}...)
|
||||
t.Assert(c.MustGetWithCmd(ctx, `redis.user`), `2`)
|
||||
})
|
||||
}
|
||||
|
@ -11,29 +11,26 @@ package gcmd
|
||||
import (
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/internal/command"
|
||||
"github.com/gogf/gf/v2/internal/utils"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultCommandFuncMap = make(map[string]func())
|
||||
)
|
||||
|
||||
// Custom initialization.
|
||||
// Init does custom initialization.
|
||||
func Init(args ...string) {
|
||||
command.Init(args...)
|
||||
}
|
||||
|
||||
// GetOpt returns the option value named <name>.
|
||||
func GetOpt(name string, def ...string) string {
|
||||
// GetOpt returns the option value named `name` as gvar.Var.
|
||||
func GetOpt(name string, def ...string) *gvar.Var {
|
||||
Init()
|
||||
return command.GetOpt(name, def...)
|
||||
}
|
||||
|
||||
// GetOptVar returns the option value named <name> as gvar.Var.
|
||||
func GetOptVar(name string, def ...string) *gvar.Var {
|
||||
Init()
|
||||
return gvar.New(GetOpt(name, def...))
|
||||
if v := command.GetOpt(name, def...); v != "" {
|
||||
return gvar.New(v)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetOptAll returns all parsed options.
|
||||
@ -42,22 +39,19 @@ func GetOptAll() map[string]string {
|
||||
return command.GetOptAll()
|
||||
}
|
||||
|
||||
// ContainsOpt checks whether option named <name> exist in the arguments.
|
||||
// ContainsOpt checks whether option named `name` exist in the arguments.
|
||||
func ContainsOpt(name string) bool {
|
||||
Init()
|
||||
return command.ContainsOpt(name)
|
||||
}
|
||||
|
||||
// GetArg returns the argument at <index>.
|
||||
func GetArg(index int, def ...string) string {
|
||||
// GetArg returns the argument at `index` as gvar.Var.
|
||||
func GetArg(index int, def ...string) *gvar.Var {
|
||||
Init()
|
||||
return command.GetArg(index, def...)
|
||||
}
|
||||
|
||||
// GetArgVar returns the argument at <index> as gvar.Var.
|
||||
func GetArgVar(index int, def ...string) *gvar.Var {
|
||||
Init()
|
||||
return gvar.New(GetArg(index, def...))
|
||||
if v := command.GetArg(index, def...); v != "" {
|
||||
return gvar.New(v)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetArgAll returns all parsed arguments.
|
||||
@ -66,19 +60,19 @@ func GetArgAll() []string {
|
||||
return command.GetArgAll()
|
||||
}
|
||||
|
||||
// GetOptWithEnv returns the command line argument of the specified <key>.
|
||||
// If the argument does not exist, then it returns the environment variable with specified <key>.
|
||||
// It returns the default value <def> if none of them exists.
|
||||
// GetOptWithEnv returns the command line argument of the specified `key`.
|
||||
// If the argument does not exist, then it returns the environment variable with specified `key`.
|
||||
// It returns the default value `def` if none of them exists.
|
||||
//
|
||||
// Fetching Rules:
|
||||
// 1. Command line arguments are in lowercase format, eg: gf.<package name>.<variable name>;
|
||||
// 2. Environment arguments are in uppercase format, eg: GF_<package name>_<variable name>;
|
||||
// 1. Command line arguments are in lowercase format, eg: gf.`package name`.<variable name>;
|
||||
// 2. Environment arguments are in uppercase format, eg: GF_`package name`_<variable name>;
|
||||
func GetOptWithEnv(key string, def ...interface{}) *gvar.Var {
|
||||
cmdKey := strings.ToLower(strings.Replace(key, "_", ".", -1))
|
||||
cmdKey := utils.FormatCmdKey(key)
|
||||
if ContainsOpt(cmdKey) {
|
||||
return gvar.New(GetOpt(cmdKey))
|
||||
} else {
|
||||
envKey := strings.ToUpper(strings.Replace(key, ".", "_", -1))
|
||||
envKey := utils.FormatEnvKey(key)
|
||||
if r, ok := os.LookupEnv(envKey); ok {
|
||||
return gvar.New(r)
|
||||
} else {
|
||||
@ -87,7 +81,7 @@ func GetOptWithEnv(key string, def ...interface{}) *gvar.Var {
|
||||
}
|
||||
}
|
||||
}
|
||||
return gvar.New(nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
// BuildOptions builds the options as string.
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
// BindHandle registers callback function <f> with <cmd>.
|
||||
// BindHandle registers callback function `f` with `cmd`.
|
||||
func BindHandle(cmd string, f func()) error {
|
||||
if _, ok := defaultCommandFuncMap[cmd]; ok {
|
||||
return gerror.NewCode(gcode.CodeInvalidOperation, "duplicated handle for command:"+cmd)
|
||||
@ -22,7 +22,7 @@ func BindHandle(cmd string, f func()) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// BindHandleMap registers callback function with map <m>.
|
||||
// BindHandleMap registers callback function with map `m`.
|
||||
func BindHandleMap(m map[string]func()) error {
|
||||
var err error
|
||||
for k, v := range m {
|
||||
@ -33,7 +33,7 @@ func BindHandleMap(m map[string]func()) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// RunHandle executes the callback function registered by <cmd>.
|
||||
// RunHandle executes the callback function registered by `cmd`.
|
||||
func RunHandle(cmd string) error {
|
||||
if handle, ok := defaultCommandFuncMap[cmd]; ok {
|
||||
handle()
|
||||
@ -46,11 +46,11 @@ func RunHandle(cmd string) error {
|
||||
// AutoRun automatically recognizes and executes the callback function
|
||||
// by value of index 0 (the first console parameter).
|
||||
func AutoRun() error {
|
||||
if cmd := GetArg(1); cmd != "" {
|
||||
if handle, ok := defaultCommandFuncMap[cmd]; ok {
|
||||
if cmd := GetArg(1); !cmd.IsEmpty() {
|
||||
if handle, ok := defaultCommandFuncMap[cmd.String()]; ok {
|
||||
handle()
|
||||
} else {
|
||||
return gerror.NewCode(gcode.CodeMissingConfiguration, "no handle found for command:"+cmd)
|
||||
return gerror.NewCode(gcode.CodeMissingConfiguration, "no handle found for command:"+cmd.String())
|
||||
}
|
||||
} else {
|
||||
return gerror.NewCode(gcode.CodeMissingParameter, "no command found")
|
||||
|
@ -33,20 +33,20 @@ type Parser struct {
|
||||
|
||||
// Parse creates and returns a new Parser with os.Args and supported options.
|
||||
//
|
||||
// Note that the parameter <supportedOptions> is as [option name: need argument], which means
|
||||
// the value item of <supportedOptions> indicates whether corresponding option name needs argument or not.
|
||||
// Note that the parameter `supportedOptions` is as [option name: need argument], which means
|
||||
// the value item of `supportedOptions` indicates whether corresponding option name needs argument or not.
|
||||
//
|
||||
// The optional parameter <strict> specifies whether stops parsing and returns error if invalid option passed.
|
||||
// The optional parameter `strict` specifies whether stops parsing and returns error if invalid option passed.
|
||||
func Parse(supportedOptions map[string]bool, strict ...bool) (*Parser, error) {
|
||||
return ParseWithArgs(os.Args, supportedOptions, strict...)
|
||||
}
|
||||
|
||||
// ParseWithArgs creates and returns a new Parser with given arguments and supported options.
|
||||
//
|
||||
// Note that the parameter <supportedOptions> is as [option name: need argument], which means
|
||||
// the value item of <supportedOptions> indicates whether corresponding option name needs argument or not.
|
||||
// Note that the parameter `supportedOptions` is as [option name: need argument], which means
|
||||
// the value item of `supportedOptions` indicates whether corresponding option name needs argument or not.
|
||||
//
|
||||
// The optional parameter <strict> specifies whether stops parsing and returns error if invalid option passed.
|
||||
// The optional parameter `strict` specifies whether stops parsing and returns error if invalid option passed.
|
||||
func ParseWithArgs(args []string, supportedOptions map[string]bool, strict ...bool) (*Parser, error) {
|
||||
strictParsing := false
|
||||
if len(strict) > 0 {
|
||||
@ -158,26 +158,15 @@ func (p *Parser) setOptionValue(name, value string) {
|
||||
}
|
||||
}
|
||||
|
||||
// GetOpt returns the option value named <name>.
|
||||
func (p *Parser) GetOpt(name string, def ...string) string {
|
||||
// GetOpt returns the option value named `name` as gvar.Var.
|
||||
func (p *Parser) GetOpt(name string, def ...interface{}) *gvar.Var {
|
||||
if v, ok := p.parsedOptions[name]; ok {
|
||||
return v
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetOptVar returns the option value named <name> as gvar.Var.
|
||||
func (p *Parser) GetOptVar(name string, def ...interface{}) *gvar.Var {
|
||||
if p.ContainsOpt(name) {
|
||||
return gvar.New(p.GetOpt(name))
|
||||
return gvar.New(v)
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return gvar.New(def[0])
|
||||
}
|
||||
return gvar.New(nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetOptAll returns all parsed options.
|
||||
@ -185,26 +174,21 @@ func (p *Parser) GetOptAll() map[string]string {
|
||||
return p.parsedOptions
|
||||
}
|
||||
|
||||
// ContainsOpt checks whether option named <name> exist in the arguments.
|
||||
// ContainsOpt checks whether option named `name` exist in the arguments.
|
||||
func (p *Parser) ContainsOpt(name string) bool {
|
||||
_, ok := p.parsedOptions[name]
|
||||
return ok
|
||||
}
|
||||
|
||||
// GetArg returns the argument at <index>.
|
||||
func (p *Parser) GetArg(index int, def ...string) string {
|
||||
// GetArg returns the argument at `index` as gvar.Var.
|
||||
func (p *Parser) GetArg(index int, def ...string) *gvar.Var {
|
||||
if index < len(p.parsedArgs) {
|
||||
return p.parsedArgs[index]
|
||||
return gvar.New(p.parsedArgs[index])
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return def[0]
|
||||
return gvar.New(def[0])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetArgVar returns the argument at <index> as gvar.Var.
|
||||
func (p *Parser) GetArgVar(index int, def ...string) *gvar.Var {
|
||||
return gvar.New(p.GetArg(index, def...))
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetArgAll returns all parsed arguments.
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
// BindHandle registers callback function <f> with <cmd>.
|
||||
// BindHandle registers callback function `f` with `cmd`.
|
||||
func (p *Parser) BindHandle(cmd string, f func()) error {
|
||||
if _, ok := p.commandFuncMap[cmd]; ok {
|
||||
return gerror.NewCode(gcode.CodeInvalidOperation, "duplicated handle for command:"+cmd)
|
||||
@ -22,7 +22,7 @@ func (p *Parser) BindHandle(cmd string, f func()) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// BindHandleMap registers callback function with map <m>.
|
||||
// BindHandleMap registers callback function with map `m`.
|
||||
func (p *Parser) BindHandleMap(m map[string]func()) error {
|
||||
var err error
|
||||
for k, v := range m {
|
||||
@ -33,7 +33,7 @@ func (p *Parser) BindHandleMap(m map[string]func()) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// RunHandle executes the callback function registered by <cmd>.
|
||||
// RunHandle executes the callback function registered by `cmd`.
|
||||
func (p *Parser) RunHandle(cmd string) error {
|
||||
if handle, ok := p.commandFuncMap[cmd]; ok {
|
||||
handle()
|
||||
@ -46,11 +46,11 @@ func (p *Parser) RunHandle(cmd string) error {
|
||||
// AutoRun automatically recognizes and executes the callback function
|
||||
// by value of index 0 (the first console parameter).
|
||||
func (p *Parser) AutoRun() error {
|
||||
if cmd := p.GetArg(1); cmd != "" {
|
||||
if handle, ok := p.commandFuncMap[cmd]; ok {
|
||||
if cmd := p.GetArg(1); !cmd.IsEmpty() {
|
||||
if handle, ok := p.commandFuncMap[cmd.String()]; ok {
|
||||
handle()
|
||||
} else {
|
||||
return gerror.NewCode(gcode.CodeMissingConfiguration, "no handle found for command:"+cmd)
|
||||
return gerror.NewCode(gcode.CodeMissingConfiguration, "no handle found for command:"+cmd.String())
|
||||
}
|
||||
} else {
|
||||
return gerror.NewCode(gcode.CodeMissingParameter, "no command found")
|
||||
|
@ -15,13 +15,13 @@ import (
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
// Scan prints <info> to stdout, reads and returns user input, which stops by '\n'.
|
||||
// Scan prints `info` to stdout, reads and returns user input, which stops by '\n'.
|
||||
func Scan(info ...interface{}) string {
|
||||
fmt.Print(info...)
|
||||
return readline()
|
||||
}
|
||||
|
||||
// Scanf prints <info> to stdout with <format>, reads and returns user input, which stops by '\n'.
|
||||
// Scanf prints `info` to stdout with `format`, reads and returns user input, which stops by '\n'.
|
||||
func Scanf(format string, info ...interface{}) string {
|
||||
fmt.Printf(format, info...)
|
||||
return readline()
|
||||
|
@ -33,14 +33,14 @@ func Test_Parse(t *testing.T) {
|
||||
t.Assert(p.GetArg(0), "gf")
|
||||
t.Assert(p.GetArg(1), "remove")
|
||||
t.Assert(p.GetArg(2), "path")
|
||||
t.Assert(p.GetArgVar(2).String(), "path")
|
||||
t.Assert(p.GetArg(2).String(), "path")
|
||||
|
||||
t.Assert(len(p.GetOptAll()), 8)
|
||||
t.Assert(p.GetOpt("n"), "root")
|
||||
t.Assert(p.GetOpt("name"), "root")
|
||||
t.Assert(p.GetOpt("p"), "www")
|
||||
t.Assert(p.GetOpt("prefix"), "www")
|
||||
t.Assert(p.GetOptVar("prefix").String(), "www")
|
||||
t.Assert(p.GetOpt("prefix").String(), "www")
|
||||
|
||||
t.Assert(p.ContainsOpt("n"), true)
|
||||
t.Assert(p.ContainsOpt("name"), true)
|
||||
@ -69,14 +69,14 @@ func Test_ParseWithArgs(t *testing.T) {
|
||||
t.Assert(p.GetArg(0), "gf")
|
||||
t.Assert(p.GetArg(1), "remove")
|
||||
t.Assert(p.GetArg(2), "path")
|
||||
t.Assert(p.GetArgVar(2).String(), "path")
|
||||
t.Assert(p.GetArg(2).String(), "path")
|
||||
|
||||
t.Assert(len(p.GetOptAll()), 8)
|
||||
t.Assert(p.GetOpt("n"), "root")
|
||||
t.Assert(p.GetOpt("name"), "root")
|
||||
t.Assert(p.GetOpt("p"), "www")
|
||||
t.Assert(p.GetOpt("prefix"), "www")
|
||||
t.Assert(p.GetOptVar("prefix").String(), "www")
|
||||
t.Assert(p.GetOpt("prefix").String(), "www")
|
||||
|
||||
t.Assert(p.ContainsOpt("n"), true)
|
||||
t.Assert(p.ContainsOpt("name"), true)
|
||||
|
@ -9,6 +9,7 @@ package genv
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/internal/utils"
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
"os"
|
||||
"strings"
|
||||
@ -31,28 +32,21 @@ func Map() map[string]string {
|
||||
return m
|
||||
}
|
||||
|
||||
// Get returns the value of the environment variable named by the <key>.
|
||||
// It returns given <def> if the variable does not exist in the environment.
|
||||
func Get(key string, def ...string) string {
|
||||
v, ok := os.LookupEnv(key)
|
||||
if !ok && len(def) > 0 {
|
||||
return def[0]
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// GetVar creates and returns a Var with the value of the environment variable
|
||||
// named by the <key>. It uses the given <def> if the variable does not exist
|
||||
// Get creates and returns a Var with the value of the environment variable
|
||||
// named by the `key`. It uses the given `def` if the variable does not exist
|
||||
// in the environment.
|
||||
func GetVar(key string, def ...interface{}) *gvar.Var {
|
||||
func Get(key string, def ...interface{}) *gvar.Var {
|
||||
v, ok := os.LookupEnv(key)
|
||||
if !ok && len(def) > 0 {
|
||||
return gvar.New(def[0])
|
||||
if !ok {
|
||||
if len(def) > 0 {
|
||||
return gvar.New(def[0])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return gvar.New(v)
|
||||
}
|
||||
|
||||
// Set sets the value of the environment variable named by the <key>.
|
||||
// Set sets the value of the environment variable named by the `key`.
|
||||
// It returns an error, if any.
|
||||
func Set(key, value string) error {
|
||||
return os.Setenv(key, value)
|
||||
@ -68,7 +62,7 @@ func SetMap(m map[string]string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Contains checks whether the environment variable named <key> exists.
|
||||
// Contains checks whether the environment variable named `key` exists.
|
||||
func Contains(key string) bool {
|
||||
_, ok := os.LookupEnv(key)
|
||||
return ok
|
||||
@ -86,31 +80,29 @@ func Remove(key ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetWithCmd returns the environment value specified <key>.
|
||||
// GetWithCmd returns the environment value specified `key`.
|
||||
// If the environment value does not exist, then it retrieves and returns the value from command line options.
|
||||
// It returns the default value <def> if none of them exists.
|
||||
// It returns the default value `def` if none of them exists.
|
||||
//
|
||||
// Fetching Rules:
|
||||
// 1. Environment arguments are in uppercase format, eg: GF_<package name>_<variable name>;
|
||||
// 2. Command line arguments are in lowercase format, eg: gf.<package name>.<variable name>;
|
||||
func GetWithCmd(key string, def ...interface{}) *gvar.Var {
|
||||
value := interface{}(nil)
|
||||
if len(def) > 0 {
|
||||
value = def[0]
|
||||
}
|
||||
envKey := strings.ToUpper(strings.Replace(key, ".", "_", -1))
|
||||
envKey := utils.FormatEnvKey(key)
|
||||
if v := os.Getenv(envKey); v != "" {
|
||||
value = v
|
||||
} else {
|
||||
cmdKey := strings.ToLower(strings.Replace(key, "_", ".", -1))
|
||||
if v := gcmd.GetOpt(cmdKey); v != "" {
|
||||
value = v
|
||||
}
|
||||
return gvar.New(v)
|
||||
}
|
||||
return gvar.New(value)
|
||||
cmdKey := utils.FormatCmdKey(key)
|
||||
if v := gcmd.GetOpt(cmdKey); !v.IsEmpty() {
|
||||
return v
|
||||
}
|
||||
if len(def) > 0 {
|
||||
return gvar.New(def[0])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Build builds a map to a environment variable slice.
|
||||
// Build builds a map to an environment variable slice.
|
||||
func Build(m map[string]string) []string {
|
||||
array := make([]string, len(m))
|
||||
index := 0
|
||||
|
@ -40,7 +40,7 @@ func Test_GEnv_Get(t *testing.T) {
|
||||
key := "TEST_ENV_" + value
|
||||
err := os.Setenv(key, "TEST")
|
||||
t.Assert(err, nil)
|
||||
t.AssertEQ(genv.Get(key), "TEST")
|
||||
t.AssertEQ(genv.Get(key).String(), "TEST")
|
||||
})
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ func Test_GEnv_GetVar(t *testing.T) {
|
||||
key := "TEST_ENV_" + value
|
||||
err := os.Setenv(key, "TEST")
|
||||
t.Assert(err, nil)
|
||||
t.AssertEQ(genv.GetVar(key).String(), "TEST")
|
||||
t.AssertEQ(genv.Get(key).String(), "TEST")
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -58,8 +58,8 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// Mkdir creates directories recursively with given <path>.
|
||||
// The parameter <path> is suggested to be an absolute path instead of relative one.
|
||||
// Mkdir creates directories recursively with given `path`.
|
||||
// The parameter `path` is suggested to be an absolute path instead of relative one.
|
||||
func Mkdir(path string) error {
|
||||
if err := os.MkdirAll(path, os.ModePerm); err != nil {
|
||||
return err
|
||||
@ -67,8 +67,8 @@ func Mkdir(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create creates file with given <path> recursively.
|
||||
// The parameter <path> is suggested to be absolute path.
|
||||
// Create creates file with given `path` recursively.
|
||||
// The parameter `path` is suggested to be absolute path.
|
||||
func Create(path string) (*os.File, error) {
|
||||
dir := Dir(path)
|
||||
if !Exists(dir) {
|
||||
@ -84,15 +84,15 @@ func Open(path string) (*os.File, error) {
|
||||
return os.Open(path)
|
||||
}
|
||||
|
||||
// OpenFile opens file/directory with custom <flag> and <perm>.
|
||||
// The parameter <flag> is like: O_RDONLY, O_RDWR, O_RDWR|O_CREATE|O_TRUNC, etc.
|
||||
// OpenFile opens file/directory with custom `flag` and `perm`.
|
||||
// The parameter `flag` is like: O_RDONLY, O_RDWR, O_RDWR|O_CREATE|O_TRUNC, etc.
|
||||
func OpenFile(path string, flag int, perm os.FileMode) (*os.File, error) {
|
||||
return os.OpenFile(path, flag, perm)
|
||||
}
|
||||
|
||||
// OpenWithFlag opens file/directory with default perm and custom <flag>.
|
||||
// The default <perm> is 0666.
|
||||
// The parameter <flag> is like: O_RDONLY, O_RDWR, O_RDWR|O_CREATE|O_TRUNC, etc.
|
||||
// OpenWithFlag opens file/directory with default perm and custom `flag`.
|
||||
// The default `perm` is 0666.
|
||||
// The parameter `flag` is like: O_RDONLY, O_RDWR, O_RDWR|O_CREATE|O_TRUNC, etc.
|
||||
func OpenWithFlag(path string, flag int) (*os.File, error) {
|
||||
f, err := os.OpenFile(path, flag, DefaultPermOpen)
|
||||
if err != nil {
|
||||
@ -101,9 +101,9 @@ func OpenWithFlag(path string, flag int) (*os.File, error) {
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// OpenWithFlagPerm opens file/directory with custom <flag> and <perm>.
|
||||
// The parameter <flag> is like: O_RDONLY, O_RDWR, O_RDWR|O_CREATE|O_TRUNC, etc.
|
||||
// The parameter <perm> is like: 0600, 0666, 0777, etc.
|
||||
// OpenWithFlagPerm opens file/directory with custom `flag` and `perm`.
|
||||
// The parameter `flag` is like: O_RDONLY, O_RDWR, O_RDWR|O_CREATE|O_TRUNC, etc.
|
||||
// The parameter `perm` is like: 0600, 0666, 0777, etc.
|
||||
func OpenWithFlagPerm(path string, flag int, perm os.FileMode) (*os.File, error) {
|
||||
f, err := os.OpenFile(path, flag, perm)
|
||||
if err != nil {
|
||||
@ -124,7 +124,7 @@ func Join(paths ...string) string {
|
||||
return s
|
||||
}
|
||||
|
||||
// Exists checks whether given <path> exist.
|
||||
// Exists checks whether given `path` exist.
|
||||
func Exists(path string) bool {
|
||||
if stat, err := os.Stat(path); stat != nil && !os.IsNotExist(err) {
|
||||
return true
|
||||
@ -132,8 +132,8 @@ func Exists(path string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsDir checks whether given <path> a directory.
|
||||
// Note that it returns false if the <path> does not exist.
|
||||
// IsDir checks whether given `path` a directory.
|
||||
// Note that it returns false if the `path` does not exist.
|
||||
func IsDir(path string) bool {
|
||||
s, err := os.Stat(path)
|
||||
if err != nil {
|
||||
@ -159,8 +159,8 @@ func Chdir(dir string) error {
|
||||
return os.Chdir(dir)
|
||||
}
|
||||
|
||||
// IsFile checks whether given <path> a file, which means it's not a directory.
|
||||
// Note that it returns false if the <path> does not exist.
|
||||
// IsFile checks whether given `path` a file, which means it's not a directory.
|
||||
// Note that it returns false if the `path` does not exist.
|
||||
func IsFile(path string) bool {
|
||||
s, err := os.Stat(path)
|
||||
if err != nil {
|
||||
@ -181,8 +181,8 @@ func Stat(path string) (os.FileInfo, error) {
|
||||
return os.Stat(path)
|
||||
}
|
||||
|
||||
// Move renames (moves) <src> to <dst> path.
|
||||
// If <dst> already exists and is not a directory, it'll be replaced.
|
||||
// Move renames (moves) `src` to `dst` path.
|
||||
// If `dst` already exists and is not a directory, it'll be replaced.
|
||||
func Move(src string, dst string) error {
|
||||
return os.Rename(src, dst)
|
||||
}
|
||||
@ -193,7 +193,7 @@ func Rename(src string, dst string) error {
|
||||
return Move(src, dst)
|
||||
}
|
||||
|
||||
// DirNames returns sub-file names of given directory <path>.
|
||||
// DirNames returns sub-file names of given directory `path`.
|
||||
// Note that the returned names are NOT absolute paths.
|
||||
func DirNames(path string) ([]string, error) {
|
||||
f, err := os.Open(path)
|
||||
@ -231,13 +231,13 @@ func Glob(pattern string, onlyNames ...bool) ([]string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Remove deletes all file/directory with <path> parameter.
|
||||
// If parameter <path> is directory, it deletes it recursively.
|
||||
// Remove deletes all file/directory with `path` parameter.
|
||||
// If parameter `path` is directory, it deletes it recursively.
|
||||
func Remove(path string) error {
|
||||
return os.RemoveAll(path)
|
||||
}
|
||||
|
||||
// IsReadable checks whether given <path> is readable.
|
||||
// IsReadable checks whether given `path` is readable.
|
||||
func IsReadable(path string) bool {
|
||||
result := true
|
||||
file, err := os.OpenFile(path, os.O_RDONLY, DefaultPermOpen)
|
||||
@ -248,7 +248,7 @@ func IsReadable(path string) bool {
|
||||
return result
|
||||
}
|
||||
|
||||
// IsWritable checks whether given <path> is writable.
|
||||
// IsWritable checks whether given `path` is writable.
|
||||
//
|
||||
// TODO improve performance; use golang.org/x/sys to cross-plat-form
|
||||
func IsWritable(path string) bool {
|
||||
@ -288,7 +288,7 @@ func Abs(path string) string {
|
||||
return p
|
||||
}
|
||||
|
||||
// RealPath converts the given <path> to its absolute path
|
||||
// RealPath converts the given `path` to its absolute path
|
||||
// and checks if the file path exists.
|
||||
// If the file does not exist, return an empty string.
|
||||
func RealPath(path string) string {
|
||||
@ -354,11 +354,11 @@ func Dir(path string) string {
|
||||
return filepath.Dir(path)
|
||||
}
|
||||
|
||||
// IsEmpty checks whether the given <path> is empty.
|
||||
// If <path> is a folder, it checks if there's any file under it.
|
||||
// If <path> is a file, it checks if the file size is zero.
|
||||
// IsEmpty checks whether the given `path` is empty.
|
||||
// If `path` is a folder, it checks if there's any file under it.
|
||||
// If `path` is a file, it checks if the file size is zero.
|
||||
//
|
||||
// Note that it returns true if <path> does not exist.
|
||||
// Note that it returns true if `path` does not exist.
|
||||
func IsEmpty(path string) bool {
|
||||
stat, err := Stat(path)
|
||||
if err != nil {
|
||||
@ -403,7 +403,7 @@ func ExtName(path string) string {
|
||||
// TempDir retrieves and returns the temporary directory of current system.
|
||||
// It return "/tmp" is current in *nix system, or else it returns os.TempDir().
|
||||
//
|
||||
// The optional parameter <names> specifies the its sub-folders/sub-files,
|
||||
// The optional parameter `names` specifies the its sub-folders/sub-files,
|
||||
// which will be joined with current system separator and returned with the path.
|
||||
func TempDir(names ...string) string {
|
||||
path := tempDir
|
||||
|
@ -28,16 +28,16 @@ var (
|
||||
internalCache = gcache.New()
|
||||
)
|
||||
|
||||
// GetContentsWithCache returns string content of given file by <path> from cache.
|
||||
// If there's no content in the cache, it will read it from disk file specified by <path>.
|
||||
// The parameter <expire> specifies the caching time for this file content in seconds.
|
||||
// GetContentsWithCache returns string content of given file by `path` from cache.
|
||||
// If there's no content in the cache, it will read it from disk file specified by `path`.
|
||||
// The parameter `expire` specifies the caching time for this file content in seconds.
|
||||
func GetContentsWithCache(path string, duration ...time.Duration) string {
|
||||
return string(GetBytesWithCache(path, duration...))
|
||||
}
|
||||
|
||||
// GetBytesWithCache returns []byte content of given file by <path> from cache.
|
||||
// If there's no content in the cache, it will read it from disk file specified by <path>.
|
||||
// The parameter <expire> specifies the caching time for this file content in seconds.
|
||||
// GetBytesWithCache returns []byte content of given file by `path` from cache.
|
||||
// If there's no content in the cache, it will read it from disk file specified by `path`.
|
||||
// The parameter `expire` specifies the caching time for this file content in seconds.
|
||||
func GetBytesWithCache(path string, duration ...time.Duration) []byte {
|
||||
var (
|
||||
ctx = context.Background()
|
||||
@ -51,7 +51,7 @@ func GetBytesWithCache(path string, duration ...time.Duration) []byte {
|
||||
r, _ := internalCache.GetOrSetFuncLock(ctx, cacheKey, func() (interface{}, error) {
|
||||
b := GetBytes(path)
|
||||
if b != nil {
|
||||
// Adding this <path> to gfsnotify,
|
||||
// Adding this `path` to gfsnotify,
|
||||
// it will clear its cache if there's any changes of the file.
|
||||
_, _ = gfsnotify.Add(path, func(event *gfsnotify.Event) {
|
||||
_, err := internalCache.Remove(ctx, cacheKey)
|
||||
|
@ -18,13 +18,13 @@ var (
|
||||
DefaultReadBuffer = 1024
|
||||
)
|
||||
|
||||
// GetContents returns the file content of <path> as string.
|
||||
// GetContents returns the file content of `path` as string.
|
||||
// It returns en empty string if it fails reading.
|
||||
func GetContents(path string) string {
|
||||
return string(GetBytes(path))
|
||||
}
|
||||
|
||||
// GetBytes returns the file content of <path> as []byte.
|
||||
// GetBytes returns the file content of `path` as []byte.
|
||||
// It returns nil if it fails reading.
|
||||
func GetBytes(path string) []byte {
|
||||
data, err := ioutil.ReadFile(path)
|
||||
@ -34,16 +34,16 @@ func GetBytes(path string) []byte {
|
||||
return data
|
||||
}
|
||||
|
||||
// putContents puts binary content to file of <path>.
|
||||
// putContents puts binary content to file of `path`.
|
||||
func putContents(path string, data []byte, flag int, perm os.FileMode) error {
|
||||
// It supports creating file of <path> recursively.
|
||||
// It supports creating file of `path` recursively.
|
||||
dir := Dir(path)
|
||||
if !Exists(dir) {
|
||||
if err := Mkdir(dir); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Opening file with given <flag> and <perm>.
|
||||
// Opening file with given `flag` and `perm`.
|
||||
f, err := OpenWithFlagPerm(path, flag, perm)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -57,36 +57,36 @@ func putContents(path string, data []byte, flag int, perm os.FileMode) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Truncate truncates file of <path> to given size by <size>.
|
||||
// Truncate truncates file of `path` to given size by `size`.
|
||||
func Truncate(path string, size int) error {
|
||||
return os.Truncate(path, int64(size))
|
||||
}
|
||||
|
||||
// PutContents puts string <content> to file of <path>.
|
||||
// It creates file of <path> recursively if it does not exist.
|
||||
// PutContents puts string `content` to file of `path`.
|
||||
// It creates file of `path` recursively if it does not exist.
|
||||
func PutContents(path string, content string) error {
|
||||
return putContents(path, []byte(content), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, DefaultPermOpen)
|
||||
}
|
||||
|
||||
// PutContentsAppend appends string <content> to file of <path>.
|
||||
// It creates file of <path> recursively if it does not exist.
|
||||
// PutContentsAppend appends string `content` to file of `path`.
|
||||
// It creates file of `path` recursively if it does not exist.
|
||||
func PutContentsAppend(path string, content string) error {
|
||||
return putContents(path, []byte(content), os.O_WRONLY|os.O_CREATE|os.O_APPEND, DefaultPermOpen)
|
||||
}
|
||||
|
||||
// PutBytes puts binary <content> to file of <path>.
|
||||
// It creates file of <path> recursively if it does not exist.
|
||||
// PutBytes puts binary `content` to file of `path`.
|
||||
// It creates file of `path` recursively if it does not exist.
|
||||
func PutBytes(path string, content []byte) error {
|
||||
return putContents(path, content, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, DefaultPermOpen)
|
||||
}
|
||||
|
||||
// PutBytesAppend appends binary <content> to file of <path>.
|
||||
// It creates file of <path> recursively if it does not exist.
|
||||
// PutBytesAppend appends binary `content` to file of `path`.
|
||||
// It creates file of `path` recursively if it does not exist.
|
||||
func PutBytesAppend(path string, content []byte) error {
|
||||
return putContents(path, content, os.O_WRONLY|os.O_CREATE|os.O_APPEND, DefaultPermOpen)
|
||||
}
|
||||
|
||||
// GetNextCharOffset returns the file offset for given <char> starting from <start>.
|
||||
// GetNextCharOffset returns the file offset for given `char` starting from `start`.
|
||||
func GetNextCharOffset(reader io.ReaderAt, char byte, start int64) int64 {
|
||||
buffer := make([]byte, DefaultReadBuffer)
|
||||
offset := start
|
||||
@ -105,8 +105,8 @@ func GetNextCharOffset(reader io.ReaderAt, char byte, start int64) int64 {
|
||||
return -1
|
||||
}
|
||||
|
||||
// GetNextCharOffsetByPath returns the file offset for given <char> starting from <start>.
|
||||
// It opens file of <path> for reading with os.O_RDONLY flag and default perm.
|
||||
// GetNextCharOffsetByPath returns the file offset for given `char` starting from `start`.
|
||||
// It opens file of `path` for reading with os.O_RDONLY flag and default perm.
|
||||
func GetNextCharOffsetByPath(path string, char byte, start int64) int64 {
|
||||
if f, err := OpenWithFlagPerm(path, os.O_RDONLY, DefaultPermOpen); err == nil {
|
||||
defer f.Close()
|
||||
@ -116,7 +116,7 @@ func GetNextCharOffsetByPath(path string, char byte, start int64) int64 {
|
||||
}
|
||||
|
||||
// GetBytesTilChar returns the contents of the file as []byte
|
||||
// until the next specified byte <char> position.
|
||||
// until the next specified byte `char` position.
|
||||
//
|
||||
// Note: Returned value contains the character of the last position.
|
||||
func GetBytesTilChar(reader io.ReaderAt, char byte, start int64) ([]byte, int64) {
|
||||
@ -126,9 +126,9 @@ func GetBytesTilChar(reader io.ReaderAt, char byte, start int64) ([]byte, int64)
|
||||
return nil, -1
|
||||
}
|
||||
|
||||
// GetBytesTilCharByPath returns the contents of the file given by <path> as []byte
|
||||
// until the next specified byte <char> position.
|
||||
// It opens file of <path> for reading with os.O_RDONLY flag and default perm.
|
||||
// GetBytesTilCharByPath returns the contents of the file given by `path` as []byte
|
||||
// until the next specified byte `char` position.
|
||||
// It opens file of `path` for reading with os.O_RDONLY flag and default perm.
|
||||
//
|
||||
// Note: Returned value contains the character of the last position.
|
||||
func GetBytesTilCharByPath(path string, char byte, start int64) ([]byte, int64) {
|
||||
@ -139,7 +139,7 @@ func GetBytesTilCharByPath(path string, char byte, start int64) ([]byte, int64)
|
||||
return nil, -1
|
||||
}
|
||||
|
||||
// GetBytesByTwoOffsets returns the binary content as []byte from <start> to <end>.
|
||||
// GetBytesByTwoOffsets returns the binary content as []byte from `start` to `end`.
|
||||
// Note: Returned value does not contain the character of the last position, which means
|
||||
// it returns content range as [start, end).
|
||||
func GetBytesByTwoOffsets(reader io.ReaderAt, start int64, end int64) []byte {
|
||||
@ -150,10 +150,10 @@ func GetBytesByTwoOffsets(reader io.ReaderAt, start int64, end int64) []byte {
|
||||
return buffer
|
||||
}
|
||||
|
||||
// GetBytesByTwoOffsetsByPath returns the binary content as []byte from <start> to <end>.
|
||||
// GetBytesByTwoOffsetsByPath returns the binary content as []byte from `start` to `end`.
|
||||
// Note: Returned value does not contain the character of the last position, which means
|
||||
// it returns content range as [start, end).
|
||||
// It opens file of <path> for reading with os.O_RDONLY flag and default perm.
|
||||
// It opens file of `path` for reading with os.O_RDONLY flag and default perm.
|
||||
func GetBytesByTwoOffsetsByPath(path string, start int64, end int64) []byte {
|
||||
if f, err := OpenWithFlagPerm(path, os.O_RDONLY, DefaultPermOpen); err == nil {
|
||||
defer f.Close()
|
||||
@ -162,11 +162,11 @@ func GetBytesByTwoOffsetsByPath(path string, start int64, end int64) []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadLines reads file content line by line, which is passed to the callback function <callback> as string.
|
||||
// ReadLines reads file content line by line, which is passed to the callback function `callback` as string.
|
||||
// It matches each line of text, separated by chars '\r' or '\n', stripped any trailing end-of-line marker.
|
||||
//
|
||||
// Note that the parameter passed to callback function might be an empty value, and the last non-empty line
|
||||
// will be passed to callback function <callback> even if it has no newline marker.
|
||||
// will be passed to callback function `callback` even if it has no newline marker.
|
||||
func ReadLines(file string, callback func(text string) error) error {
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
@ -183,11 +183,11 @@ func ReadLines(file string, callback func(text string) error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadLinesBytes reads file content line by line, which is passed to the callback function <callback> as []byte.
|
||||
// ReadLinesBytes reads file content line by line, which is passed to the callback function `callback` as []byte.
|
||||
// It matches each line of text, separated by chars '\r' or '\n', stripped any trailing end-of-line marker.
|
||||
//
|
||||
// Note that the parameter passed to callback function might be an empty value, and the last non-empty line
|
||||
// will be passed to callback function <callback> even if it has no newline marker.
|
||||
// will be passed to callback function `callback` even if it has no newline marker.
|
||||
func ReadLinesBytes(file string, callback func(bytes []byte) error) error {
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
|
@ -15,9 +15,9 @@ import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Copy file/directory from <src> to <dst>.
|
||||
// Copy file/directory from `src` to `dst`.
|
||||
//
|
||||
// If <src> is file, it calls CopyFile to implements copy feature,
|
||||
// If `src` is file, it calls CopyFile to implements copy feature,
|
||||
// or else it calls CopyDir.
|
||||
func Copy(src string, dst string) error {
|
||||
if src == "" {
|
||||
@ -32,8 +32,8 @@ func Copy(src string, dst string) error {
|
||||
return CopyDir(src, dst)
|
||||
}
|
||||
|
||||
// CopyFile copies the contents of the file named <src> to the file named
|
||||
// by <dst>. The file will be created if it does not exist. If the
|
||||
// CopyFile copies the contents of the file named `src` to the file named
|
||||
// by `dst`. The file will be created if it does not exist. If the
|
||||
// destination file exists, all it's contents will be replaced by the contents
|
||||
// of the source file. The file mode will be copied from the source and
|
||||
// the copied data is synced/flushed to stable storage.
|
||||
|
@ -18,7 +18,7 @@ import (
|
||||
)
|
||||
|
||||
// Home returns absolute path of current user's home directory.
|
||||
// The optional parameter <names> specifies the its sub-folders/sub-files,
|
||||
// The optional parameter `names` specifies the its sub-folders/sub-files,
|
||||
// which will be joined with current system separator and returned with the path.
|
||||
func Home(names ...string) (string, error) {
|
||||
path, err := getHomePath()
|
||||
|
@ -10,12 +10,12 @@ import (
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
// ReplaceFile replaces content for file <path>.
|
||||
// ReplaceFile replaces content for file `path`.
|
||||
func ReplaceFile(search, replace, path string) error {
|
||||
return PutContents(path, gstr.Replace(GetContents(path), search, replace))
|
||||
}
|
||||
|
||||
// ReplaceFileFunc replaces content for file <path> with callback function <f>.
|
||||
// ReplaceFileFunc replaces content for file `path` with callback function `f`.
|
||||
func ReplaceFileFunc(f func(path, content string) string, path string) error {
|
||||
data := GetContents(path)
|
||||
result := f(path, data)
|
||||
@ -25,9 +25,9 @@ func ReplaceFileFunc(f func(path, content string) string, path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReplaceDir replaces content for files under <path>.
|
||||
// The parameter <pattern> specifies the file pattern which matches to be replaced.
|
||||
// It does replacement recursively if given parameter <recursive> is true.
|
||||
// ReplaceDir replaces content for files under `path`.
|
||||
// The parameter `pattern` specifies the file pattern which matches to be replaced.
|
||||
// It does replacement recursively if given parameter `recursive` is true.
|
||||
func ReplaceDir(search, replace, path, pattern string, recursive ...bool) error {
|
||||
files, err := ScanDirFile(path, pattern, recursive...)
|
||||
if err != nil {
|
||||
@ -41,9 +41,9 @@ func ReplaceDir(search, replace, path, pattern string, recursive ...bool) error
|
||||
return err
|
||||
}
|
||||
|
||||
// ReplaceDirFunc replaces content for files under <path> with callback function <f>.
|
||||
// The parameter <pattern> specifies the file pattern which matches to be replaced.
|
||||
// It does replacement recursively if given parameter <recursive> is true.
|
||||
// ReplaceDirFunc replaces content for files under `path` with callback function `f`.
|
||||
// The parameter `pattern` specifies the file pattern which matches to be replaced.
|
||||
// It does replacement recursively if given parameter `recursive` is true.
|
||||
func ReplaceDirFunc(f func(path, content string) string, path, pattern string, recursive ...bool) error {
|
||||
files, err := ScanDirFile(path, pattern, recursive...)
|
||||
if err != nil {
|
||||
|
@ -20,10 +20,10 @@ const (
|
||||
maxScanDepth = 100000
|
||||
)
|
||||
|
||||
// ScanDir returns all sub-files with absolute paths of given <path>,
|
||||
// It scans directory recursively if given parameter <recursive> is true.
|
||||
// ScanDir returns all sub-files with absolute paths of given `path`,
|
||||
// It scans directory recursively if given parameter `recursive` is true.
|
||||
//
|
||||
// The pattern parameter <pattern> supports multiple file name patterns,
|
||||
// The pattern parameter `pattern` supports multiple file name patterns,
|
||||
// using the ',' symbol to separate multiple patterns.
|
||||
func ScanDir(path string, pattern string, recursive ...bool) ([]string, error) {
|
||||
isRecursive := false
|
||||
@ -40,18 +40,18 @@ func ScanDir(path string, pattern string, recursive ...bool) ([]string, error) {
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// ScanDirFunc returns all sub-files with absolute paths of given <path>,
|
||||
// It scans directory recursively if given parameter <recursive> is true.
|
||||
// ScanDirFunc returns all sub-files with absolute paths of given `path`,
|
||||
// It scans directory recursively if given parameter `recursive` is true.
|
||||
//
|
||||
// The pattern parameter <pattern> supports multiple file name patterns, using the ','
|
||||
// The pattern parameter `pattern` supports multiple file name patterns, using the ','
|
||||
// symbol to separate multiple patterns.
|
||||
//
|
||||
// The parameter <recursive> specifies whether scanning the <path> recursively, which
|
||||
// The parameter `recursive` specifies whether scanning the `path` recursively, which
|
||||
// means it scans its sub-files and appends the files path to result array if the sub-file
|
||||
// is also a folder. It is false in default.
|
||||
//
|
||||
// The parameter <handler> specifies the callback function handling each sub-file path of
|
||||
// the <path> and its sub-folders. It ignores the sub-file path if <handler> returns an empty
|
||||
// The parameter `handler` specifies the callback function handling each sub-file path of
|
||||
// the `path` and its sub-folders. It ignores the sub-file path if `handler` returns an empty
|
||||
// string, or else it appends the sub-file path to result slice.
|
||||
func ScanDirFunc(path string, pattern string, recursive bool, handler func(path string) string) ([]string, error) {
|
||||
list, err := doScanDir(0, path, pattern, recursive, handler)
|
||||
@ -64,10 +64,10 @@ func ScanDirFunc(path string, pattern string, recursive bool, handler func(path
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// ScanDirFile returns all sub-files with absolute paths of given <path>,
|
||||
// It scans directory recursively if given parameter <recursive> is true.
|
||||
// ScanDirFile returns all sub-files with absolute paths of given `path`,
|
||||
// It scans directory recursively if given parameter `recursive` is true.
|
||||
//
|
||||
// The pattern parameter <pattern> supports multiple file name patterns,
|
||||
// The pattern parameter `pattern` supports multiple file name patterns,
|
||||
// using the ',' symbol to separate multiple patterns.
|
||||
//
|
||||
// Note that it returns only files, exclusive of directories.
|
||||
@ -91,21 +91,21 @@ func ScanDirFile(path string, pattern string, recursive ...bool) ([]string, erro
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// ScanDirFileFunc returns all sub-files with absolute paths of given <path>,
|
||||
// It scans directory recursively if given parameter <recursive> is true.
|
||||
// ScanDirFileFunc returns all sub-files with absolute paths of given `path`,
|
||||
// It scans directory recursively if given parameter `recursive` is true.
|
||||
//
|
||||
// The pattern parameter <pattern> supports multiple file name patterns, using the ','
|
||||
// The pattern parameter `pattern` supports multiple file name patterns, using the ','
|
||||
// symbol to separate multiple patterns.
|
||||
//
|
||||
// The parameter <recursive> specifies whether scanning the <path> recursively, which
|
||||
// The parameter `recursive` specifies whether scanning the `path` recursively, which
|
||||
// means it scans its sub-files and appends the files path to result array if the sub-file
|
||||
// is also a folder. It is false in default.
|
||||
//
|
||||
// The parameter <handler> specifies the callback function handling each sub-file path of
|
||||
// the <path> and its sub-folders. It ignores the sub-file path if <handler> returns an empty
|
||||
// The parameter `handler` specifies the callback function handling each sub-file path of
|
||||
// the `path` and its sub-folders. It ignores the sub-file path if `handler` returns an empty
|
||||
// string, or else it appends the sub-file path to result slice.
|
||||
//
|
||||
// Note that the parameter <path> for <handler> is not a directory but a file.
|
||||
// Note that the parameter `path` for `handler` is not a directory but a file.
|
||||
// It returns only files, exclusive of directories.
|
||||
func ScanDirFileFunc(path string, pattern string, recursive bool, handler func(path string) string) ([]string, error) {
|
||||
list, err := doScanDir(0, path, pattern, recursive, func(path string) string {
|
||||
@ -126,15 +126,15 @@ func ScanDirFileFunc(path string, pattern string, recursive bool, handler func(p
|
||||
// doScanDir is an internal method which scans directory and returns the absolute path
|
||||
// list of files that are not sorted.
|
||||
//
|
||||
// The pattern parameter <pattern> supports multiple file name patterns, using the ','
|
||||
// The pattern parameter `pattern` supports multiple file name patterns, using the ','
|
||||
// symbol to separate multiple patterns.
|
||||
//
|
||||
// The parameter <recursive> specifies whether scanning the <path> recursively, which
|
||||
// The parameter `recursive` specifies whether scanning the `path` recursively, which
|
||||
// means it scans its sub-files and appends the files path to result array if the sub-file
|
||||
// is also a folder. It is false in default.
|
||||
//
|
||||
// The parameter <handler> specifies the callback function handling each sub-file path of
|
||||
// the <path> and its sub-folders. It ignores the sub-file path if <handler> returns an empty
|
||||
// The parameter `handler` specifies the callback function handling each sub-file path of
|
||||
// the `path` and its sub-folders. It ignores the sub-file path if `handler` returns an empty
|
||||
// string, or else it appends the sub-file path to result slice.
|
||||
func doScanDir(depth int, path string, pattern string, recursive bool, handler func(path string) string) ([]string, error) {
|
||||
if depth >= maxScanDepth {
|
||||
|
@ -15,9 +15,9 @@ import (
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
)
|
||||
|
||||
// Search searches file by name <name> in following paths with priority:
|
||||
// Search searches file by name `name` in following paths with priority:
|
||||
// prioritySearchPaths, Pwd()、SelfDir()、MainPkgPath().
|
||||
// It returns the absolute file path of <name> if found, or en empty string if not found.
|
||||
// It returns the absolute file path of `name` if found, or en empty string if not found.
|
||||
func Search(name string, prioritySearchPaths ...string) (realPath string, err error) {
|
||||
// Check if it's a absolute path.
|
||||
realPath = RealPath(name)
|
||||
|
@ -13,7 +13,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Size returns the size of file specified by <path> in byte.
|
||||
// Size returns the size of file specified by `path` in byte.
|
||||
func Size(path string) int64 {
|
||||
s, e := os.Stat(path)
|
||||
if e != nil {
|
||||
@ -22,12 +22,12 @@ func Size(path string) int64 {
|
||||
return s.Size()
|
||||
}
|
||||
|
||||
// SizeFormat returns the size of file specified by <path> in format string.
|
||||
// SizeFormat returns the size of file specified by `path` in format string.
|
||||
func SizeFormat(path string) string {
|
||||
return FormatSize(Size(path))
|
||||
}
|
||||
|
||||
// ReadableSize formats size of file given by <path>, for more human readable.
|
||||
// ReadableSize formats size of file given by `path`, for more human readable.
|
||||
func ReadableSize(path string) string {
|
||||
return FormatSize(Size(path))
|
||||
}
|
||||
@ -74,7 +74,7 @@ func StrToSize(sizeStr string) int64 {
|
||||
return -1
|
||||
}
|
||||
|
||||
// FormatSize formats size <raw> for more human readable.
|
||||
// FormatSize formats size `raw` for more human readable.
|
||||
func FormatSize(raw int64) string {
|
||||
var r float64 = float64(raw)
|
||||
var t float64 = 1024
|
||||
|
@ -14,7 +14,7 @@ import (
|
||||
|
||||
// fileSortFunc is the comparison function for files.
|
||||
// It sorts the array in order of: directory -> file.
|
||||
// If <path1> and <path2> are the same type, it then sorts them as strings.
|
||||
// If `path1` and `path2` are the same type, it then sorts them as strings.
|
||||
func fileSortFunc(path1, path2 string) int {
|
||||
isDirPath1 := IsDir(path1)
|
||||
isDirPath2 := IsDir(path2)
|
||||
@ -31,8 +31,8 @@ func fileSortFunc(path1, path2 string) int {
|
||||
}
|
||||
}
|
||||
|
||||
// SortFiles sorts the <files> in order of: directory -> file.
|
||||
// Note that the item of <files> should be absolute path.
|
||||
// SortFiles sorts the `files` in order of: directory -> file.
|
||||
// Note that the item of `files` should be absolute path.
|
||||
func SortFiles(files []string) []string {
|
||||
array := garray.NewSortedStrArrayComparator(fileSortFunc)
|
||||
array.Add(files...)
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// MTime returns the modification time of file given by <path> in second.
|
||||
// MTime returns the modification time of file given by `path` in second.
|
||||
func MTime(path string) time.Time {
|
||||
s, e := os.Stat(path)
|
||||
if e != nil {
|
||||
@ -20,7 +20,7 @@ func MTime(path string) time.Time {
|
||||
return s.ModTime()
|
||||
}
|
||||
|
||||
// MTimestamp returns the modification time of file given by <path> in second.
|
||||
// MTimestamp returns the modification time of file given by `path` in second.
|
||||
func MTimestamp(path string) int64 {
|
||||
mtime := MTime(path)
|
||||
if mtime.IsZero() {
|
||||
@ -29,7 +29,7 @@ func MTimestamp(path string) int64 {
|
||||
return mtime.Unix()
|
||||
}
|
||||
|
||||
// MTimestampMilli returns the modification time of file given by <path> in millisecond.
|
||||
// MTimestampMilli returns the modification time of file given by `path` in millisecond.
|
||||
func MTimestampMilli(path string) int64 {
|
||||
mtime := MTime(path)
|
||||
if mtime.IsZero() {
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user