go_ohm

package module
v0.0.0-...-c0c5f26 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2022 License: MIT Imports: 6 Imported by: 0

README

go_ohm

Object to redis hash mapping. Use 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 Go Reference to get more information.

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

View Source
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.

func Save

func Save(conn redis.Conn, ns string, opts *ObjectOptions, i interface{}) error

Save data struct to redis hash. See Load() for argument explanation.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL