137 lines
2.7 KiB
Go
137 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.bullercodeworks.com/brian/boltease"
|
|
)
|
|
|
|
func main() {
|
|
//example1()
|
|
//fmt.Println()
|
|
example2()
|
|
}
|
|
|
|
func example1() {
|
|
fmt.Println("# Example 1")
|
|
db, err := boltease.Create("example.db", 0600, nil)
|
|
if err != nil {
|
|
fmt.Printf("Error Opening File: %s\n", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Println("## Saving Struct")
|
|
err = db.Save(
|
|
[]string{"examples", "example1"},
|
|
ExampleType{
|
|
Name: "Example 1",
|
|
Age: 5,
|
|
})
|
|
if err != nil {
|
|
fmt.Printf("Error saving struct: %s\n", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Println("## Example 1-1: Simple")
|
|
var v string
|
|
err = db.GetForInterface([]string{"examples", "example1"}, "name", &v)
|
|
if err != nil {
|
|
fmt.Println("Error:", err.Error())
|
|
}
|
|
fmt.Println("Name:", v)
|
|
var age int
|
|
err = db.GetForInterface([]string{"examples", "example1"}, "age", &age)
|
|
if err != nil {
|
|
fmt.Println("Error:", err.Error())
|
|
}
|
|
fmt.Println("Age:", age)
|
|
fmt.Println("")
|
|
|
|
fmt.Println("## Example 1-2: LoadStruct, simple")
|
|
var name string
|
|
err = db.Load(
|
|
[]string{"examples", "example1", "name"},
|
|
&name,
|
|
)
|
|
fmt.Println("Name:", name)
|
|
if err != nil {
|
|
fmt.Println("Err:", err)
|
|
}
|
|
fmt.Println("")
|
|
|
|
fmt.Println("## Example 1-3: Struct")
|
|
fmt.Println("Loading into Struct")
|
|
newStruct := ExampleType{}
|
|
err = db.Load(
|
|
[]string{"examples", "example1"},
|
|
&newStruct,
|
|
)
|
|
fmt.Println(newStruct)
|
|
}
|
|
|
|
func example2() {
|
|
fmt.Println("# Example 2")
|
|
db, err := boltease.Create("example.db", 0600, nil)
|
|
if err != nil {
|
|
fmt.Printf("Error Opening File: %s\n", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("## Saving Struct")
|
|
num := 12345
|
|
err = db.Save(
|
|
[]string{"examples", "example2"},
|
|
&ExampleType2{
|
|
Name: "Example 2",
|
|
Age: 20,
|
|
SubTypePtr: &ExampleSubType{
|
|
SubName: "Example SubType Pointer",
|
|
},
|
|
SubType: ExampleSubType{
|
|
SubName: "Example SubType",
|
|
},
|
|
Number: &num,
|
|
})
|
|
if err != nil {
|
|
fmt.Printf("Error saving struct: %s\n", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
newStruct := ExampleType2{}
|
|
err = db.Load([]string{"examples", "example2"}, &newStruct)
|
|
fmt.Println(newStruct)
|
|
}
|
|
|
|
type ExampleType struct {
|
|
Name string `boltease:"name"`
|
|
Age int `boltease:"age"`
|
|
}
|
|
|
|
func (e ExampleType) String() string {
|
|
r, _ := json.Marshal(e)
|
|
return string(r)
|
|
}
|
|
|
|
type ExampleType2 struct {
|
|
Name string `boltease:"name"`
|
|
Age int `boltease:"age"`
|
|
SubTypePtr *ExampleSubType `boltease:"subtypeptr"`
|
|
SubType ExampleSubType `boltease:"subtype"`
|
|
Number *int `boltease:"number"`
|
|
}
|
|
|
|
func (e ExampleType2) String() string {
|
|
r, _ := json.Marshal(e)
|
|
return string(r)
|
|
}
|
|
|
|
type ExampleSubType struct {
|
|
SubName string `boltease:"subname"`
|
|
}
|
|
|
|
func (e ExampleSubType) String() string {
|
|
r, _ := json.Marshal(e)
|
|
return string(r)
|
|
}
|