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