func TestENV(t *testing.T) { m := EnvironmentLoader{} s := &Server{} structName := structs.Name(s) // set env variables setEnvVars(t, structName, "") if err := m.Load(s); err != nil { t.Error(err) } testStruct(t, s, getDefaultServer()) }
func TestCamelCaseEnv(t *testing.T) { m := EnvironmentLoader{ CamelCase: true, } s := &CamelCaseServer{} structName := structs.Name(s) // set env variables setEnvVars(t, structName, "") if err := m.Load(s); err != nil { t.Error(err) } testCamelcaseStruct(t, s, getDefaultCamelCaseServer()) }
func TestTagFlags(t *testing.T) { m := FlagLoader{} s := &TestFlagTagServer{} structName := structs.Name(s) // get flags args := getFlags(t, structName, "") m.Args = args[1:] if err := m.Load(s); err != nil { t.Error(err) } testFlagTagStruct(t, s, getDefaultFlagTagServer()) }
func TestFlagWithPrefix(t *testing.T) { const prefix = "Prefix" m := FlagLoader{Prefix: prefix} s := &TestFlagServer{} structName := structs.Name(s) // get flags args := getFlags(t, structName, prefix) m.Args = args[1:] if err := m.Load(s); err != nil { t.Error(err) } testFlagStruct(t, s, getDefaultFlagServer()) }
func TestCamelcaseFlags(t *testing.T) { m := FlagLoader{ CamelCase: true, } s := &CamelCaseServer{} structName := structs.Name(s) // get flags args := getFlags(t, structName, "") m.Args = args[1:] if err := m.Load(s); err != nil { t.Error(err) } testCamelcaseStruct(t, s, getDefaultCamelCaseServer()) }
func otherReflect(ui IEntity) { structName := structs.Name(ui) log.Println("structName", structName) fieldNames := structs.Names(ui) structInfo := structs.New(ui) values := make([]interface{}, 0) sqlFieldStr := "" sqlFieldPre := "" i := 0 for _, fieldName := range fieldNames { field := structInfo.Field(fieldName) if field.IsExported() { tags := strings.Split(strings.Trim(field.Tag("db"), " "), ",") isPk := false if len(tags) == 2 && tags[1] == "pk" { isPk = true } tag := tags[0] if !isPk { values = append(values, field.Value()) if i == 0 { sqlFieldStr += "(" + strings.ToLower(tag) sqlFieldPre += "(?" } else { sqlFieldStr += "," + strings.ToLower(tag) sqlFieldPre += ",?" } i++ } } } if len(values) > 1 { sqlFieldStr += ")" sqlFieldPre += ")" } sql := fmt.Sprintf("INSERT INTO %s %s VALUES %s", ui.TableName(), sqlFieldStr, sqlFieldPre) fmt.Println(sql) fmt.Println(values) }