~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/cmd/gofix/httpfs.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

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 (
 
8
        "go/ast"
 
9
        "go/token"
 
10
)
 
11
 
 
12
var httpFileSystemFix = fix{
 
13
        "httpfs",
 
14
        httpfs,
 
15
        `Adapt http FileServer to take a FileSystem.
 
16
 
 
17
http://codereview.appspot.com/4629047  http FileSystem interface
 
18
`,
 
19
}
 
20
 
 
21
func init() {
 
22
        register(httpFileSystemFix)
 
23
}
 
24
 
 
25
func httpfs(f *ast.File) bool {
 
26
        if !imports(f, "http") {
 
27
                return false
 
28
        }
 
29
 
 
30
        fixed := false
 
31
        walk(f, func(n interface{}) {
 
32
                call, ok := n.(*ast.CallExpr)
 
33
                if !ok || !isPkgDot(call.Fun, "http", "FileServer") {
 
34
                        return
 
35
                }
 
36
                if len(call.Args) != 2 {
 
37
                        return
 
38
                }
 
39
                dir, prefix := call.Args[0], call.Args[1]
 
40
                call.Args = []ast.Expr{&ast.CallExpr{
 
41
                        Fun:  &ast.SelectorExpr{ast.NewIdent("http"), ast.NewIdent("Dir")},
 
42
                        Args: []ast.Expr{dir},
 
43
                }}
 
44
                wrapInStripHandler := true
 
45
                if prefixLit, ok := prefix.(*ast.BasicLit); ok {
 
46
                        if prefixLit.Kind == token.STRING && (prefixLit.Value == `"/"` || prefixLit.Value == `""`) {
 
47
                                wrapInStripHandler = false
 
48
                        }
 
49
                }
 
50
                if wrapInStripHandler {
 
51
                        call.Fun.(*ast.SelectorExpr).Sel = ast.NewIdent("StripPrefix")
 
52
                        call.Args = []ast.Expr{
 
53
                                prefix,
 
54
                                &ast.CallExpr{
 
55
                                        Fun:  &ast.SelectorExpr{ast.NewIdent("http"), ast.NewIdent("FileServer")},
 
56
                                        Args: call.Args,
 
57
                                },
 
58
                        }
 
59
                }
 
60
                fixed = true
 
61
        })
 
62
        return fixed
 
63
}