Working on Struct Save/Load

This commit is contained in:
2023-05-31 17:36:34 -05:00
parent 0e0dff859f
commit de7d97d96b
3 changed files with 205 additions and 58 deletions

View File

@@ -12,6 +12,10 @@ type ExampleType struct {
Age int `boltease:"age"`
}
func (e ExampleType) String() string {
return fmt.Sprintf("{\"Name\":\"%s\",\"Age\":%d}", e.Name, e.Age)
}
type ExampleSubType struct {
SubName string `boltease:"subname"`
}
@@ -22,6 +26,35 @@ func main() {
fmt.Printf("Error Opening File: %s\n", err.Error())
os.Exit(1)
}
fmt.Println("# Example 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 2: LoadStruct, simple")
var name string
err = db.LoadStruct(
[]string{"examples", "example1", "name"},
&name,
)
fmt.Println("Name:", name)
if err != nil {
fmt.Println("Err:", err)
}
fmt.Println("")
fmt.Println("# Example 3: Struct")
err = db.SaveStruct(
[]string{"examples", "example1"},
ExampleType{
@@ -32,4 +65,12 @@ func main() {
fmt.Printf("Error saving struct: %s\n", err.Error())
os.Exit(1)
}
fmt.Println("Loading into Struct")
newStruct := ExampleType{}
err = db.LoadStruct(
[]string{"examples", "example1"},
&newStruct,
)
fmt.Println(newStruct)
}