diff options
Diffstat (limited to 'src/pkg/xml/read.go')
-rw-r--r-- | src/pkg/xml/read.go | 116 |
1 files changed, 58 insertions, 58 deletions
diff --git a/src/pkg/xml/read.go b/src/pkg/xml/read.go index 9ae3bb8ee..a3ddb9d4c 100644 --- a/src/pkg/xml/read.go +++ b/src/pkg/xml/read.go @@ -139,8 +139,8 @@ import ( // to a freshly allocated value and then mapping the element to that value. // func Unmarshal(r io.Reader, val interface{}) os.Error { - v, ok := reflect.NewValue(val).(*reflect.PtrValue) - if !ok { + v := reflect.NewValue(val) + if v.Kind() != reflect.Ptr { return os.NewError("non-pointer passed to Unmarshal") } p := NewParser(r) @@ -176,8 +176,8 @@ func (e *TagPathError) String() string { // Passing a nil start element indicates that Unmarshal should // read the token stream to find the start element. func (p *Parser) Unmarshal(val interface{}, start *StartElement) os.Error { - v, ok := reflect.NewValue(val).(*reflect.PtrValue) - if !ok { + v := reflect.NewValue(val) + if v.Kind() != reflect.Ptr { return os.NewError("non-pointer passed to Unmarshal") } return p.unmarshal(v.Elem(), start) @@ -219,10 +219,10 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error { } } - if pv, ok := val.(*reflect.PtrValue); ok { - if pv.Get() == 0 { - zv := reflect.MakeZero(pv.Type().(*reflect.PtrType).Elem()) - pv.PointTo(zv) + if pv := val; pv.Kind() == reflect.Ptr { + if pv.Pointer() == 0 { + zv := reflect.Zero(pv.Type().Elem()) + pv.Set(zv.Addr()) val = zv } else { val = pv.Elem() @@ -237,17 +237,17 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error { saveXML reflect.Value saveXMLIndex int saveXMLData []byte - sv *reflect.StructValue - styp *reflect.StructType + sv reflect.Value + styp reflect.Type fieldPaths map[string]pathInfo ) - switch v := val.(type) { + switch v := val; v.Kind() { default: return os.ErrorString("unknown type " + v.Type().String()) - case *reflect.SliceValue: - typ := v.Type().(*reflect.SliceType) + case reflect.Slice: + typ := v.Type() if typ.Elem().Kind() == reflect.Uint8 { // []byte saveData = v @@ -269,23 +269,23 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error { v.SetLen(n + 1) // Recur to read element into slice. - if err := p.unmarshal(v.Elem(n), start); err != nil { + if err := p.unmarshal(v.Index(n), start); err != nil { v.SetLen(n) return err } return nil - case *reflect.BoolValue, *reflect.FloatValue, *reflect.IntValue, *reflect.UintValue, *reflect.StringValue: + case reflect.Bool, reflect.Float32, reflect.Float64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.String: saveData = v - case *reflect.StructValue: + case reflect.Struct: if _, ok := v.Interface().(Name); ok { - v.Set(reflect.NewValue(start.Name).(*reflect.StructValue)) + v.Set(reflect.NewValue(start.Name)) break } sv = v - typ := sv.Type().(*reflect.StructType) + typ := sv.Type() styp = typ // Assign name. if f, ok := typ.FieldByName("XMLName"); ok { @@ -316,7 +316,7 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error { if _, ok := v.Interface().(Name); !ok { return UnmarshalError(sv.Type().String() + " field XMLName does not have type xml.Name") } - v.(*reflect.StructValue).Set(reflect.NewValue(start.Name).(*reflect.StructValue)) + v.Set(reflect.NewValue(start.Name)) } // Assign attributes. @@ -325,8 +325,8 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error { f := typ.Field(i) switch f.Tag { case "attr": - strv, ok := sv.FieldByIndex(f.Index).(*reflect.StringValue) - if !ok { + strv := sv.FieldByIndex(f.Index) + if strv.Kind() != reflect.String { return UnmarshalError(sv.Type().String() + " field " + f.Name + " has attr tag but is not type string") } // Look for attribute. @@ -338,20 +338,20 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error { break } } - strv.Set(val) + strv.SetString(val) case "comment": - if saveComment == nil { + if !saveComment.IsValid() { saveComment = sv.FieldByIndex(f.Index) } case "chardata": - if saveData == nil { + if !saveData.IsValid() { saveData = sv.FieldByIndex(f.Index) } case "innerxml": - if saveXML == nil { + if !saveXML.IsValid() { saveXML = sv.FieldByIndex(f.Index) if p.saved == nil { saveXMLIndex = 0 @@ -387,7 +387,7 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error { Loop: for { var savedOffset int - if saveXML != nil { + if saveXML.IsValid() { savedOffset = p.savedOffset() } tok, err := p.Token() @@ -398,7 +398,7 @@ Loop: case StartElement: // Sub-element. // Look up by tag name. - if sv != nil { + if sv.IsValid() { k := fieldName(t.Name.Local) if fieldPaths != nil { @@ -437,7 +437,7 @@ Loop: } case EndElement: - if saveXML != nil { + if saveXML.IsValid() { saveXMLData = p.saved.Bytes()[saveXMLIndex:savedOffset] if saveXMLIndex == 0 { p.saved = nil @@ -446,12 +446,12 @@ Loop: break Loop case CharData: - if saveData != nil { + if saveData.IsValid() { data = append(data, t...) } case Comment: - if saveComment != nil { + if saveComment.IsValid() { comment = append(comment, t...) } } @@ -479,50 +479,50 @@ Loop: } // Save accumulated data and comments - switch t := saveData.(type) { - case nil: + switch t := saveData; t.Kind() { + case reflect.Invalid: // Probably a comment, handled below default: return os.ErrorString("cannot happen: unknown type " + t.Type().String()) - case *reflect.IntValue: + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: if !getInt64() { return err } - t.Set(itmp) - case *reflect.UintValue: + t.SetInt(itmp) + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: if !getUint64() { return err } - t.Set(utmp) - case *reflect.FloatValue: + t.SetUint(utmp) + case reflect.Float32, reflect.Float64: if !getFloat64() { return err } - t.Set(ftmp) - case *reflect.BoolValue: + t.SetFloat(ftmp) + case reflect.Bool: value, err := strconv.Atob(strings.TrimSpace(string(data))) if err != nil { return err } - t.Set(value) - case *reflect.StringValue: - t.Set(string(data)) - case *reflect.SliceValue: - t.Set(reflect.NewValue(data).(*reflect.SliceValue)) + t.SetBool(value) + case reflect.String: + t.SetString(string(data)) + case reflect.Slice: + t.Set(reflect.NewValue(data)) } - switch t := saveComment.(type) { - case *reflect.StringValue: - t.Set(string(comment)) - case *reflect.SliceValue: - t.Set(reflect.NewValue(comment).(*reflect.SliceValue)) + switch t := saveComment; t.Kind() { + case reflect.String: + t.SetString(string(comment)) + case reflect.Slice: + t.Set(reflect.NewValue(comment)) } - switch t := saveXML.(type) { - case *reflect.StringValue: - t.Set(string(saveXMLData)) - case *reflect.SliceValue: - t.Set(reflect.NewValue(saveXMLData).(*reflect.SliceValue)) + switch t := saveXML; t.Kind() { + case reflect.String: + t.SetString(string(saveXMLData)) + case reflect.Slice: + t.Set(reflect.NewValue(saveXMLData)) } return nil @@ -537,7 +537,7 @@ type pathInfo struct { // paths map with all paths leading to it ("a", "a>b", and "a>b>c"). // It is okay for paths to share a common, shorter prefix but not ok // for one path to itself be a prefix of another. -func addFieldPath(sv *reflect.StructValue, paths map[string]pathInfo, path string, fieldIdx []int) os.Error { +func addFieldPath(sv reflect.Value, paths map[string]pathInfo, path string, fieldIdx []int) os.Error { if info, found := paths[path]; found { return tagError(sv, info.fieldIdx, fieldIdx) } @@ -560,8 +560,8 @@ func addFieldPath(sv *reflect.StructValue, paths map[string]pathInfo, path strin } -func tagError(sv *reflect.StructValue, idx1 []int, idx2 []int) os.Error { - t := sv.Type().(*reflect.StructType) +func tagError(sv reflect.Value, idx1 []int, idx2 []int) os.Error { + t := sv.Type() f1 := t.FieldByIndex(idx1) f2 := t.FieldByIndex(idx2) return &TagPathError{t, f1.Name, f1.Tag, f2.Name, f2.Tag} @@ -569,7 +569,7 @@ func tagError(sv *reflect.StructValue, idx1 []int, idx2 []int) os.Error { // unmarshalPaths walks down an XML structure looking for // wanted paths, and calls unmarshal on them. -func (p *Parser) unmarshalPaths(sv *reflect.StructValue, paths map[string]pathInfo, path string, start *StartElement) os.Error { +func (p *Parser) unmarshalPaths(sv reflect.Value, paths map[string]pathInfo, path string, start *StartElement) os.Error { if info, _ := paths[path]; info.complete { return p.unmarshal(sv.FieldByIndex(info.fieldIdx), start) } |