~ubuntu-branches/ubuntu/saucy/golang/saucy

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2013-07-08 05:52:37 UTC
  • mfrom: (29.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130708055237-at01839e0hp8z3ni
Tags: 2:1.1-1ubuntu1
016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2011 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 "go/ast"
8
 
 
9
 
func init() {
10
 
        register(urlFix)
11
 
}
12
 
 
13
 
var urlFix = fix{
14
 
        "url",
15
 
        "2011-08-17",
16
 
        url,
17
 
        `Move the URL pieces of package http into a new package, url.
18
 
 
19
 
http://codereview.appspot.com/4893043
20
 
`,
21
 
}
22
 
 
23
 
var urlRenames = []struct{ in, out string }{
24
 
        {"URL", "URL"},
25
 
        {"ParseURL", "Parse"},
26
 
        {"ParseURLReference", "ParseWithReference"},
27
 
        {"ParseQuery", "ParseQuery"},
28
 
        {"Values", "Values"},
29
 
        {"URLEscape", "QueryEscape"},
30
 
        {"URLUnescape", "QueryUnescape"},
31
 
        {"URLError", "Error"},
32
 
        {"URLEscapeError", "EscapeError"},
33
 
}
34
 
 
35
 
func url(f *ast.File) bool {
36
 
        if imports(f, "url") || !imports(f, "http") {
37
 
                return false
38
 
        }
39
 
 
40
 
        fixed := false
41
 
 
42
 
        // Update URL code.
43
 
        urlWalk := func(n interface{}) {
44
 
                // Is it an identifier?
45
 
                if ident, ok := n.(*ast.Ident); ok && ident.Name == "url" {
46
 
                        ident.Name = "url_"
47
 
                        return
48
 
                }
49
 
                // Parameter and result names.
50
 
                if fn, ok := n.(*ast.FuncType); ok {
51
 
                        fixed = urlDoFields(fn.Params) || fixed
52
 
                        fixed = urlDoFields(fn.Results) || fixed
53
 
                }
54
 
        }
55
 
 
56
 
        // Fix up URL code and add import, at most once.
57
 
        fix := func() {
58
 
                if fixed {
59
 
                        return
60
 
                }
61
 
                addImport(f, "url")
62
 
                walkBeforeAfter(f, urlWalk, nop)
63
 
                fixed = true
64
 
        }
65
 
 
66
 
        walk(f, func(n interface{}) {
67
 
                // Rename functions and methods.
68
 
                if expr, ok := n.(ast.Expr); ok {
69
 
                        for _, s := range urlRenames {
70
 
                                if isPkgDot(expr, "http", s.in) {
71
 
                                        fix()
72
 
                                        expr.(*ast.SelectorExpr).X.(*ast.Ident).Name = "url"
73
 
                                        expr.(*ast.SelectorExpr).Sel.Name = s.out
74
 
                                        return
75
 
                                }
76
 
                        }
77
 
                }
78
 
        })
79
 
 
80
 
        // Remove the http import if no longer needed.
81
 
        if fixed && !usesImport(f, "http") {
82
 
                deleteImport(f, "http")
83
 
        }
84
 
 
85
 
        return fixed
86
 
}
87
 
 
88
 
func urlDoFields(list *ast.FieldList) (fixed bool) {
89
 
        if list == nil {
90
 
                return
91
 
        }
92
 
        for _, field := range list.List {
93
 
                for _, ident := range field.Names {
94
 
                        if ident.Name == "url" {
95
 
                                fixed = true
96
 
                                ident.Name = "url_"
97
 
                        }
98
 
                }
99
 
        }
100
 
        return
101
 
}