"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "vendor/gopkg.in/ini.v1/struct.go" between
gitea-1.9.3.tar.gz and gitea-1.9.4.tar.gz

About: Gitea allows to set up a self-hosted code hosting Git service (a fork of Gogs).

struct.go  (gitea-1.9.3):struct.go  (gitea-1.9.4)
skipping to change at line 152 skipping to change at line 152
func wrapStrictError(err error, isStrict bool) error { func wrapStrictError(err error, isStrict bool) error {
if isStrict { if isStrict {
return err return err
} }
return nil return nil
} }
// setWithProperType sets proper value to field based on its type, // setWithProperType sets proper value to field based on its type,
// but it does not return error for failing parsing, // but it does not return error for failing parsing,
// because we want to use default value that is already assigned to strcut. // because we want to use default value that is already assigned to struct.
func setWithProperType(t reflect.Type, key *Key, field reflect.Value, delim stri ng, allowShadow, isStrict bool) error { func setWithProperType(t reflect.Type, key *Key, field reflect.Value, delim stri ng, allowShadow, isStrict bool) error {
switch t.Kind() { switch t.Kind() {
case reflect.String: case reflect.String:
if len(key.String()) == 0 { if len(key.String()) == 0 {
return nil return nil
} }
field.SetString(key.String()) field.SetString(key.String())
case reflect.Bool: case reflect.Bool:
boolVal, err := key.Bool() boolVal, err := key.Bool()
if err != nil { if err != nil {
skipping to change at line 208 skipping to change at line 208
} }
field.SetFloat(floatVal) field.SetFloat(floatVal)
case reflectTime: case reflectTime:
timeVal, err := key.Time() timeVal, err := key.Time()
if err != nil { if err != nil {
return wrapStrictError(err, isStrict) return wrapStrictError(err, isStrict)
} }
field.Set(reflect.ValueOf(timeVal)) field.Set(reflect.ValueOf(timeVal))
case reflect.Slice: case reflect.Slice:
return setSliceWithProperType(key, field, delim, allowShadow, isS trict) return setSliceWithProperType(key, field, delim, allowShadow, isS trict)
case reflect.Ptr:
switch t.Elem().Kind() {
case reflect.Bool:
boolVal, err := key.Bool()
if err != nil {
return wrapStrictError(err, isStrict)
}
field.Set(reflect.ValueOf(&boolVal))
default:
return fmt.Errorf("unsupported type '%s'", t)
}
default: default:
return fmt.Errorf("unsupported type '%s'", t) return fmt.Errorf("unsupported type '%s'", t)
} }
return nil return nil
} }
func parseTagOptions(tag string) (rawName string, omitEmpty bool, allowShadow bo ol) { func parseTagOptions(tag string) (rawName string, omitEmpty bool, allowShadow bo ol) {
opts := strings.SplitN(tag, ",", 3) opts := strings.SplitN(tag, ",", 3)
rawName = opts[0] rawName = opts[0]
if len(opts) > 1 { if len(opts) > 1 {
skipping to change at line 247 skipping to change at line 258
if tag == "-" { if tag == "-" {
continue continue
} }
rawName, _, allowShadow := parseTagOptions(tag) rawName, _, allowShadow := parseTagOptions(tag)
fieldName := s.parseFieldName(tpField.Name, rawName) fieldName := s.parseFieldName(tpField.Name, rawName)
if len(fieldName) == 0 || !field.CanSet() { if len(fieldName) == 0 || !field.CanSet() {
continue continue
} }
isAnonymous := tpField.Type.Kind() == reflect.Ptr && tpField.Anon ymous
isStruct := tpField.Type.Kind() == reflect.Struct isStruct := tpField.Type.Kind() == reflect.Struct
isStructPtr := tpField.Type.Kind() == reflect.Ptr && tpField.Type
.Elem().Kind() == reflect.Struct
isAnonymous := tpField.Type.Kind() == reflect.Ptr && tpField.Anon
ymous
if isAnonymous { if isAnonymous {
field.Set(reflect.New(tpField.Type.Elem())) field.Set(reflect.New(tpField.Type.Elem()))
} }
if isAnonymous || isStruct { if isAnonymous || isStruct || isStructPtr {
if sec, err := s.f.GetSection(fieldName); err == nil { if sec, err := s.f.GetSection(fieldName); err == nil {
// Only set the field to non-nil struct value if
we have
// a section for it. Otherwise, we end up with a
non-nil
// struct ptr even though there is no data.
if isStructPtr && field.IsNil() {
field.Set(reflect.New(tpField.Type.Elem()
))
}
if err = sec.mapTo(field, isStrict); err != nil { if err = sec.mapTo(field, isStrict); err != nil {
return fmt.Errorf("error mapping field(%s ): %v", fieldName, err) return fmt.Errorf("error mapping field(%s ): %v", fieldName, err)
} }
continue continue
} }
} }
if key, err := s.GetKey(fieldName); err == nil { if key, err := s.GetKey(fieldName); err == nil {
delim := parseDelim(tpField.Tag.Get("delim")) delim := parseDelim(tpField.Tag.Get("delim"))
if err = setWithProperType(tpField.Type, key, field, deli m, allowShadow, isStrict); err != nil { if err = setWithProperType(tpField.Type, key, field, deli m, allowShadow, isStrict); err != nil {
skipping to change at line 286 skipping to change at line 304
if typ.Kind() == reflect.Ptr { if typ.Kind() == reflect.Ptr {
typ = typ.Elem() typ = typ.Elem()
val = val.Elem() val = val.Elem()
} else { } else {
return errors.New("cannot map to non-pointer struct") return errors.New("cannot map to non-pointer struct")
} }
return s.mapTo(val, false) return s.mapTo(val, false)
} }
// MapTo maps section to given struct in strict mode, // StrictMapTo maps section to given struct in strict mode,
// which returns all possible error including value parsing error. // which returns all possible error including value parsing error.
func (s *Section) StrictMapTo(v interface{}) error { func (s *Section) StrictMapTo(v interface{}) error {
typ := reflect.TypeOf(v) typ := reflect.TypeOf(v)
val := reflect.ValueOf(v) val := reflect.ValueOf(v)
if typ.Kind() == reflect.Ptr { if typ.Kind() == reflect.Ptr {
typ = typ.Elem() typ = typ.Elem()
val = val.Elem() val = val.Elem()
} else { } else {
return errors.New("cannot map to non-pointer struct") return errors.New("cannot map to non-pointer struct")
} }
return s.mapTo(val, true) return s.mapTo(val, true)
} }
// MapTo maps file to given struct. // MapTo maps file to given struct.
func (f *File) MapTo(v interface{}) error { func (f *File) MapTo(v interface{}) error {
return f.Section("").MapTo(v) return f.Section("").MapTo(v)
} }
// MapTo maps file to given struct in strict mode, // StrictMapTo maps file to given struct in strict mode,
// which returns all possible error including value parsing error. // which returns all possible error including value parsing error.
func (f *File) StrictMapTo(v interface{}) error { func (f *File) StrictMapTo(v interface{}) error {
return f.Section("").StrictMapTo(v) return f.Section("").StrictMapTo(v)
} }
// MapTo maps data sources to given struct with name mapper. // MapToWithMapper maps data sources to given struct with name mapper.
func MapToWithMapper(v interface{}, mapper NameMapper, source interface{}, other s ...interface{}) error { func MapToWithMapper(v interface{}, mapper NameMapper, source interface{}, other s ...interface{}) error {
cfg, err := Load(source, others...) cfg, err := Load(source, others...)
if err != nil { if err != nil {
return err return err
} }
cfg.NameMapper = mapper cfg.NameMapper = mapper
return cfg.MapTo(v) return cfg.MapTo(v)
} }
// StrictMapToWithMapper maps data sources to given struct with name mapper in s trict mode, // StrictMapToWithMapper maps data sources to given struct with name mapper in s trict mode,
skipping to change at line 345 skipping to change at line 363
return MapToWithMapper(v, nil, source, others...) return MapToWithMapper(v, nil, source, others...)
} }
// StrictMapTo maps data sources to given struct in strict mode, // StrictMapTo maps data sources to given struct in strict mode,
// which returns all possible error including value parsing error. // which returns all possible error including value parsing error.
func StrictMapTo(v, source interface{}, others ...interface{}) error { func StrictMapTo(v, source interface{}, others ...interface{}) error {
return StrictMapToWithMapper(v, nil, source, others...) return StrictMapToWithMapper(v, nil, source, others...)
} }
// reflectSliceWithProperType does the opposite thing as setSliceWithProperType. // reflectSliceWithProperType does the opposite thing as setSliceWithProperType.
func reflectSliceWithProperType(key *Key, field reflect.Value, delim string) err or { func reflectSliceWithProperType(key *Key, field reflect.Value, delim string, all owShadow bool) error {
slice := field.Slice(0, field.Len()) slice := field.Slice(0, field.Len())
if field.Len() == 0 { if field.Len() == 0 {
return nil return nil
} }
sliceOf := field.Type().Elem().Kind()
if allowShadow {
var keyWithShadows *Key
for i := 0; i < field.Len(); i++ {
var val string
switch sliceOf {
case reflect.String:
val = slice.Index(i).String()
case reflect.Int, reflect.Int64:
val = fmt.Sprint(slice.Index(i).Int())
case reflect.Uint, reflect.Uint64:
val = fmt.Sprint(slice.Index(i).Uint())
case reflect.Float64:
val = fmt.Sprint(slice.Index(i).Float())
case reflectTime:
val = slice.Index(i).Interface().(time.Time).Form
at(time.RFC3339)
default:
return fmt.Errorf("unsupported type '[]%s'", slic
eOf)
}
if i == 0 {
keyWithShadows = newKey(key.s, key.name, val)
} else {
keyWithShadows.AddShadow(val)
}
}
key = keyWithShadows
return nil
}
var buf bytes.Buffer var buf bytes.Buffer
sliceOf := field.Type().Elem().Kind()
for i := 0; i < field.Len(); i++ { for i := 0; i < field.Len(); i++ {
switch sliceOf { switch sliceOf {
case reflect.String: case reflect.String:
buf.WriteString(slice.Index(i).String()) buf.WriteString(slice.Index(i).String())
case reflect.Int, reflect.Int64: case reflect.Int, reflect.Int64:
buf.WriteString(fmt.Sprint(slice.Index(i).Int())) buf.WriteString(fmt.Sprint(slice.Index(i).Int()))
case reflect.Uint, reflect.Uint64: case reflect.Uint, reflect.Uint64:
buf.WriteString(fmt.Sprint(slice.Index(i).Uint())) buf.WriteString(fmt.Sprint(slice.Index(i).Uint()))
case reflect.Float64: case reflect.Float64:
buf.WriteString(fmt.Sprint(slice.Index(i).Float())) buf.WriteString(fmt.Sprint(slice.Index(i).Float()))
case reflectTime: case reflectTime:
buf.WriteString(slice.Index(i).Interface().(time.Time).Fo rmat(time.RFC3339)) buf.WriteString(slice.Index(i).Interface().(time.Time).Fo rmat(time.RFC3339))
default: default:
return fmt.Errorf("unsupported type '[]%s'", sliceOf) return fmt.Errorf("unsupported type '[]%s'", sliceOf)
} }
buf.WriteString(delim) buf.WriteString(delim)
} }
key.SetValue(buf.String()[:buf.Len()-1]) key.SetValue(buf.String()[:buf.Len()-len(delim)])
return nil return nil
} }
// reflectWithProperType does the opposite thing as setWithProperType. // reflectWithProperType does the opposite thing as setWithProperType.
func reflectWithProperType(t reflect.Type, key *Key, field reflect.Value, delim string) error { func reflectWithProperType(t reflect.Type, key *Key, field reflect.Value, delim string, allowShadow bool) error {
switch t.Kind() { switch t.Kind() {
case reflect.String: case reflect.String:
key.SetValue(field.String()) key.SetValue(field.String())
case reflect.Bool: case reflect.Bool:
key.SetValue(fmt.Sprint(field.Bool())) key.SetValue(fmt.Sprint(field.Bool()))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int 64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int 64:
key.SetValue(fmt.Sprint(field.Int())) key.SetValue(fmt.Sprint(field.Int()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect .Uint64: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect .Uint64:
key.SetValue(fmt.Sprint(field.Uint())) key.SetValue(fmt.Sprint(field.Uint()))
case reflect.Float32, reflect.Float64: case reflect.Float32, reflect.Float64:
key.SetValue(fmt.Sprint(field.Float())) key.SetValue(fmt.Sprint(field.Float()))
case reflectTime: case reflectTime:
key.SetValue(fmt.Sprint(field.Interface().(time.Time).Format(time .RFC3339))) key.SetValue(fmt.Sprint(field.Interface().(time.Time).Format(time .RFC3339)))
case reflect.Slice: case reflect.Slice:
return reflectSliceWithProperType(key, field, delim) return reflectSliceWithProperType(key, field, delim, allowShadow)
case reflect.Ptr:
if !field.IsNil() {
return reflectWithProperType(t.Elem(), key, field.Elem(),
delim, allowShadow)
}
default: default:
return fmt.Errorf("unsupported type '%s'", t) return fmt.Errorf("unsupported type '%s'", t)
} }
return nil return nil
} }
// CR: copied from encoding/json/encode.go with modifications of time.Time suppo rt. // CR: copied from encoding/json/encode.go with modifications of time.Time suppo rt.
// TODO: add more test coverage. // TODO: add more test coverage.
func isEmptyValue(v reflect.Value) bool { func isEmptyValue(v reflect.Value) bool {
switch v.Kind() { switch v.Kind() {
skipping to change at line 435 skipping to change at line 486
for i := 0; i < typ.NumField(); i++ { for i := 0; i < typ.NumField(); i++ {
field := val.Field(i) field := val.Field(i)
tpField := typ.Field(i) tpField := typ.Field(i)
tag := tpField.Tag.Get("ini") tag := tpField.Tag.Get("ini")
if tag == "-" { if tag == "-" {
continue continue
} }
opts := strings.SplitN(tag, ",", 2) rawName, omitEmpty, allowShadow := parseTagOptions(tag)
if len(opts) == 2 && opts[1] == "omitempty" && isEmptyValue(field if omitEmpty && isEmptyValue(field) {
) {
continue continue
} }
fieldName := s.parseFieldName(tpField.Name, opts[0]) fieldName := s.parseFieldName(tpField.Name, rawName)
if len(fieldName) == 0 || !field.CanSet() { if len(fieldName) == 0 || !field.CanSet() {
continue continue
} }
if (tpField.Type.Kind() == reflect.Ptr && tpField.Anonymous) || if (tpField.Type.Kind() == reflect.Ptr && tpField.Anonymous) ||
(tpField.Type.Kind() == reflect.Struct && tpField.Type.Na me() != "Time") { (tpField.Type.Kind() == reflect.Struct && tpField.Type.Na me() != "Time") {
// Note: The only error here is section doesn't exist. // Note: The only error here is section doesn't exist.
sec, err := s.f.GetSection(fieldName) sec, err := s.f.GetSection(fieldName)
if err != nil { if err != nil {
// Note: fieldName can never be empty here, ignor e error. // Note: fieldName can never be empty here, ignor e error.
skipping to change at line 476 skipping to change at line 527
key, err := s.GetKey(fieldName) key, err := s.GetKey(fieldName)
if err != nil { if err != nil {
key, _ = s.NewKey(fieldName, "") key, _ = s.NewKey(fieldName, "")
} }
// Add comment from comment tag // Add comment from comment tag
if len(key.Comment) == 0 { if len(key.Comment) == 0 {
key.Comment = tpField.Tag.Get("comment") key.Comment = tpField.Tag.Get("comment")
} }
if err = reflectWithProperType(tpField.Type, key, field, parseDel im(tpField.Tag.Get("delim"))); err != nil { if err = reflectWithProperType(tpField.Type, key, field, parseDel im(tpField.Tag.Get("delim")), allowShadow); err != nil {
return fmt.Errorf("error reflecting field (%s): %v", fiel dName, err) return fmt.Errorf("error reflecting field (%s): %v", fiel dName, err)
} }
} }
return nil return nil
} }
// ReflectFrom reflects secion from given struct. // ReflectFrom reflects secion from given struct.
func (s *Section) ReflectFrom(v interface{}) error { func (s *Section) ReflectFrom(v interface{}) error {
typ := reflect.TypeOf(v) typ := reflect.TypeOf(v)
skipping to change at line 503 skipping to change at line 554
} }
return s.reflectFrom(val) return s.reflectFrom(val)
} }
// ReflectFrom reflects file from given struct. // ReflectFrom reflects file from given struct.
func (f *File) ReflectFrom(v interface{}) error { func (f *File) ReflectFrom(v interface{}) error {
return f.Section("").ReflectFrom(v) return f.Section("").ReflectFrom(v)
} }
// ReflectFrom reflects data sources from given struct with name mapper. // ReflectFromWithMapper reflects data sources from given struct with name mappe r.
func ReflectFromWithMapper(cfg *File, v interface{}, mapper NameMapper) error { func ReflectFromWithMapper(cfg *File, v interface{}, mapper NameMapper) error {
cfg.NameMapper = mapper cfg.NameMapper = mapper
return cfg.ReflectFrom(v) return cfg.ReflectFrom(v)
} }
// ReflectFrom reflects data sources from given struct. // ReflectFrom reflects data sources from given struct.
func ReflectFrom(cfg *File, v interface{}) error { func ReflectFrom(cfg *File, v interface{}) error {
return ReflectFromWithMapper(cfg, v, nil) return ReflectFromWithMapper(cfg, v, nil)
} }
 End of changes. 19 change blocks. 
17 lines changed or deleted 75 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)