github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/patches/0007-remove-bazel-build-files.patch (about)

     1  From 33963cf2f14c72a60817f7d732bff2015f70f125 Mon Sep 17 00:00:00 2001
     2  From: Jeremy Yang <jyang@cockroachlabs.com>
     3  Date: Tue, 13 Feb 2024 13:35:35 -0800
     4  Subject: [PATCH] remove bazel build files
     5  
     6  ---
     7   pkg/build/bazel/bazel.go                  | 146 -------
     8   pkg/build/bazel/non_bazel.go              |  67 ----
     9   pkg/build/bazel/util/tinystringer/main.go | 452 ----------------------
    10   3 files changed, 665 deletions(-)
    11   delete mode 100644 pkg/build/bazel/bazel.go
    12   delete mode 100644 pkg/build/bazel/non_bazel.go
    13   delete mode 100644 pkg/build/bazel/util/tinystringer/main.go
    14  
    15  diff --git a/pkg/build/bazel/bazel.go b/pkg/build/bazel/bazel.go
    16  deleted file mode 100644
    17  index 2504468..0000000
    18  --- a/pkg/build/bazel/bazel.go
    19  +++ /dev/null
    20  @@ -1,146 +0,0 @@
    21  -// Copyright 2015 The Cockroach Authors.
    22  -//
    23  -// Use of this software is governed by the Business Source License
    24  -// included in the file licenses/BSL.txt.
    25  -//
    26  -// As of the Change Date specified in that file, in accordance with
    27  -// the Business Source License, use of this software will be governed
    28  -// by the Apache License, Version 2.0, included in the file
    29  -// licenses/APL.txt.
    30  -
    31  -//go:build bazel
    32  -// +build bazel
    33  -
    34  -package bazel
    35  -
    36  -import (
    37  -	"fmt"
    38  -	"os"
    39  -	"path"
    40  -	"path/filepath"
    41  -	"strconv"
    42  -	"strings"
    43  -
    44  -	inner "github.com/bazelbuild/rules_go/go/tools/bazel"
    45  -)
    46  -
    47  -// bazelTestEnvVar can be used to determine when running in the `bazel test`
    48  -// environment.
    49  -const bazelTestEnvVar = "BAZEL_TEST"
    50  -
    51  -// BuiltWithBazel returns true iff this library was built with Bazel.
    52  -func BuiltWithBazel() bool {
    53  -	return true
    54  -}
    55  -
    56  -// InBazelTest returns true iff called from a test run by Bazel.
    57  -func InBazelTest() bool {
    58  -	if bazelTestEnv, ok := os.LookupEnv(bazelTestEnvVar); ok {
    59  -		if bazelTest, err := strconv.ParseBool(bazelTestEnv); err == nil {
    60  -			return bazelTest
    61  -		}
    62  -	}
    63  -
    64  -	return false
    65  -}
    66  -
    67  -// InTestWrapper returns true iff called from Bazel's generated test wrapper.
    68  -// When enabled and running under `bazel test`, the entire test runs using a
    69  -// Bazel-generated wrapper. This wrapper imports the test module, so any
    70  -// import-time code will be run twice: once under the wrapper, and once by the
    71  -// test process itself. Hence, checking can be helpful for any module
    72  -// import-time code, such as init() or any global variable initialization.
    73  -// For more info, see:
    74  -// https://github.com/bazelbuild/rules_go/blob/master/docs/go/core/rules.md#go_test
    75  -//
    76  -// Duplicates logic from rules_go's bzltestutil.ShouldWrap(), but does not
    77  -// import in order to avoid a dependency (and its global initialization code).
    78  -func InTestWrapper() bool {
    79  -	if !InBazelTest() {
    80  -		return false
    81  -	}
    82  -
    83  -	if wrapEnv, ok := os.LookupEnv("GO_TEST_WRAP"); ok {
    84  -		wrap, err := strconv.ParseBool(wrapEnv)
    85  -		if err != nil {
    86  -			return false
    87  -		}
    88  -		return wrap
    89  -	}
    90  -	_, ok := os.LookupEnv("XML_OUTPUT_FILE")
    91  -	return ok
    92  -}
    93  -
    94  -// FindBinary is a convenience wrapper around the rules_go variant.
    95  -func FindBinary(pkg, name string) (string, bool) {
    96  -	return inner.FindBinary(pkg, name)
    97  -}
    98  -
    99  -// Runfile is a convenience wrapper around the rules_go variant.
   100  -func Runfile(path string) (string, error) {
   101  -	return inner.Runfile(path)
   102  -}
   103  -
   104  -// RunfilesPath is a convenience wrapper around the rules_go variant.
   105  -func RunfilesPath() (string, error) {
   106  -	return inner.RunfilesPath()
   107  -}
   108  -
   109  -// TestTmpDir is a convenience wrapper around the rules_go variant.
   110  -func TestTmpDir() string {
   111  -	return inner.TestTmpDir()
   112  -}
   113  -
   114  -// NewTmpDir is a convenience wrapper around the rules_go variant.
   115  -// The caller is responsible for cleaning the directory up after use.
   116  -func NewTmpDir(prefix string) (string, error) {
   117  -	return inner.NewTmpDir(prefix)
   118  -}
   119  -
   120  -// Updates the current environment to use the Go toolchain that Bazel built this
   121  -// binary/test with (updates the `PATH`/`GOROOT`/`GOCACHE` environment
   122  -// variables).
   123  -// If you want to use this function, your binary/test target MUST have
   124  -// `@go_sdk//:files` in its `data` -- this will make sure the whole toolchain
   125  -// gets pulled into the sandbox as well. Generally, this function should only
   126  -// be called in init().
   127  -func SetGoEnv() {
   128  -	gobin, err := Runfile("bin/go")
   129  -	if err != nil {
   130  -		panic(err)
   131  -	}
   132  -	if err := os.Setenv("PATH", fmt.Sprintf("%s%c%s", filepath.Dir(gobin), os.PathListSeparator, os.Getenv("PATH"))); err != nil {
   133  -		panic(err)
   134  -	}
   135  -	// GOPATH has to be set to some value (not equal to GOROOT) in order for `go env` to work.
   136  -	// See https://github.com/golang/go/issues/43938 for the details.
   137  -	// Specify a name under the system TEMP/TMP directory in order to be platform agnostic.
   138  -	if err := os.Setenv("GOPATH", filepath.Join(os.TempDir(), "nonexist-gopath")); err != nil {
   139  -		panic(err)
   140  -	}
   141  -	if err := os.Setenv("GOROOT", filepath.Dir(filepath.Dir(gobin))); err != nil {
   142  -		panic(err)
   143  -	}
   144  -	if err := os.Setenv("GOCACHE", path.Join(inner.TestTmpDir(), ".gocache")); err != nil {
   145  -		panic(err)
   146  -	}
   147  -}
   148  -
   149  -// Name of the environment variable containing the bazel target path
   150  -// (//pkg/cmd/foo:bar).
   151  -const testTargetEnv = "TEST_TARGET"
   152  -
   153  -// RelativeTestTargetPath returns relative path to the package
   154  -// of the current test.
   155  -func RelativeTestTargetPath() string {
   156  -	target := os.Getenv(testTargetEnv)
   157  -	if target == "" {
   158  -		return ""
   159  -	}
   160  -
   161  -	// Drop target name.
   162  -	if last := strings.LastIndex(target, ":"); last > 0 {
   163  -		target = target[:last]
   164  -	}
   165  -	return strings.TrimPrefix(target, "//")
   166  -}
   167  diff --git a/pkg/build/bazel/non_bazel.go b/pkg/build/bazel/non_bazel.go
   168  deleted file mode 100644
   169  index 17833fb..0000000
   170  --- a/pkg/build/bazel/non_bazel.go
   171  +++ /dev/null
   172  @@ -1,67 +0,0 @@
   173  -// Copyright 2015 The Cockroach Authors.
   174  -//
   175  -// Use of this software is governed by the Business Source License
   176  -// included in the file licenses/BSL.txt.
   177  -//
   178  -// As of the Change Date specified in that file, in accordance with
   179  -// the Business Source License, use of this software will be governed
   180  -// by the Apache License, Version 2.0, included in the file
   181  -// licenses/APL.txt.
   182  -
   183  -//go:build !bazel
   184  -// +build !bazel
   185  -
   186  -package bazel
   187  -
   188  -// This file contains stub implementations for non-bazel builds.
   189  -// See bazel.go for full documentation on the contracts of these functions.
   190  -
   191  -// BuiltWithBazel returns true iff this library was built with Bazel.
   192  -func BuiltWithBazel() bool {
   193  -	return false
   194  -}
   195  -
   196  -// InBazelTest returns true iff called from a test run by Bazel.
   197  -func InBazelTest() bool {
   198  -	return false
   199  -}
   200  -
   201  -// InTestWrapper returns true iff called from Bazel's generated test wrapper.
   202  -func InTestWrapper() bool {
   203  -	return false
   204  -}
   205  -
   206  -// FindBinary is not implemented.
   207  -func FindBinary(pkg, name string) (string, bool) {
   208  -	panic("not build with Bazel")
   209  -}
   210  -
   211  -// Runfile is not implemented.
   212  -func Runfile(string) (string, error) {
   213  -	panic("not built with Bazel")
   214  -}
   215  -
   216  -// RunfilesPath is not implemented.
   217  -func RunfilesPath() (string, error) {
   218  -	panic("not built with Bazel")
   219  -}
   220  -
   221  -// TestTmpDir is not implemented.
   222  -func TestTmpDir() string {
   223  -	panic("not built with Bazel")
   224  -}
   225  -
   226  -// NewTmpDir is not implemented.
   227  -func NewTmpDir(prefix string) (string, error) {
   228  -	panic("not built with Bazel")
   229  -}
   230  -
   231  -// RelativeTestTargetPath is not implemented.
   232  -func RelativeTestTargetPath() string {
   233  -	panic("not built with Bazel")
   234  -}
   235  -
   236  -// SetGoEnv is not implemented.
   237  -func SetGoEnv() {
   238  -	panic("not built with Bazel")
   239  -}
   240  diff --git a/pkg/build/bazel/util/tinystringer/main.go b/pkg/build/bazel/util/tinystringer/main.go
   241  deleted file mode 100644
   242  index b0b260a..0000000
   243  --- a/pkg/build/bazel/util/tinystringer/main.go
   244  +++ /dev/null
   245  @@ -1,452 +0,0 @@
   246  -// Copyright 2023 The Cockroach Authors.
   247  -//
   248  -// Use of this software is governed by the Business Source License
   249  -// included in the file licenses/BSL.txt.
   250  -//
   251  -// As of the Change Date specified in that file, in accordance with
   252  -// the Business Source License, use of this software will be governed
   253  -// by the Apache License, Version 2.0, included in the file
   254  -// licenses/APL.txt.
   255  -
   256  -package main
   257  -
   258  -import (
   259  -	"errors"
   260  -	"flag"
   261  -	"fmt"
   262  -	"go/ast"
   263  -	"go/parser"
   264  -	"go/token"
   265  -	"os"
   266  -	"path/filepath"
   267  -	"sort"
   268  -	"strconv"
   269  -	"strings"
   270  -)
   271  -
   272  -var (
   273  -	lineComment                                                             bool
   274  -	output, typeName, trimPrefix, stringToValueMapName, enumValuesSliceName string
   275  -	allowedIntegerTypes                                                     = []string{
   276  -		"byte",
   277  -		"int",
   278  -		"int8",
   279  -		"int16",
   280  -		"int32",
   281  -		"int64",
   282  -		"rune",
   283  -		"uint",
   284  -		"uint8",
   285  -		"uint16",
   286  -		"uint32",
   287  -		"uint64",
   288  -	}
   289  -)
   290  -
   291  -type tinyStringer struct {
   292  -	files                                                                   []string
   293  -	typeName, trimPrefix, output, stringToValueMapName, enumValuesSliceName string
   294  -	lineComment                                                             bool
   295  -}
   296  -
   297  -func init() {
   298  -	flag.StringVar(&stringToValueMapName, "stringtovaluemapname", "", "if set, also create a map of enum name -> value of the given name")
   299  -	flag.StringVar(&enumValuesSliceName, "enumvaluesslicename", "", "if set, also create a slice of all enum values of the given name")
   300  -	flag.StringVar(&output, "output", "", "name of output file; default srcdir/<type>_string.go")
   301  -	flag.StringVar(&typeName, "type", "", "the type for which to generate output")
   302  -	flag.StringVar(&trimPrefix, "trimprefix", "", "trim the given prefix from generated names")
   303  -	flag.BoolVar(&lineComment, "linecomment", false, "use line comment text as printed text when present")
   304  -}
   305  -
   306  -func main() {
   307  -	flag.Parse()
   308  -	if err := doMain(); err != nil {
   309  -		panic(err)
   310  -	}
   311  -}
   312  -
   313  -func doMain() error {
   314  -	if typeName == "" {
   315  -		return errors.New("must provide --type")
   316  -	}
   317  -	return tinyStringer{
   318  -		enumValuesSliceName:  enumValuesSliceName,
   319  -		files:                flag.Args(),
   320  -		lineComment:          lineComment,
   321  -		output:               output,
   322  -		stringToValueMapName: stringToValueMapName,
   323  -		typeName:             typeName,
   324  -		trimPrefix:           trimPrefix,
   325  -	}.stringify()
   326  -}
   327  -
   328  -func (s tinyStringer) stringify() error {
   329  -	if len(s.files) == 0 {
   330  -		return errors.New("must provide at least one file argument")
   331  -	}
   332  -	// Make sure all input files are in the same package.
   333  -	var srcDir, whichFile string
   334  -	for _, file := range s.files {
   335  -		dir := filepath.Dir(file)
   336  -		if srcDir == "" {
   337  -			srcDir = dir
   338  -			whichFile = file
   339  -		} else {
   340  -			if srcDir != dir {
   341  -				return fmt.Errorf("all input files must be in the same source directory; got input file %s in directory %s, but input file %s in directory %s", whichFile, srcDir, file, dir)
   342  -			}
   343  -		}
   344  -	}
   345  -	if s.output == "" {
   346  -		s.output = filepath.Join(srcDir, strings.ToLower(s.typeName)+"_string.go")
   347  -	}
   348  -
   349  -	parsedFiles, pkgName, err := parseAllFiles(s.files)
   350  -	if err != nil {
   351  -		return err
   352  -	}
   353  -	if err := validateType(parsedFiles, s.typeName); err != nil {
   354  -		return err
   355  -	}
   356  -
   357  -	inOrder, nameToInt, nameToPrinted, err := s.computeConstantValues(parsedFiles)
   358  -	if err != nil {
   359  -		return err
   360  -	}
   361  -
   362  -	if len(nameToInt) == 0 || len(nameToPrinted) == 0 {
   363  -		return fmt.Errorf("did not find enough constant values for type %s", s.typeName)
   364  -	}
   365  -
   366  -	// Produce s.output.
   367  -	outputFile, err := os.Create(s.output)
   368  -	if err != nil {
   369  -		return err
   370  -	}
   371  -	defer func() {
   372  -		_ = outputFile.Close()
   373  -	}()
   374  -	fmt.Fprintf(outputFile, `// Code generated by "stringer"; DO NOT EDIT.
   375  -
   376  -package %s
   377  -
   378  -import "strconv"
   379  -
   380  -func _() {
   381  -	// An "invalid array index" compiler error signifies that the constant values have changed.
   382  -	// Re-run the stringer command to generate them again.
   383  -	var x [1]struct{}
   384  -`, pkgName)
   385  -	for _, constName := range inOrder {
   386  -		if constName == "_" {
   387  -			continue
   388  -		}
   389  -		minus := "-"
   390  -		if nameToInt[constName] < 0 {
   391  -			// Implement the behavior of gofmt, which wants no space
   392  -			// between the operands unless the number on the right
   393  -			// is negative (would probably trigger some parse error).
   394  -			minus = " - "
   395  -		}
   396  -		fmt.Fprintf(outputFile, "	_ = x[%s%s%d]\n", constName, minus, nameToInt[constName])
   397  -	}
   398  -	receiverVar := "i"
   399  -	if _, ok := nameToInt[receiverVar]; ok {
   400  -		receiverVar = "_i"
   401  -		if _, ok := nameToInt[receiverVar]; ok {
   402  -			return fmt.Errorf("don't know how to choose a receiver variable because %s is a constant name", receiverVar)
   403  -		}
   404  -	}
   405  -	fmt.Fprintf(outputFile, `}
   406  -
   407  -func (%s %s) String() string {
   408  -	switch %s {
   409  -`, receiverVar, s.typeName, receiverVar)
   410  -	seen := make(map[int]struct{})
   411  -	for _, constName := range inOrder {
   412  -		if constName == "_" {
   413  -			continue
   414  -		}
   415  -		if _, ok := seen[nameToInt[constName]]; ok {
   416  -			continue
   417  -		}
   418  -		fmt.Fprintf(outputFile, `	case %s:
   419  -		return "%s"
   420  -`, constName, nameToPrinted[constName])
   421  -		seen[nameToInt[constName]] = struct{}{}
   422  -	}
   423  -	fmt.Fprintf(outputFile, `	default:
   424  -		return "%s(" + strconv.FormatInt(int64(i), 10) + ")"
   425  -	}
   426  -}
   427  -`, s.typeName)
   428  -	if s.stringToValueMapName != "" {
   429  -		fmt.Fprintf(outputFile, `
   430  -var %s = map[string]%s{
   431  -`, s.stringToValueMapName, s.typeName)
   432  -		// Figure out the length of the longest const name to see how
   433  -		// much we need to pad it out.
   434  -		var maxLen int
   435  -		for _, constName := range inOrder {
   436  -			if len(nameToPrinted[constName]) > maxLen {
   437  -				maxLen = len(nameToPrinted[constName])
   438  -			}
   439  -		}
   440  -		for _, constName := range inOrder {
   441  -			if constName == "_" {
   442  -				continue
   443  -			}
   444  -			padding := strings.Repeat(" ", 1+maxLen-len(nameToPrinted[constName]))
   445  -			fmt.Fprintf(outputFile, `	"%s":%s%d,
   446  -`, nameToPrinted[constName], padding, nameToInt[constName])
   447  -		}
   448  -		fmt.Fprintf(outputFile, `}
   449  -`)
   450  -	}
   451  -	if s.enumValuesSliceName != "" {
   452  -		seen := make(map[int]struct{})
   453  -		fmt.Fprintf(outputFile, `
   454  -var %s = []%s{
   455  -`, s.enumValuesSliceName, s.typeName)
   456  -		inLexicographicOrder := make([]string, len(inOrder))
   457  -		copy(inLexicographicOrder, inOrder)
   458  -		// Clear duplicates, select the first one in order.
   459  -		i := 0
   460  -		for i < len(inLexicographicOrder) {
   461  -			constName := inLexicographicOrder[i]
   462  -			if _, ok := seen[nameToInt[constName]]; ok {
   463  -				inLexicographicOrder = append(inLexicographicOrder[:i], inLexicographicOrder[i+1:]...)
   464  -			} else {
   465  -				i += 1
   466  -				seen[nameToInt[constName]] = struct{}{}
   467  -			}
   468  -		}
   469  -		sort.Slice(inLexicographicOrder, func(i, j int) bool {
   470  -			return nameToPrinted[inLexicographicOrder[i]] < nameToPrinted[inLexicographicOrder[j]]
   471  -		})
   472  -		seen = make(map[int]struct{})
   473  -		for _, constName := range inLexicographicOrder {
   474  -			if constName == "_" {
   475  -				continue
   476  -			}
   477  -			if _, ok := seen[nameToInt[constName]]; ok {
   478  -				continue
   479  -			}
   480  -			fmt.Fprintf(outputFile, `	%s,
   481  -`, constName)
   482  -			seen[nameToInt[constName]] = struct{}{}
   483  -		}
   484  -		fmt.Fprintf(outputFile, `}
   485  -`)
   486  -	}
   487  -
   488  -	return nil
   489  -}
   490  -
   491  -// parseAllFiles returns a list of all the files parsed, the name of the package, and an error if one occurred.
   492  -func parseAllFiles(files []string) ([]*ast.File, string, error) {
   493  -	// Parse all files.
   494  -	fset := token.NewFileSet()
   495  -	parsedFiles := make([]*ast.File, 0, len(files))
   496  -	for _, file := range files {
   497  -		parsed, err := parser.ParseFile(fset, file, nil, parser.SkipObjectResolution|parser.ParseComments)
   498  -		if err != nil {
   499  -			return nil, "", err
   500  -		}
   501  -		parsedFiles = append(parsedFiles, parsed)
   502  -	}
   503  -	// All files should have the same package declaration. This will help us
   504  -	// determine what package the generated file should be in.
   505  -	var pkgName, whichFile string
   506  -	for i, file := range parsedFiles {
   507  -		if pkgName == "" {
   508  -			pkgName = file.Name.Name
   509  -			whichFile = files[i]
   510  -		} else {
   511  -			if pkgName != file.Name.Name {
   512  -				return nil, "", fmt.Errorf("all input files must have the same package name; got input file %s w/ 'package %s', but input file %s w/ 'package %s'", whichFile, pkgName, files[i], file.Name.Name)
   513  -			}
   514  -		}
   515  -	}
   516  -	return parsedFiles, pkgName, nil
   517  -}
   518  -
   519  -func validateType(files []*ast.File, typeName string) error {
   520  -	// Find the definition of the type. Should be an alias for some
   521  -	// integer type.
   522  -	for _, file := range files {
   523  -		for _, decl := range file.Decls {
   524  -			var genDecl *ast.GenDecl
   525  -			genDecl, ok := decl.(*ast.GenDecl)
   526  -			if !ok {
   527  -				continue
   528  -			}
   529  -			if genDecl.Tok != token.TYPE {
   530  -				continue
   531  -			}
   532  -			for _, spec := range genDecl.Specs {
   533  -				typeSpec, ok := spec.(*ast.TypeSpec)
   534  -				if !ok {
   535  -					// Should never happen.
   536  -					return fmt.Errorf("unexpected error occurred while processing %+v", spec)
   537  -				}
   538  -				if typeSpec.Name.Name != typeName {
   539  -					continue
   540  -				}
   541  -				// Ensure the type is an alias for a built-in integer type.
   542  -				ident, ok := typeSpec.Type.(*ast.Ident)
   543  -				if !ok {
   544  -					return fmt.Errorf("expected identifier for definition of type %s", typeName)
   545  -				}
   546  -				var found bool
   547  -				for _, intType := range allowedIntegerTypes {
   548  -					if ident.Name == intType {
   549  -						found = true
   550  -						break
   551  -					}
   552  -
   553  -				}
   554  -				if !found {
   555  -					return fmt.Errorf("expected an integer type for definition of type %s; got %s", typeName, ident.Name)
   556  -				}
   557  -			}
   558  -		}
   559  -	}
   560  -	return nil
   561  -}
   562  -
   563  -func (s tinyStringer) computeConstantValues(
   564  -	files []*ast.File,
   565  -) (inOrder []string, nameToInt map[string]int, nameToPrinted map[string]string, err error) {
   566  -	nameToInt = make(map[string]int)
   567  -	nameToPrinted = make(map[string]string)
   568  -
   569  -	for _, file := range files {
   570  -		for _, decl := range file.Decls {
   571  -			var genDecl *ast.GenDecl
   572  -			genDecl, ok := decl.(*ast.GenDecl)
   573  -			if !ok {
   574  -				continue
   575  -			}
   576  -			if genDecl.Tok != token.CONST {
   577  -				continue
   578  -			}
   579  -			var inferAppropriateType, inIota bool
   580  -			var iotaVal int
   581  -			for _, spec := range genDecl.Specs {
   582  -				valueSpec, ok := spec.(*ast.ValueSpec)
   583  -				if !ok {
   584  -					// Should never happen.
   585  -					err = fmt.Errorf("unexpected error occurred while processing %+v", spec)
   586  -					return
   587  -				}
   588  -				if valueSpec.Type == nil && !inferAppropriateType {
   589  -					continue
   590  -				}
   591  -				ident, ok := valueSpec.Type.(*ast.Ident)
   592  -				if (ok && ident.Name != s.typeName) || (!ok && !inferAppropriateType) {
   593  -					inferAppropriateType = false
   594  -					continue
   595  -				}
   596  -				inferAppropriateType = true
   597  -				if len(valueSpec.Names) != 1 {
   598  -					err = fmt.Errorf("expected one name for constant of type %s; found %+v", s.typeName, valueSpec.Names)
   599  -					return
   600  -				}
   601  -				constName := valueSpec.Names[0].Name
   602  -				inOrder = append(inOrder, constName)
   603  -				// Check the value to see what value we'll assign to the constant.
   604  -				if valueSpec.Values == nil {
   605  -					if inIota {
   606  -						nameToInt[constName] = iotaVal
   607  -						iotaVal += 1
   608  -					} else {
   609  -						nameToInt[constName] = 0
   610  -					}
   611  -				} else if len(valueSpec.Values) != 1 {
   612  -					err = fmt.Errorf("expected one value for constant %s; found %+v", constName, valueSpec.Values)
   613  -					return
   614  -				} else if lit, ok := valueSpec.Values[0].(*ast.BasicLit); ok {
   615  -					if lit.Kind == token.INT {
   616  -						var intVal int64
   617  -						intVal, err = strconv.ParseInt(lit.Value, 0, 0)
   618  -						if err != nil {
   619  -							return
   620  -						}
   621  -						nameToInt[constName] = int(intVal)
   622  -						inIota = false
   623  -					} else if lit.Kind == token.CHAR {
   624  -						if len(lit.Value) != 3 {
   625  -							err = fmt.Errorf("expected string of form 'X' for character: got %s", lit.Value)
   626  -							return
   627  -						}
   628  -						if lit.Value[0] != '\'' || lit.Value[2] != '\'' {
   629  -							err = fmt.Errorf("expected string of form 'X' for character: got %s", lit.Value)
   630  -							return
   631  -						}
   632  -						nameToInt[constName] = int(lit.Value[1])
   633  -						inIota = false
   634  -					} else {
   635  -						err = fmt.Errorf("expected integer value for constant %s; found %s", constName, lit.Value)
   636  -						return
   637  -					}
   638  -				} else if ident, ok := valueSpec.Values[0].(*ast.Ident); ok {
   639  -					if ident.Name == "iota" {
   640  -						inIota = true
   641  -						nameToInt[constName] = iotaVal
   642  -						iotaVal += 1
   643  -					} else if otherValue, ok := nameToInt[ident.Name]; ok {
   644  -						nameToInt[constName] = otherValue
   645  -						inIota = false
   646  -					}
   647  -				} else if binExpr, ok := valueSpec.Values[0].(*ast.BinaryExpr); ok {
   648  -					// Handle iota + N or iota - N.
   649  -					iotaIdent, ok := binExpr.X.(*ast.Ident)
   650  -					if !ok || iotaIdent.Name != "iota" {
   651  -						err = fmt.Errorf("expected 'iota' in binary expression %+v; found %+v", binExpr, binExpr.X)
   652  -						return
   653  -					}
   654  -					var otherNumParsed int64
   655  -					if otherNum, ok := binExpr.Y.(*ast.BasicLit); ok && otherNum.Kind == token.INT {
   656  -						otherNumParsed, err = strconv.ParseInt(otherNum.Value, 0, 0)
   657  -						if err != nil {
   658  -							return
   659  -						}
   660  -					} else if otherRef, ok := binExpr.Y.(*ast.Ident); ok {
   661  -						otherNum, ok := nameToInt[otherRef.Name]
   662  -						if !ok {
   663  -							err = fmt.Errorf("could not find value of %s", otherRef.Name)
   664  -							return
   665  -						}
   666  -						otherNumParsed = int64(otherNum)
   667  -					} else {
   668  -						err = fmt.Errorf("couldn't parse second argument of binary expression %+v; found %+v", binExpr, binExpr.Y)
   669  -						return
   670  -					}
   671  -					if binExpr.Op == token.ADD {
   672  -						iotaVal = iotaVal + int(otherNumParsed)
   673  -					} else if binExpr.Op == token.SUB {
   674  -						iotaVal = iotaVal - int(otherNumParsed)
   675  -					}
   676  -					inIota = true
   677  -					nameToInt[constName] = iotaVal
   678  -					iotaVal += 1
   679  -				} else {
   680  -					err = fmt.Errorf("don't know how to process %+v", valueSpec.Values[0])
   681  -					return
   682  -				}
   683  -
   684  -				// Determine the printed name of the constant.
   685  -				printedName := constName
   686  -				if s.lineComment && valueSpec.Comment != nil {
   687  -					printedName = strings.TrimSpace(valueSpec.Comment.Text())
   688  -				}
   689  -				if s.trimPrefix != "" {
   690  -					printedName = strings.TrimPrefix(printedName, s.trimPrefix)
   691  -				}
   692  -				nameToPrinted[constName] = printedName
   693  -			}
   694  -		}
   695  -	}
   696  -	return
   697  -}
   698  -- 
   699  2.38.1
   700