WIP: Implementing setJailProperty, add recursivity to getStructFieldValue
This commit is contained in:
parent
239bcd4b95
commit
f40db69b9d
11
cmd/start.go
11
cmd/start.go
@ -47,7 +47,11 @@ func SetJailProperties(args []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, p := range props {
|
for _, p := range props {
|
||||||
setJailProperty(&jail, p.name, p.value)
|
err := setJailProperty(&jail, p.name, p.value)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %s\n", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,8 +70,9 @@ func setJailProperty(jail *Jail, propName string, propValue string) error {
|
|||||||
for i, j := range gJails {
|
for i, j := range gJails {
|
||||||
if j.Name == jail.Name {
|
if j.Name == jail.Name {
|
||||||
val, _, err := getStructFieldValue(&gJails[i], propName)
|
val, _, err := getStructFieldValue(&gJails[i], propName)
|
||||||
|
//val, _, err := getStructFieldValue(&gJails[i].Config, strings.Split(propName, ".")[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New(fmt.Sprintf("Field not found: %s", propName))
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
/*if kind == "string" {
|
/*if kind == "string" {
|
||||||
@ -93,7 +98,7 @@ func setJailProperty(jail *Jail, propName string, propValue string) error {
|
|||||||
|
|
||||||
|
|
||||||
// TODO : integrate this function
|
// TODO : integrate this function
|
||||||
setJailConfigUpdated(jail)
|
//setJailConfigUpdated(jail)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
110
cmd/utils.go
110
cmd/utils.go
@ -188,11 +188,15 @@ func getStructFieldNames(parentStruct interface{}, result []string, prefix strin
|
|||||||
func getStructFieldValue(parentStruct interface{}, fieldName string) (*reflect.Value, string, error) {
|
func getStructFieldValue(parentStruct interface{}, fieldName string) (*reflect.Value, string, error) {
|
||||||
v := reflect.ValueOf(parentStruct)
|
v := reflect.ValueOf(parentStruct)
|
||||||
|
|
||||||
if v.Kind() == reflect.Ptr {
|
// Get value while we're dealing with pointers
|
||||||
fmt.Printf("We got a pointer, get Elem form it\n")
|
for ; v.Kind() == reflect.Ptr ; v = v.Elem() {}
|
||||||
v = v.Elem()
|
|
||||||
|
if v.Kind() != reflect.Struct {
|
||||||
|
return &v, fieldName, errors.New(fmt.Sprintf("parentStruct is not a struct! Kind: %s", v.Kind().String()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typeOfV := v.Type()
|
||||||
|
|
||||||
if false {
|
if false {
|
||||||
for i := 0 ; i < v.NumField(); i++ {
|
for i := 0 ; i < v.NumField(); i++ {
|
||||||
f := v.Field(i)
|
f := v.Field(i)
|
||||||
@ -203,17 +207,111 @@ func getStructFieldValue(parentStruct interface{}, fieldName string) (*reflect.V
|
|||||||
}
|
}
|
||||||
|
|
||||||
if strings.Contains(fieldName, ".") {
|
if strings.Contains(fieldName, ".") {
|
||||||
|
|
||||||
|
// Le code degueulasse mais qui marche
|
||||||
|
legacy := false
|
||||||
|
|
||||||
|
if legacy {
|
||||||
fs := strings.Split(fieldName, ".")
|
fs := strings.Split(fieldName, ".")
|
||||||
f := v.FieldByName(fs[0])
|
fmt.Printf("Dealing with field \"%s\" of struct \"%s\"\n", strings.Join(fs[1:], "."), fs[0])
|
||||||
|
var f reflect.Value
|
||||||
|
var found bool
|
||||||
|
for i := 0 ; i < v.NumField() ; i++ {
|
||||||
|
if typeOfV.Field(i).Name == fs[0] {
|
||||||
|
f = v.Field(i)
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
return &v, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
||||||
|
}
|
||||||
|
|
||||||
|
for ; f.Kind() == reflect.Ptr ; {
|
||||||
|
fmt.Printf("We got a pointer as a nested field, get Elem form it\n")
|
||||||
|
f = f.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("v.kind() = %v\n", v.Kind().String())
|
||||||
|
fmt.Printf("v = %v\n", v)
|
||||||
|
fmt.Printf("f.kind() = %v\n", f.Kind().String())
|
||||||
|
fmt.Printf("f = %v\n", f)
|
||||||
|
/*if f.Kind() == reflect.Ptr {
|
||||||
|
fmt.Printf("We got a pointer as a nested field, get Elem form it\n")
|
||||||
|
f = f.Elem()
|
||||||
|
}*/
|
||||||
//f := v.Elem().FieldByName(fs[0])
|
//f := v.Elem().FieldByName(fs[0])
|
||||||
if f.Kind() == reflect.Struct {
|
if f.Kind() == reflect.Struct {
|
||||||
return getStructFieldValue(f.Interface(), strings.Join(fs[1:], "."))
|
//return getStructFieldValue(&f, strings.Join(fs[1:], "."))
|
||||||
|
//return getStructFieldValue(f, strings.Join(fs[1:], "."))
|
||||||
|
|
||||||
|
|
||||||
|
/* GRUIIIK */
|
||||||
|
|
||||||
|
g := f.FieldByName(fs[1])
|
||||||
|
fmt.Printf("get substruct field, f.kind() = %v\n", f.Kind().String())
|
||||||
|
fmt.Printf("get substruct field, g.kind() = %v\n", g.Kind().String())
|
||||||
|
fmt.Printf("get substruct field, f = %v\n", f)
|
||||||
|
fmt.Printf("get substruct field, g = %v\n", g)
|
||||||
|
if g.IsValid() {
|
||||||
|
fmt.Printf("Return g = %v of Kind %s\n", g, g.Kind().String())
|
||||||
|
return &g, fieldName, nil
|
||||||
|
} else {
|
||||||
|
return &f, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
||||||
|
}
|
||||||
|
|
||||||
|
/* FIN DE GRUIIIIK */
|
||||||
} else {
|
} else {
|
||||||
log.Fatalln(fmt.Sprintf("%s is not a struct: %s\n", fs[0], f.Kind().String()))
|
log.Fatalln(fmt.Sprintf("%s is not a struct: %s\n", fs[0], f.Kind().String()))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
f := v.FieldByName(fieldName)
|
var f reflect.Value
|
||||||
|
var found bool
|
||||||
|
fs := strings.Split(fieldName, ".")
|
||||||
|
// Loop through properties
|
||||||
|
for i, curF := range fs {
|
||||||
|
found = false
|
||||||
|
for j := 0 ; j < v.NumField() ; j++ {
|
||||||
|
if typeOfV.Field(j).Name == curF {
|
||||||
|
f = v.Field(j)
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
return &v, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
||||||
|
}
|
||||||
|
|
||||||
|
for ; f.Kind() == reflect.Ptr ; f = f.Elem() {}
|
||||||
|
|
||||||
|
/*fmt.Printf("v.kind() = %v\n", v.Kind().String())
|
||||||
|
fmt.Printf("v = %v\n", v)
|
||||||
|
fmt.Printf("f.kind() = %v\n", f.Kind().String())
|
||||||
|
fmt.Printf("f = %v\n", f)*/
|
||||||
|
|
||||||
|
// If this is the last loop, return result even if it's a struct
|
||||||
|
// FIXME : What if we got interface?
|
||||||
|
if f.Kind() != reflect.Struct && i < len(fs)-1 {
|
||||||
if f.IsValid() {
|
if f.IsValid() {
|
||||||
|
//fmt.Printf("Return f = %v of Kind %s\n", f, f.Kind().String())
|
||||||
|
return &f, fieldName, nil
|
||||||
|
} else {
|
||||||
|
return &v, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
v = f
|
||||||
|
typeOfV = v.Type()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
f := v.FieldByName(fieldName)
|
||||||
|
fmt.Printf("No substruct, v.kind() = %v\n", v.Kind().String())
|
||||||
|
fmt.Printf("No substruct, f.kind() = %v\n", f.Kind().String())
|
||||||
|
fmt.Printf("No substruct, v = %v\n", v)
|
||||||
|
fmt.Printf("No substruct, f = %v\n", f)
|
||||||
|
if f.IsValid() {
|
||||||
|
fmt.Printf("Return f = %v of Kind %s\n", f, f.Kind().String())
|
||||||
return &f, fieldName, nil
|
return &f, fieldName, nil
|
||||||
} else {
|
} else {
|
||||||
return &v, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
return &v, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
||||||
|
Loading…
Reference in New Issue
Block a user