Documentation
¶
Index ¶
- func WithDefaultLang(li language.Index) func(o *Option)
- func WithNameSorting() func(o *Option)
- func WithPKSorting() func(o *Option)
- func WithTable(tableName string) func(o *Option)
- type Book
- func (b *Book[T]) Cache(db *dbw.DB) error
- func (b *Book[T]) Compile() error
- func (rb *Book[T]) Exist(id int) bool
- func (b *Book[T]) Item(id int) (T, bool)
- func (rb *Book[T]) ItemFn(id int, fn func(T)) bool
- func (rb *Book[T]) ItemFnPtr(id int, fn func(*T)) bool
- func (b *Book[T]) ItemPtr(id int) *T
- func (rb *Book[T]) JSON() []byte
- func (rb *Book[T]) JSONWithHash() []byte
- func (b *Book[T]) Parse(data []byte) error
- func (b *Book[T]) Text(id int) string
- func (b *Book[T]) TextWithDefault(id int, notFoundResult string) string
- func (rb *Book[T]) Traverse(fn func(T))
- func (rb *Book[T]) TraversePtr(fn func(*T))
- func (rb *Book[T]) TraversePtrWithBreak(fn func(*T) (stop bool))
- type BookShelf
- type Booker
- type Item
- type MultiLangBook
- func (b *MultiLangBook[T]) Cache(db *dbw.DB) error
- func (b *MultiLangBook[T]) Compile() error
- func (b *MultiLangBook[T]) Exist(id int) bool
- func (b *MultiLangBook[T]) Item(id int) (T, bool)
- func (b *MultiLangBook[T]) ItemFn(id int, fn func(T)) bool
- func (b *MultiLangBook[T]) ItemFnPtr(id int, fn func(*T)) bool
- func (b *MultiLangBook[T]) ItemPtr(id int) *T
- func (b *MultiLangBook[T]) JSON(li language.Index) []byte
- func (b *MultiLangBook[T]) JSONWithHash(li language.Index) []byte
- func (b *MultiLangBook[T]) Parse(data []byte) error
- func (b *MultiLangBook[T]) Text(id int, li language.Index) string
- func (b *MultiLangBook[T]) Text2(id int, primary, secondary language.Index) string
- func (b *MultiLangBook[T]) Traverse(fn func(T))
- func (b *MultiLangBook[T]) TraversePtr(fn func(*T))
- func (b *MultiLangBook[T]) TraversePtrWithBreak(fn func(*T) (stop bool))
- type MultiLangItem
- type MultiLangNamer
- type Namer
- type Option
- type PK
- type SortMethod
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithDefaultLang ¶
func WithNameSorting ¶
func WithNameSorting() func(o *Option)
func WithPKSorting ¶
func WithPKSorting() func(o *Option)
Types ¶
type Book ¶
type Book[T Item] struct { // contains filtered or unexported fields }
Book implements a functionality of managing reference book which has a single language attribute Name.
Example ¶
package main
import (
"fmt"
"github.com/axkit/rb"
)
type CustomerType struct {
ID int `json:"id"`
Name string `json:"name"`
IsIndividual bool `json:"isIndividual"`
}
func (ct CustomerType) PK() int {
return ct.ID
}
func (ct CustomerType) NameValue() string {
return ct.Name
}
func main() {
b := rb.NewBook[CustomerType](rb.WithNameSorting())
_ = b.Parse([]byte(`[{"id":1,"name":"Individual","isIndividual":true},{"id":2,"name":"Legal entity","isIndividual":false}]`))
fmt.Println(b.Text(1))
}
Output: Individual
func (*Book[T]) ItemFn ¶
ItemFn finds the item by it's ID and calls the function fn with the item as an argument.
func (*Book[T]) ItemFnPtr ¶
ItemFnPtr finds the item by it's ID and calls the function fn with the reference to the item as an argument.
func (*Book[T]) JSONWithHash ¶
JSONWithHash returns the JSON representation of the Book. As an object with two fields: "hash" and "items".
func (*Book[T]) Parse ¶
Parse parses the data from the JSON and initializes the Book. It resets the Book before applying the new data. The data must be an array of objects. The objects must at least have the fields "id" and "name".
func (*Book[T]) Text ¶
Text returns the value of the item by it's ID. Returns empty string if the item is not found.
func (*Book[T]) TextWithDefault ¶
TextWithDefault returns the value of the item by it's ID. Returns notFoundResult if the item is not found.
func (*Book[T]) Traverse ¶
func (rb *Book[T]) Traverse(fn func(T))
Traverse traverses the Book items and calls the function fn with each item as an argument.
func (*Book[T]) TraversePtr ¶
func (rb *Book[T]) TraversePtr(fn func(*T))
Traverse traverses the Book items and calls the function fn with reference to each item as an argument.
func (*Book[T]) TraversePtrWithBreak ¶
TraversePtrWithBreak traverses the Book items and calls the function fn with reference to each item as an argument. If the function fn returns true the traversing is stopped.
type BookShelf ¶
type BookShelf[T Booker] struct { // contains filtered or unexported fields }
func NewBookShelf ¶
type MultiLangBook ¶
type MultiLangBook[T MultiLangItem] struct { // contains filtered or unexported fields }
func NewMultiLangBook ¶
func NewMultiLangBook[T MultiLangItem](fn ...func(*Option)) *MultiLangBook[T]
func (*MultiLangBook[T]) Cache ¶
func (b *MultiLangBook[T]) Cache(db *dbw.DB) error
Cache reads the data from the database table and initializes the Book.
func (*MultiLangBook[T]) Compile ¶
func (b *MultiLangBook[T]) Compile() error
func (*MultiLangBook[T]) Exist ¶
func (b *MultiLangBook[T]) Exist(id int) bool
func (*MultiLangBook[T]) Item ¶
func (b *MultiLangBook[T]) Item(id int) (T, bool)
func (*MultiLangBook[T]) ItemFn ¶
func (b *MultiLangBook[T]) ItemFn(id int, fn func(T)) bool
func (*MultiLangBook[T]) ItemFnPtr ¶
func (b *MultiLangBook[T]) ItemFnPtr(id int, fn func(*T)) bool
func (*MultiLangBook[T]) ItemPtr ¶
func (b *MultiLangBook[T]) ItemPtr(id int) *T
func (*MultiLangBook[T]) JSONWithHash ¶
func (b *MultiLangBook[T]) JSONWithHash(li language.Index) []byte
func (*MultiLangBook[T]) Parse ¶
func (b *MultiLangBook[T]) Parse(data []byte) error
func (*MultiLangBook[T]) Text2 ¶
func (b *MultiLangBook[T]) Text2(id int, primary, secondary language.Index) string
func (*MultiLangBook[T]) Traverse ¶
func (b *MultiLangBook[T]) Traverse(fn func(T))
func (*MultiLangBook[T]) TraversePtr ¶
func (b *MultiLangBook[T]) TraversePtr(fn func(*T))
func (*MultiLangBook[T]) TraversePtrWithBreak ¶
func (b *MultiLangBook[T]) TraversePtrWithBreak(fn func(*T) (stop bool))
type MultiLangItem ¶
type MultiLangItem interface {
PK
MultiLangNamer
}