...

Source file src/cmd/fix/netipv6zone.go

Documentation: cmd/fix

     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  	name: "netipv6zone",
    15  	date: "2012-11-26",
    16  	f:    netipv6zone,
    17  	desc: `Adapt element key to IPAddr, UDPAddr or TCPAddr composite literals.
    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 any) {
    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 "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 elit, ok := e.(*ast.BasicLit); ok && elit.Value == "0" {
    55  						cl.Elts = append(cl.Elts[:i], cl.Elts[i+1:]...)
    56  					} else {
    57  						cl.Elts[i] = &ast.KeyValueExpr{
    58  							Key:   ast.NewIdent("Port"),
    59  							Value: e,
    60  						}
    61  					}
    62  				}
    63  				fixed = true
    64  			}
    65  		}
    66  	})
    67  	return fixed
    68  }
    69  

View as plain text