Saving Arbitrary Structs

Now for loading them
This commit is contained in:
2023-06-08 09:52:03 -05:00
parent 7c152f1273
commit ac4d008f57
3 changed files with 66 additions and 34 deletions

View File

@@ -9,8 +9,8 @@ import (
)
func main() {
example1()
fmt.Println()
//example1()
//fmt.Println()
example2()
}
@@ -23,7 +23,7 @@ func example1() {
}
fmt.Println("## Saving Struct")
err = db.SaveStruct(
err = db.Save(
[]string{"examples", "example1"},
ExampleType{
Name: "Example 1",
@@ -51,7 +51,7 @@ func example1() {
fmt.Println("## Example 1-2: LoadStruct, simple")
var name string
err = db.LoadStruct(
err = db.Load(
[]string{"examples", "example1", "name"},
&name,
)
@@ -64,7 +64,7 @@ func example1() {
fmt.Println("## Example 1-3: Struct")
fmt.Println("Loading into Struct")
newStruct := ExampleType{}
err = db.LoadStruct(
err = db.Load(
[]string{"examples", "example1"},
&newStruct,
)
@@ -79,23 +79,28 @@ func example2() {
os.Exit(1)
}
fmt.Println("## Saving Struct")
err = db.SaveStruct(
num := 12345
err = db.Save(
[]string{"examples", "example2"},
&ExampleType2{
Name: "Example 2",
Age: 20,
SubType: &ExampleSubType{
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.LoadStruct([]string{"examples", "example2"}, &newStruct)
//fmt.Println(newStruct)
newStruct := ExampleType2{}
err = db.Load([]string{"examples", "example2"}, &newStruct)
fmt.Println(newStruct)
}
type ExampleType struct {
@@ -109,9 +114,11 @@ func (e ExampleType) String() string {
}
type ExampleType2 struct {
Name string `boltease:"name"`
Age int `boltease:"age"`
SubType *ExampleSubType `boltease:"subtype"`
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 {