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.
20
Make encoding/xml's API look more like the rest of the encoding packages.
22
http://codereview.appspot.com/5574053
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",
38
var isReader = map[string]bool{
40
"*bytes.Buffer": true,
41
"*bufio.Reader": true,
42
"*bufio.ReadWriter": true,
46
func xmlapi(f *ast.File) bool {
47
if !imports(f, "encoding/xml") {
51
typeof, _ := typecheck(xmlapiTypeConfig, f)
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"
61
if ok && isPkgDot(s, "xml", "Parser") {
62
s.Sel.Name = "Decoder"
67
call, ok := n.(*ast.CallExpr)
72
case len(call.Args) == 2 && isPkgDot(call.Fun, "xml", "Marshal"):
73
*call = xmlMarshal(call.Args)
75
case len(call.Args) == 2 && isPkgDot(call.Fun, "xml", "Unmarshal"):
76
if isReader[typeof[call.Args[0]]] {
77
*call = xmlUnmarshal(call.Args)
80
case len(call.Args) == 1 && isPkgDot(call.Fun, "xml", "NewParser"):
81
sel := call.Fun.(*ast.SelectorExpr).Sel
82
sel.Name = "NewDecoder"
89
func xmlMarshal(args []ast.Expr) ast.CallExpr {
90
return xmlCallChain("NewEncoder", "Encode", args)
93
func xmlUnmarshal(args []ast.Expr) ast.CallExpr {
94
return xmlCallChain("NewDecoder", "Decode", args)
97
func xmlCallChain(first, second string, args []ast.Expr) ast.CallExpr {
99
Fun: &ast.SelectorExpr{
101
Fun: &ast.SelectorExpr{
102
X: ast.NewIdent("xml"),
103
Sel: ast.NewIdent(first),
107
Sel: ast.NewIdent(second),