I'M WORKING ON IT, JEEZ.
This commit is contained in:
parent
de7d97d96b
commit
7c152f1273
@ -36,19 +36,32 @@ func (b *DB) LoadStruct(path []string, dest any) error {
|
||||
}
|
||||
} else {
|
||||
// Set the value
|
||||
reflect.ValueOf(dest).Elem().FieldByName(structFld.Name).Set(reflect.ValueOf(wrk))
|
||||
}
|
||||
|
||||
case reflect.Bool:
|
||||
var wrk bool
|
||||
err := b.GetForInterface(path, fldName, &wrk)
|
||||
if err != nil {
|
||||
if ret == nil {
|
||||
ret = err
|
||||
}
|
||||
} else {
|
||||
// Set the value
|
||||
reflect.ValueOf(dest).Elem().FieldByName(structFld.Name).Set(reflect.ValueOf(wrk))
|
||||
}
|
||||
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
|
||||
}
|
||||
err := b.GetForInterface(path, fldName, fld)
|
||||
if err != nil {
|
||||
fmt.Printf(" Err: %s\n", err.Error())
|
||||
}
|
||||
if err != nil && ret == nil {
|
||||
ret = err
|
||||
var wrk int
|
||||
err := b.GetForInterface(path, fldName, &wrk)
|
||||
if err != nil {
|
||||
if ret == nil {
|
||||
ret = err
|
||||
}
|
||||
} else {
|
||||
// Set the value
|
||||
reflect.ValueOf(dest).Elem().FieldByName(structFld.Name).Set(reflect.ValueOf(wrk))
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@ -84,13 +97,31 @@ func (b *DB) LoadStruct(path []string, dest any) error {
|
||||
|
||||
func (b *DB) SaveStruct(path []string, src any) error {
|
||||
t := reflect.TypeOf(src)
|
||||
fmt.Println(t.Kind())
|
||||
if t.Kind() == reflect.Pointer {
|
||||
// Save the actual struct
|
||||
return b.SaveStruct(path, reflect.Indirect(reflect.ValueOf(src)))
|
||||
}
|
||||
|
||||
fields := reflect.VisibleFields(t)
|
||||
r := reflect.ValueOf(src)
|
||||
fmt.Println()
|
||||
for _, fld := range fields {
|
||||
r := reflect.ValueOf(src)
|
||||
f := reflect.Indirect(r).FieldByName(fld.Name)
|
||||
if err := b.Set(path, FieldName(fld), f); err != nil {
|
||||
return err
|
||||
}
|
||||
f := r.FieldByName(fld.Name)
|
||||
fmt.Println(f, FieldName(fld))
|
||||
/*
|
||||
if (f.Kind() == reflect.Struct || f.Kind() == reflect.Pointer) && f != src {
|
||||
fmt.Println("Recursing on Field:", f)
|
||||
err := b.SaveStruct(append(path, FieldName(fld)), f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := b.Set(path, FieldName(fld), f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -109,6 +140,8 @@ func FieldIgnored(fld reflect.StructField) bool {
|
||||
|
||||
func ReflectValueToInterface(val reflect.Value) interface{} {
|
||||
switch val.Kind() {
|
||||
case reflect.Pointer:
|
||||
return ReflectValueToInterface(reflect.Indirect(val))
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return val.Int()
|
||||
case reflect.Bool:
|
||||
|
107
example/main.go
107
example/main.go
@ -1,33 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.bullercodeworks.com/brian/boltease"
|
||||
)
|
||||
|
||||
type ExampleType struct {
|
||||
Name string `boltease:"name"`
|
||||
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"`
|
||||
}
|
||||
|
||||
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("# Example 1: Simple")
|
||||
fmt.Println("## Saving Struct")
|
||||
err = db.SaveStruct(
|
||||
[]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 {
|
||||
@ -42,7 +49,7 @@ func main() {
|
||||
fmt.Println("Age:", age)
|
||||
fmt.Println("")
|
||||
|
||||
fmt.Println("# Example 2: LoadStruct, simple")
|
||||
fmt.Println("## Example 1-2: LoadStruct, simple")
|
||||
var name string
|
||||
err = db.LoadStruct(
|
||||
[]string{"examples", "example1", "name"},
|
||||
@ -54,18 +61,7 @@ func main() {
|
||||
}
|
||||
fmt.Println("")
|
||||
|
||||
fmt.Println("# Example 3: Struct")
|
||||
err = db.SaveStruct(
|
||||
[]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-3: Struct")
|
||||
fmt.Println("Loading into Struct")
|
||||
newStruct := ExampleType{}
|
||||
err = db.LoadStruct(
|
||||
@ -74,3 +70,60 @@ func main() {
|
||||
)
|
||||
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")
|
||||
err = db.SaveStruct(
|
||||
[]string{"examples", "example2"},
|
||||
&ExampleType2{
|
||||
Name: "Example 2",
|
||||
Age: 20,
|
||||
SubType: &ExampleSubType{
|
||||
SubName: "Example SubType",
|
||||
},
|
||||
})
|
||||
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)
|
||||
}
|
||||
|
||||
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"`
|
||||
SubType *ExampleSubType `boltease:"subtype"`
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user