Documentation ¶
Index ¶
- Constants
- func CheckBool(v interface{}) interface{}
- func CheckFloat(value interface{}) interface{}
- func CheckInt(value interface{}) interface{}
- func CheckObject(value interface{}) interface{}
- func CheckString(value interface{}) interface{}
- func CheckUint(value interface{}) interface{}
- func EmptyValue(kind uint) interface{}
- func GenUniqueID() string
- func GetString(s interface{}) string
- func GetStringD(s interface{}, d string) string
- func Init(cfg *GlobalConfig, processors *[]Processor) error
- func IsEmpty(value interface{}, kind uint) bool
- func IsEmptyArray(value interface{}) bool
- func IsEmptyBool(value interface{}) bool
- func IsEmptyNumber(value interface{}) bool
- func IsEmptyObject(value interface{}) bool
- func IsEmptyString(value interface{}) bool
- func ParseKindArray(value []interface{}, kind uint) interface{}
- func ParseKindMap(value map[string]interface{}, kind uint) interface{}
- func ParseKindValue(value interface{}, kind uint) interface{}
- func RandString(n int) string
- func Register(method, pattern string, h Handler)
- func RemoveDupArray(s []string) []string
- type ExtLog
- type Field
- type FieldSet
- func (fs *FieldSet) BuildAllObj(all map[string]interface{}, cond map[string]interface{}) error
- func (fs *FieldSet) BuildFilterObj(filter map[string]interface{}, cond map[string]interface{}) error
- func (fs *FieldSet) BuildInObj(in map[string]interface{}, cond map[string]interface{}) error
- func (fs *FieldSet) BuildNinObj(nin map[string]interface{}, cond map[string]interface{}) error
- func (fs *FieldSet) BuildOrObj(or []interface{}, cond map[string]interface{}) error
- func (fs *FieldSet) BuildOrderArray(order []string, sort *bson.D) error
- func (fs *FieldSet) BuildRangeObj(rang map[string]interface{}, cond map[string]interface{}) error
- func (fs *FieldSet) BuildRegexSearchObj(search string, regexSearchFields []string, cond map[string]interface{}) error
- func (fs *FieldSet) BuildSearchContent(obj map[string]interface{}, fields []string) string
- func (fs *FieldSet) BuildSelectObj(slice []string, sel map[string]interface{}) error
- func (fs *FieldSet) CheckIndexFields(fields []string) ([]string, error)
- func (fs *FieldSet) CheckObject(obj map[string]interface{}, dotOk bool) error
- func (fs *FieldSet) CheckRegexSearchFields(fields []string) error
- func (fs *FieldSet) CheckSearchFields(fields []string) error
- func (fs *FieldSet) InReplace(value *map[string]interface{})
- func (fs *FieldSet) InSort(data *map[string]interface{}) bson.D
- func (fs *FieldSet) IsFieldCreateOnly(field string) bool
- func (fs *FieldSet) IsFieldMember(field string) (uint, bool)
- func (fs *FieldSet) IsFieldReadOnly(field string) bool
- func (fs *FieldSet) IsMapMember(field string) (uint, bool)
- func (fs *FieldSet) OrderArray2Slice(sort *bson.D) []string
- func (fs *FieldSet) OutReplace(value *map[string]interface{})
- func (fs *FieldSet) OutReplaceArray(values []interface{})
- func (fs *FieldSet) ParseSimpleArray(value interface{}, kind uint) interface{}
- func (fs *FieldSet) ParseSimpleValue(value interface{}, kind uint) interface{}
- func (fs *FieldSet) SetCreateOnlyFields(fields []string)
- func (fs *FieldSet) SetReadOnlyFields(fields []string)
- type GlobalConfig
- type Handler
- type Index
- type IndexEnsureList
- type IndexEnsuredMap
- type IndexToEnsureStruct
- type Logger
- type Processor
- type Rsp
- type RspGetPageData
- type SearchResponse
Constants ¶
const ( KindInvalid = uint(reflect.Invalid) KindBool = uint(reflect.Bool) KindInt = uint(reflect.Int64) KindUint = uint(reflect.Uint64) KindFloat = uint(reflect.Float64) KindString = uint(reflect.String) KindObject = uint(reflect.Struct) KindSimpleEnd = uint(999) KindArrayBase = uint(1000) KindArrayBool = KindArrayBase + KindBool KindArrayInt = KindArrayBase + KindInt KindArrayUint = KindArrayBase + KindUint KindArrayFloat = KindArrayBase + KindFloat KindArrayString = KindArrayBase + KindString KindArrayObject = KindArrayBase + KindObject KindArrayEnd = uint(1999) KindMapBase = uint(2000) KindMapBool = KindMapBase + KindBool KindMapInt = KindMapBase + KindInt KindMapUint = KindMapBase + KindUint KindMapFloat = KindMapBase + KindFloat KindMapString = KindMapBase + KindString KindMapObject = KindMapBase + KindObject KindMapEnd = uint(2999) )
A set of supported Field Kind
Variables ¶
This section is empty.
Functions ¶
func CheckBool ¶
func CheckBool(v interface{}) interface{}
CheckBool check v type if v is BOOL, return v if v is not BOOL, return nil
func CheckFloat ¶
func CheckFloat(value interface{}) interface{}
CheckFloat check value type if value is any type represent FLOAT, return FLOAT64 value if value is not any type represent FLOAT, return nil
func CheckInt ¶
func CheckInt(value interface{}) interface{}
CheckInt check value type if value is any type represent INT, return INT64 value if value is not any type represent INT, return nil
func CheckObject ¶
func CheckObject(value interface{}) interface{}
CheckObject check value type if value is OBJECT, return its value if value is not OBJECT, return nil
func CheckString ¶
func CheckString(value interface{}) interface{}
CheckString check value type if value is any type represent STRING, return STRING value if value is not any type represent STRING, return nil
func CheckUint ¶
func CheckUint(value interface{}) interface{}
CheckUint check value type if value is any type represent UINT, return UINT64 value if value is not any type represent UINT, return nil
func GenUniqueID ¶
func GenUniqueID() string
GenUniqueID is an function to gen a unique id with STRING type support objectid or uuid
func GetString ¶
func GetString(s interface{}) string
GetString check s type if s is String, return its value if s is not STRING, return empty string
func GetStringD ¶
GetStringD check s type if s is String, return its value if s is not STRING, return default d
func Init ¶
func Init(cfg *GlobalConfig, processors *[]Processor) error
Init is a function to init restful service
func IsEmptyArray ¶
func IsEmptyArray(value interface{}) bool
IsEmptyArray check whether value is empty if value is nil or empty array, return true
func IsEmptyBool ¶
func IsEmptyBool(value interface{}) bool
IsEmptyBool check whether value is empty if value is nil or default value of bool, return true
func IsEmptyNumber ¶
func IsEmptyNumber(value interface{}) bool
IsEmptyNumber check whether value is empty if value is nil or default value of float64, return true
func IsEmptyObject ¶
func IsEmptyObject(value interface{}) bool
IsEmptyObject check whether value is empty if value is nil or empty object, return true
func IsEmptyString ¶
func IsEmptyString(value interface{}) bool
IsEmptyString check whether value is empty if value is nil or default value of string, return true
func ParseKindArray ¶
func ParseKindArray(value []interface{}, kind uint) interface{}
ParseKindArray parse all array kind of value
func ParseKindMap ¶
ParseKindMap parse map kind of value
func ParseKindValue ¶
func ParseKindValue(value interface{}, kind uint) interface{}
ParseKindValue parse all kind of value
func RemoveDupArray ¶
RemoveDupArray remove duplicate elements
Types ¶
type ExtLog ¶
ExtLog is the default Logger
func (*ExtLog) Debugln ¶
func (l *ExtLog) Debugln(v ...interface{})
Debugln prints debug log with a newline appended
func (*ExtLog) Fatalln ¶
func (l *ExtLog) Fatalln(v ...interface{})
Fatalln prints fatal log with new line
type Field ¶
type Field struct { Kind uint // field's kind CreateOnly bool // field can only be written when creating by POST or PUT ReadOnly bool // field can not be written or update, data should be loaded into DB by other ways }
Field definition
type FieldSet ¶
FieldSet is a structure to store DataStruct fields parsing result
func BuildFieldSet ¶
BuildFieldSet is a function to parsing the DataStruct
func (*FieldSet) BuildAllObj ¶
BuildAllObj build the condition of `all` filter
func (*FieldSet) BuildFilterObj ¶
func (fs *FieldSet) BuildFilterObj(filter map[string]interface{}, cond map[string]interface{}) error
BuildFilterObj build the condition like `WHERE f1 = xxx AND ...` in SQL
func (*FieldSet) BuildInObj ¶
BuildInObj build the condition of `in` filter
func (*FieldSet) BuildNinObj ¶
BuildNinObj build the condition of `nin` filter
func (*FieldSet) BuildOrObj ¶
BuildOrObj build the condition of `or` filter
func (*FieldSet) BuildOrderArray ¶
BuildOrderArray build sort
func (*FieldSet) BuildRangeObj ¶
BuildRangeObj build the condition of `range` filter
func (*FieldSet) BuildRegexSearchObj ¶
func (fs *FieldSet) BuildRegexSearchObj(search string, regexSearchFields []string, cond map[string]interface{}) error
BuildRegexSearchObj build the condition of `regex search` filter
func (*FieldSet) BuildSearchContent ¶
BuildSearchContent concat the text according to the search fields
func (*FieldSet) BuildSelectObj ¶
BuildSelectObj build the select fields
func (*FieldSet) CheckIndexFields ¶
CheckIndexFields check the index in the config of Processor valid or not
func (*FieldSet) CheckObject ¶
CheckObject check obj is valid or not
func (*FieldSet) CheckRegexSearchFields ¶
CheckRegexSearchFields check the search fields in the config of Processor valid or not
func (*FieldSet) CheckSearchFields ¶
CheckSearchFields check the search fields in the config of Processor valid or not
func (*FieldSet) IsFieldCreateOnly ¶
IsFieldCreateOnly check field is create only or not
func (*FieldSet) IsFieldMember ¶
IsFieldMember check field is a member of Struct or not
func (*FieldSet) IsFieldReadOnly ¶
IsFieldReadOnly check field is read only or not
func (*FieldSet) IsMapMember ¶
IsMapMember check field is a member of Struct map field or not
func (*FieldSet) OrderArray2Slice ¶
OrderArray2Slice convert the sort array to string slice
func (*FieldSet) OutReplace ¶
OutReplace adapted MongoDB '_id' field
func (*FieldSet) OutReplaceArray ¶
func (fs *FieldSet) OutReplaceArray(values []interface{})
OutReplaceArray adapted MongoDB '_id' field for ARRAY
func (*FieldSet) ParseSimpleArray ¶
ParseSimpleArray parse a simple array kind of value
func (*FieldSet) ParseSimpleValue ¶
ParseSimpleValue parse a simple kind of value
func (*FieldSet) SetCreateOnlyFields ¶
SetCreateOnlyFields set the fields create only
func (*FieldSet) SetReadOnlyFields ¶
SetReadOnlyFields set the fields read only
type GlobalConfig ¶
type GlobalConfig struct { Mux *mux.Router // gorilla/mux MgoSess *mgo.Session // mongodb session DefaultDbName string // default db name, using "restful" if not setting DefaultIdGenerator string // default id gnerator, objectid or uuid, using objectid if not setting EsEnable bool // enable es for search EsUrl string // es url, default: http://127.0.0.1:9200 EsUser string // es username EsPwd string // es password EsIndex string // es index, default: restful EsAnalyzer string // default: ik_max_word EsSearchAnalyzer string // default: ik_max_word }
GlobalConfig is a config to init restful service
type Index ¶
type Index struct { Key []string // Index key fields; prefix name with dash (-) for descending order Unique bool // Prevent two documents from having the same index key }
Index describes the definition of an index
type IndexEnsureList ¶
IndexEnsureList is a structure describes a list of indices to ensure
func (*IndexEnsureList) Init ¶
func (l *IndexEnsureList) Init() *IndexEnsureList
Init init the IndexEnsureList
func (*IndexEnsureList) Pop ¶
func (l *IndexEnsureList) Pop() *IndexToEnsureStruct
Pop pop an index out of IndexEnsureList
func (*IndexEnsureList) Push ¶
func (l *IndexEnsureList) Push(idx *IndexToEnsureStruct)
Push push an index into IndexEnsureList
type IndexEnsuredMap ¶
IndexEnsuredMap cache to store index that has been ensured
func (*IndexEnsuredMap) Exist ¶
func (s *IndexEnsuredMap) Exist(k string) bool
Exist check whether an index exists or not
func (*IndexEnsuredMap) Set ¶
func (s *IndexEnsuredMap) Set(k string)
Set add an index into the cache
type IndexToEnsureStruct ¶
IndexToEnsureStruct defines where and how to create the index
type Logger ¶
type Logger interface { Debugf(format string, v ...interface{}) Debugln(v ...interface{}) Warnf(format string, v ...interface{}) Warnln(v ...interface{}) Fatalf(format string, v ...interface{}) Fatalln(v ...interface{}) }
Logger is an interface for logging
var Log Logger
Log is a global log handle you can reassign Log to your own Logger which contains the methods Logger interface contains
type Processor ¶
type Processor struct { // Business name, functions: // 1. default value of TableName // 2. default value of URLPath // 3. logs Biz string // Table name, using ${Biz} if empty TableName string // URL Path as service, usually equal to Biz URLPath string // for fields type parsing DataStruct interface{} // fields for search // to use the search feature, you must enable GlobalConfig.EsEnable // field's type must be string or []string SearchFields []string // fields for search implemented by db regex RegexSearchFields []string // fields CreateOnly // fields can only be written when creating by POST or PUT CreateOnlyFields []string // fields ReadOnly // fields can not be written or update, data should be loaded into DB by other ways ReadOnlyFields []string // indexes will be created in database/table Indexes []Index // fields type and R/W config FieldSet *FieldSet // CURD handler PostHandler Handler PutHandler Handler PatchHandler Handler GetHandler Handler GetPageHandler Handler DeleteHandler Handler TriggerHandler Handler // Do something after data write success // 1. update search data to es OnWriteDone func(method string, vars map[string]string, query url.Values, data map[string]interface{}) // specify db and table name from URL Query // e.g.: /path?db=dbName&table=tableName // default db name: restful // default table name: ${TableName} GetDbName func(query url.Values) string GetTableName func(query url.Values) string }
Processor is a set of configurations and handlers of a Restful resource
type Rsp ¶
type Rsp struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data,omitempty"` }
Rsp is a general returning structure for all request
type RspGetPageData ¶
type RspGetPageData struct { Total int64 `json:"total"` Hits []interface{} `json:"hits"` }
RspGetPageData is a general returning structure in `data` field for GetPage request
type SearchResponse ¶
type SearchResponse struct { Result string `json:"result"` Error struct { Type string `json:"type"` Reason string `json:"reason"` } `json:"error"` Hits struct { Total int64 `json:"total"` Hits []struct { ID string `json:"_id"` Source struct { Db string `json:"db"` Table string `json:"table"` Content string `json:"content"` } `json:"_source"` } `json:"hits"` } `json:"hits"` }
SearchResponse is the rsp structure of es