Documentation ¶
Overview ¶
Package go_ohm is for "Object to redis hash" mapping.
It uses go's struct tag to indicate how to map a struct field to a redis hash field. And includes reference directive, which likes SQL's ForeignKey.
See `ObjectOptions` to know all usable struct tags.
Index ¶
- Constants
- func Load(conn redis.Conn, ns string, opts *ObjectOptions, i interface{}) error
- func Save(conn redis.Conn, ns string, opts *ObjectOptions, i interface{}) error
- type ErrorBugOccurred
- type ErrorJsonFailed
- type ErrorObjectWithoutHashKey
- type ErrorRedisCommandFailed
- type ErrorUnsupportedObjectType
- type ObjectOptions
Constants ¶
const ( ObjectOpLoad uint = iota ObjectOpSave )
Variables ¶
This section is empty.
Functions ¶
func Load ¶
func Load(conn redis.Conn, ns string, opts *ObjectOptions, i interface{}) error
Load data struct from redis hash.
`conn` is a redis connection created by external package `redigo`. See https://github.com/gomodule/redigo.
`ns` is namespace to classify hash keys. It is represented as prefix of hash keys.
`opts` specified how to deal with data struct in `i`. See `ObjectOptions`.
`i` is data struct, currently it supports struct pointer, map, and map pointer. The map key must be int, uint or string.
It returns `error` while failed.
Types ¶
type ErrorBugOccurred ¶
type ErrorBugOccurred struct {
// contains filtered or unexported fields
}
type ErrorJsonFailed ¶
type ErrorJsonFailed struct {
// contains filtered or unexported fields
}
type ErrorObjectWithoutHashKey ¶
type ErrorObjectWithoutHashKey struct {
// contains filtered or unexported fields
}
type ErrorRedisCommandFailed ¶
type ErrorRedisCommandFailed struct {
// contains filtered or unexported fields
}
type ErrorUnsupportedObjectType ¶
type ErrorUnsupportedObjectType struct {
// contains filtered or unexported fields
}
type ObjectOptions ¶
type ObjectOptions struct { // Redis hash's name, for map and struct only. Default is field name. HashName string // Redis hash's field, for primitive types and jsonified compound types, // Default is field name. HashField string // Prefix of hash field. Default is field type name. HashPrefix string // Refer to other field. If presented, the hash name is referred field // value. Reference string // Jsonify the struct field, and store as a hash field. This option // corresponded two struct tag options: "json" and "non_json". For compound // types, includes slice(except byte slice), array, map and struct, default // is "json", and for other types default is "non_json". Json bool // Don't Jsonify elements of map. Only for field which type is map. default // is jsonify all types. ElemNonJson bool }
ObjectOptions is options for a struct field. Every struct field is a `Object` internally.
This type exported mainly for customize Load() and Save()'s argument `i`'s behaviors, because `i` is the "root object" and is not a field of certain struct, so can't use struct tag.
Every option has a corresponding struct tag option, in snake case. For instance:
type A struct { A int `go_ohm:"hash_name=a"` }
The struct tag are parsed as a `ObjectOptions` internally, and customized field `A`'s mapping result.