mimir

package module
v0.0.0-...-76f9891 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2017 License: MPL-2.0 Imports: 11 Imported by: 0

README

Report card

Mímir

Mímir logo

Generates code for an embedded database with minimal API from structs in golang.

When you trying to make a little tool that has to save some objects somewhere, it's hard to make an easy to use store quickly. And in existing embedded databases you must handle objects as map[string]interface{}.

That's why Mímir was created! It takes structs from an go file and generates code to store, retrieve and iterate trough collections of objects defined by parsed structs. So you can easily store your actual structs (and not []byte or map[string]interface{}).

Indexing of all builtin types + time.Time supported!

The store operates on top of leveldb. Mímir is a wisdom deity from the Norse mythology.

Contribution of all kind is welcome!

example structs (structs.go):

package main

//Person ...
type Person struct {
	Name, Lastname string
	Age            int `index:"Age"`
	Addresses      []*address
}

type address struct {
	Street     string
	Number     int
	PostalCode string
	City       string `index:"AddressCity"`
}

generate db code: mimir structs.go

Usage:

//Open db file
db, err := OpenDB("/tmp/mimirdb")
if err != nil {
	panic(err)
}
defer db.Close()

//Get the Persons Collection
persons := db.Persons

//Add an person to db
id, err := persons.Add(&Person{
	Name: "Foo",
	Lastname: "Bar",
	Addresses: []*address{&address{Street: "Valhalla", Number: 404, City: "Asgard"}},
})
if err != nil {
    panic(err)
}

//Get the person by id
person, err := persons.Get(id)
if err != nil {
    panic(err)
}

//Update person by id
person.Name = "Meh"
err := persons.Update(id, person)
if err != nil {
    panic(err)
}

//Delete person by id
err := persons.Delete(id)
if err != nil {
    panic(err)
}

//Iterate trough the all persons in the Persons Collection
iter := persons.All()
defer iter.Release() //iter must be released
for iter.Next() {
	p, err := iter.Value()
	if err != nil {
    	panic(err)
	}
	fmt.Println(p.Name, p.Lastname, p.Age)
}

All exported structs get there own collection. The struct tags can be used as index declarations. In the example above Person.Age has an index with name "Age" and address.City has index "AddressCity". All substructs with specified indexes build also an index for the collection above. So "AddressCity" is an index for Persons.

//Iterating through persons with Age in range 30-40 (if passed nil it means -/+ infinity)
fromAge, toAge := 30, 40
iter := db.Persons.AgeRange(&fromAge, &toAge)
defer iter.Release()
for iter.Next() {
	p, err := iter.Value()
	if err != nil {
    	panic(err)
	}
	fmt.Println(p.Name, p.Lastname, p.Age)
}
//Iterating through persons which have city in address equal to "London"
iter := db.Persons.AddressCityEq("London")
defer iter.Release()
for iter.Next() {
	p, err := iter.Value()
	if err != nil {
    	panic(err)
	}
	fmt.Println(p.Name, p.Lastname, p.Age)
}

Documentation

Overview

Package mimir implemenst the AST parsing and code generation

Index

Constants

View Source
const DBTEMPLATE = `` /* 15565-byte string literal not displayed */

DBTEMPLATE the main mart of the db source

Variables

View Source
var (

	//INDEXABLE is an array of type names whitch have supported lexDump
	INDEXABLE = map[string]string{
		"int": "Int", "int8": "Int8", "int16": "Int16", "int32": "Int32", "int64": "Int64",
		"uint": "Uint", "uint8": "Uint8", "uint16": "Uint16", "uint32": "Uint32", "uint64": "Uint64",
		"float32": "Float32", "float64": "Float64",
		"string": "String", "rune": "Rune", "byte": "Byte", "[]rune": "Runes", "[]byte": "Bytes",
		"time.Time": "Time",
	}
)

Functions

This section is empty.

Types

type Attr

type Attr struct {
	Type  string
	Index string
}

Attr is the attribute of a struct, its type and if the attribute will be indexed

func (*Attr) IsIndexable

func (attr *Attr) IsIndexable() bool

IsIndexable returns true if the attribute type has suporting lexDump

type DBGenerator

type DBGenerator struct {
	PackageName string
	Structs     map[string]*Struct
}

DBGenerator represents the structs in file

func Parse

func Parse(filename string) (*DBGenerator, error)

Parse parses the given file and returns the DBGenerator

func (DBGenerator) Generate

func (gen DBGenerator) Generate(w io.Writer) error

Generate generates the db sourcecode

func (*DBGenerator) String

func (gen *DBGenerator) String() string

type Struct

type Struct struct {
	Name     string
	Exported bool
	Attrs    map[string]*Attr
}

Struct is the struct from whitch the collections are generated

Directories

Path Synopsis
test
test1
Package main genereated with github.com/microo8/mimir DO NOT MODIFY!
Package main genereated with github.com/microo8/mimir DO NOT MODIFY!
test2/mypackage
Package db genereated with github.com/microo8/mimir DO NOT MODIFY!
Package db genereated with github.com/microo8/mimir DO NOT MODIFY!

Jump to

Keyboard shortcuts

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