~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/cmd/fix/xmlapi.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2012 The Go Authors.  All rights reserved.
2
 
// Use of this source code is governed by a BSD-style
3
 
// license that can be found in the LICENSE file.
4
 
 
5
 
package main
6
 
 
7
 
import (
8
 
        "go/ast"
9
 
)
10
 
 
11
 
func init() {
12
 
        register(xmlapiFix)
13
 
}
14
 
 
15
 
var xmlapiFix = fix{
16
 
        "xmlapi",
17
 
        "2012-01-23",
18
 
        xmlapi,
19
 
        `
20
 
        Make encoding/xml's API look more like the rest of the encoding packages.
21
 
 
22
 
http://codereview.appspot.com/5574053
23
 
`,
24
 
}
25
 
 
26
 
var xmlapiTypeConfig = &TypeConfig{
27
 
        Func: map[string]string{
28
 
                "xml.NewParser":         "*xml.Parser",
29
 
                "os.Open":               "*os.File",
30
 
                "os.OpenFile":           "*os.File",
31
 
                "bytes.NewBuffer":       "*bytes.Buffer",
32
 
                "bytes.NewBufferString": "*bytes.Buffer",
33
 
                "bufio.NewReader":       "*bufio.Reader",
34
 
                "bufio.NewReadWriter":   "*bufio.ReadWriter",
35
 
        },
36
 
}
37
 
 
38
 
var isReader = map[string]bool{
39
 
        "*os.File":          true,
40
 
        "*bytes.Buffer":     true,
41
 
        "*bufio.Reader":     true,
42
 
        "*bufio.ReadWriter": true,
43
 
        "io.Reader":         true,
44
 
}
45
 
 
46
 
func xmlapi(f *ast.File) bool {
47
 
        if !imports(f, "encoding/xml") {
48
 
                return false
49
 
        }
50
 
 
51
 
        typeof, _ := typecheck(xmlapiTypeConfig, f)
52
 
 
53
 
        fixed := false
54
 
        walk(f, func(n interface{}) {
55
 
                s, ok := n.(*ast.SelectorExpr)
56
 
                if ok && typeof[s.X] == "*xml.Parser" && s.Sel.Name == "Unmarshal" {
57
 
                        s.Sel.Name = "DecodeElement"
58
 
                        fixed = true
59
 
                        return
60
 
                }
61
 
                if ok && isPkgDot(s, "xml", "Parser") {
62
 
                        s.Sel.Name = "Decoder"
63
 
                        fixed = true
64
 
                        return
65
 
                }
66
 
 
67
 
                call, ok := n.(*ast.CallExpr)
68
 
                if !ok {
69
 
                        return
70
 
                }
71
 
                switch {
72
 
                case len(call.Args) == 2 && isPkgDot(call.Fun, "xml", "Marshal"):
73
 
                        *call = xmlMarshal(call.Args)
74
 
                        fixed = true
75
 
                case len(call.Args) == 2 && isPkgDot(call.Fun, "xml", "Unmarshal"):
76
 
                        if isReader[typeof[call.Args[0]]] {
77
 
                                *call = xmlUnmarshal(call.Args)
78
 
                                fixed = true
79
 
                        }
80
 
                case len(call.Args) == 1 && isPkgDot(call.Fun, "xml", "NewParser"):
81
 
                        sel := call.Fun.(*ast.SelectorExpr).Sel
82
 
                        sel.Name = "NewDecoder"
83
 
                        fixed = true
84
 
                }
85
 
        })
86
 
        return fixed
87
 
}
88
 
 
89
 
func xmlMarshal(args []ast.Expr) ast.CallExpr {
90
 
        return xmlCallChain("NewEncoder", "Encode", args)
91
 
}
92
 
 
93
 
func xmlUnmarshal(args []ast.Expr) ast.CallExpr {
94
 
        return xmlCallChain("NewDecoder", "Decode", args)
95
 
}
96
 
 
97
 
func xmlCallChain(first, second string, args []ast.Expr) ast.CallExpr {
98
 
        return ast.CallExpr{
99
 
                Fun: &ast.SelectorExpr{
100
 
                        X: &ast.CallExpr{
101
 
                                Fun: &ast.SelectorExpr{
102
 
                                        X:   ast.NewIdent("xml"),
103
 
                                        Sel: ast.NewIdent(first),
104
 
                                },
105
 
                                Args: args[:1],
106
 
                        },
107
 
                        Sel: ast.NewIdent(second),
108
 
                },
109
 
                Args: args[1:2],
110
 
        }
111
 
}