LEFT | RIGHT |
(no file at all) | |
| 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 "go/ast" |
| 8 |
| 9 func init() { |
| 10 register(netipv6zoneFix) |
| 11 } |
| 12 |
| 13 var netipv6zoneFix = fix{ |
| 14 "netipv6zone", |
| 15 "2012-11-26", |
| 16 netipv6zone, |
| 17 `Adapt element key to IPNet, IPAddr, UDPAddr or TCPAddr composite litera
ls. |
| 18 |
| 19 https://codereview.appspot.com/6849045/ |
| 20 `, |
| 21 } |
| 22 |
| 23 func netipv6zone(f *ast.File) bool { |
| 24 if !imports(f, "net") { |
| 25 return false |
| 26 } |
| 27 |
| 28 fixed := false |
| 29 walk(f, func(n interface{}) { |
| 30 cl, ok := n.(*ast.CompositeLit) |
| 31 if !ok { |
| 32 return |
| 33 } |
| 34 se, ok := cl.Type.(*ast.SelectorExpr) |
| 35 if !ok { |
| 36 return |
| 37 } |
| 38 if !isTopName(se.X, "net") || se.Sel == nil { |
| 39 return |
| 40 } |
| 41 switch ss := se.Sel.String(); ss { |
| 42 case "IPNet", "IPAddr", "UDPAddr", "TCPAddr": |
| 43 for i, e := range cl.Elts { |
| 44 if _, ok := e.(*ast.KeyValueExpr); ok { |
| 45 break |
| 46 } |
| 47 switch i { |
| 48 case 0: |
| 49 cl.Elts[i] = &ast.KeyValueExpr{ |
| 50 Key: ast.NewIdent("IP"), |
| 51 Value: e, |
| 52 } |
| 53 case 1: |
| 54 if ss == "IPNet" { |
| 55 cl.Elts[i] = &ast.KeyValueExpr{ |
| 56 Key: ast.NewIdent("Mas
k"), |
| 57 Value: e, |
| 58 } |
| 59 } else { |
| 60 cl.Elts[i] = &ast.KeyValueExpr{ |
| 61 Key: ast.NewIdent("Por
t"), |
| 62 Value: e, |
| 63 } |
| 64 } |
| 65 } |
| 66 fixed = true |
| 67 } |
| 68 } |
| 69 }) |
| 70 return fixed |
| 71 } |
LEFT | RIGHT |