rb

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2022 License: MIT Imports: 8 Imported by: 0

README

rb - Reference Book

Generic reference book management package. Requires 1.18+ Alternative implementation of axkit/refbook using Go type parameters available from version 1.18

Example

Single Name Reference Book
type Item struct {
    ID          int     `json:"id"`
    Name        string  `json:"name"
    IsActive    bool    `json:"isActive"
}


func (item Item)PK() int {
    return item.ID
}

func (item Item)NameValue() string {
    return item.Name
}

b := rb.NewBook[Item](rb.WithNameSorting())
err := b.Parse([]byte(`[{"id": 1, "name": "Dog", "isActive": true}, {"id": 2, "name": "Cow", "isActive": true},
{"id": 3, "name":"Cat", "isActive": true}]`))

// or 

var db *dbw.DB
...
b := rb.NewBook[Item](rb.WithTable("items"), rb.WithNameSorting())
err = b.Cache(db)

s := b.Name(1)

Multi-language Name Reference Book
type MultiLangItem struct {
    ID          int             `json:"id"`
    Name        language.Name   `json:"name"
    IsActive    bool            `json:"isActive"
}


func (item MultiLangItem)PK() int {
    return item.ID
}

func (item MultiLangItem)NameValue(li language.Index) string {
    return item.Name.Elem(li)
}

b := rb.NewMultiLangBook[Item](rb.WithNameSorting())
err := b.Parse([]byte(`[{"id": 1, "name": {"en": "Dog", "cz": "Pes"}, "isActive": true}, 
{"id": 2, "name": {"en": "Cow", "cz": "Krava"}, "isActive": true}]`))

// or 

var db *dbw.DB
...
b := rb.NewBook[Item](rb.WithTable("animals"), rb.WithNameSorting())
err = b.Cache(db)

s := b.Name(1, language.ToIndex("en")) // Dog
s = b.Name(1, language.ToIndex("cz")) // Pes

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithDefaultLang

func WithDefaultLang(li language.Index) func(o *Option)

func WithNameSorting

func WithNameSorting() func(o *Option)

func WithPKSorting

func WithPKSorting() func(o *Option)

func WithTable

func WithTable(tableName string) 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 NewBook

func NewBook[T Item](fn ...func(*Option)) *Book[T]

NewBook returns a new single language Book.

func (*Book[T]) Cache

func (b *Book[T]) Cache(db *dbw.DB) error

Cache reads the data from the database table and initializes the Book.

func (*Book[T]) Compile

func (b *Book[T]) Compile() error

Compile compiles the Book.

func (*Book[T]) Exist

func (rb *Book[T]) Exist(id int) bool

Exist returns true if the item with the specified ID exists.

func (*Book[T]) Item

func (b *Book[T]) Item(id int) (T, bool)

Item returns the item by it's ID.

func (*Book[T]) ItemFn

func (rb *Book[T]) ItemFn(id int, fn func(T)) bool

ItemFn finds the item by it's ID and calls the function fn with the item as an argument.

func (*Book[T]) ItemFnPtr

func (rb *Book[T]) ItemFnPtr(id int, fn func(*T)) bool

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]) ItemPtr

func (b *Book[T]) ItemPtr(id int) *T

ItemPtr returns the reference to the item by it's ID.

func (*Book[T]) JSON

func (rb *Book[T]) JSON() []byte

JSON returns the JSON representation of the Book. As array of objects.

func (*Book[T]) JSONWithHash

func (rb *Book[T]) JSONWithHash() []byte

JSONWithHash returns the JSON representation of the Book. As an object with two fields: "hash" and "items".

func (*Book[T]) Parse

func (b *Book[T]) Parse(data []byte) error

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

func (b *Book[T]) Text(id int) string

Text returns the value of the item by it's ID. Returns empty string if the item is not found.

func (*Book[T]) TextWithDefault

func (b *Book[T]) TextWithDefault(id int, notFoundResult string) string

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

func (rb *Book[T]) TraversePtrWithBreak(fn func(*T) (stop bool))

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

func NewBookShelf[T Booker]() *BookShelf[T]

func (*BookShelf[T]) Add

func (bs *BookShelf[T]) Add(name string, b T)

func (*BookShelf[T]) Book

func (bs *BookShelf[T]) Book(name string) *T

func (*BookShelf[T]) Cache

func (bs *BookShelf[T]) Cache(db *dbw.DB) error

func (*BookShelf[T]) Compile

func (bs *BookShelf[T]) Compile() error

func (*BookShelf[T]) Hash

func (bs *BookShelf[T]) Hash() uint64

func (*BookShelf[T]) JSON

func (bs *BookShelf[T]) JSON() []byte

type Booker

type Booker interface {
	Cache(*dbw.DB) error
	Compile() error
	Hash() uint64
	JSON() []byte
	JSONWithHash() []byte
}

type Item

type Item interface {
	PK
	Namer
}

Item is the interface that wraps two interfaces PK and Namer.

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]) JSON

func (b *MultiLangBook[T]) JSON(li language.Index) []byte

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]) Text

func (b *MultiLangBook[T]) Text(id int, li language.Index) string

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
}

type MultiLangNamer

type MultiLangNamer interface {
	NameValue(language.Index) string
}

type Namer

type Namer interface {
	NameValue() string
}

type Option

type Option struct {
	// contains filtered or unexported fields
}

Option holds FlexBook configuration.

type PK

type PK interface {
	PK() int
}

type SortMethod

type SortMethod int
const (
	WithoutSorting SortMethod = iota
	ByName
	ByPK
)

Jump to

Keyboard shortcuts

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