boltease/example/main.go

137 lines
2.7 KiB
Go
Raw Normal View History

2023-05-25 16:11:41 +00:00
package main
import (
2023-06-01 16:15:35 +00:00
"encoding/json"
2023-05-25 16:11:41 +00:00
"fmt"
"os"
"git.bullercodeworks.com/brian/boltease"
)
2023-06-01 16:15:35 +00:00
func main() {
//example1()
//fmt.Println()
2023-06-01 16:15:35 +00:00
example2()
2023-05-25 16:11:41 +00:00
}
2023-06-01 16:15:35 +00:00
func example1() {
fmt.Println("# Example 1")
2023-05-25 16:11:41 +00:00
db, err := boltease.Create("example.db", 0600, nil)
if err != nil {
fmt.Printf("Error Opening File: %s\n", err.Error())
os.Exit(1)
}
2023-05-31 22:36:34 +00:00
2023-06-01 16:15:35 +00:00
fmt.Println("## Saving Struct")
err = db.Save(
2023-06-01 16:15:35 +00:00
[]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")
2023-05-31 22:36:34 +00:00
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("")
2023-06-01 16:15:35 +00:00
fmt.Println("## Example 1-2: LoadStruct, simple")
2023-05-31 22:36:34 +00:00
var name string
err = db.Load(
2023-05-31 22:36:34 +00:00
[]string{"examples", "example1", "name"},
&name,
)
fmt.Println("Name:", name)
if err != nil {
fmt.Println("Err:", err)
}
fmt.Println("")
2023-06-01 16:15:35 +00:00
fmt.Println("## Example 1-3: Struct")
2023-05-31 22:36:34 +00:00
fmt.Println("Loading into Struct")
newStruct := ExampleType{}
err = db.Load(
2023-05-31 22:36:34 +00:00
[]string{"examples", "example1"},
&newStruct,
)
fmt.Println(newStruct)
2023-05-25 16:11:41 +00:00
}
2023-06-01 16:15:35 +00:00
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(
2023-06-01 16:15:35 +00:00
[]string{"examples", "example2"},
&ExampleType2{
Name: "Example 2",
Age: 20,
SubTypePtr: &ExampleSubType{
SubName: "Example SubType Pointer",
},
SubType: ExampleSubType{
2023-06-01 16:15:35 +00:00
SubName: "Example SubType",
},
Number: &num,
2023-06-01 16:15:35 +00:00
})
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)
2023-06-01 16:15:35 +00:00
}
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"`
2023-06-01 16:15:35 +00:00
}
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)
}