github.com/songzhibin97/gkit@v1.2.13/structure/hashset/types_gen.go (about)

     1  // Copyright 2021 ByteDance Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build ignore
    16  // +build ignore
    17  
    18  package main
    19  
    20  import (
    21  	"bytes"
    22  	"go/format"
    23  	"io/ioutil"
    24  	"os"
    25  	"strings"
    26  )
    27  
    28  var lengthFunction = `func (s Int64Set) Len() int {
    29  	return len(s)
    30  }`
    31  
    32  func main() {
    33  	f, err := os.Open("hashset.go")
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  	filedata, err := ioutil.ReadAll(f)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	w := new(bytes.Buffer)
    42  	w.WriteString(`// Copyright 2021 ByteDance Inc.
    43  //
    44  // Licensed under the Apache License, Version 2.0 (the "License");
    45  // you may not use this file except in compliance with the License.
    46  // You may obtain a copy of the License at
    47  //
    48  //     http://www.apache.org/licenses/LICENSE-2.0
    49  //
    50  // Unless required by applicable law or agreed to in writing, software
    51  // distributed under the License is distributed on an "AS IS" BASIS,
    52  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    53  // See the License for the specific language governing permissions and
    54  // limitations under the License.
    55  
    56  // Code generated by go run types_gen.go; DO NOT EDIT.` + "\r\n\n")
    57  	start_pos := strings.Index(string(filedata), "package hashset")
    58  	w.WriteString(string(filedata)[start_pos : start_pos+len("package hashset")])
    59  	ts := []string{"Byte", "Complex64", "Complex128", "Float32", "Float64", "Int", "Int8", "Int16", "Int32", "Rune", "String", "Uint", "Uint8", "Uint16", "Uint32", "Uint64", "Uintptr"} // all types need to be converted
    60  
    61  	for _, upper := range ts {
    62  		lower := strings.ToLower(upper)
    63  		data := string(filedata)
    64  		// Remove header.
    65  		data = data[start_pos+len("package hashset"):]
    66  		// Remove the special case.
    67  		data = strings.Replace(data, lengthFunction, "", -1)
    68  		// Common cases.
    69  		data = strings.Replace(data, "int64", lower, -1)
    70  		data = strings.Replace(data, "Int64", upper, -1)
    71  		if inSlice(lowerSlice(ts), lower) {
    72  			data = strings.Replace(data, "length "+lower, "length int64", 1)
    73  		}
    74  		// Add the special case.
    75  		data = data + strings.Replace(lengthFunction, "Int64Set", upper+"Set", 1)
    76  		w.WriteString(data)
    77  		w.WriteString("\r\n")
    78  	}
    79  
    80  	out, err := format.Source(w.Bytes())
    81  	if err != nil {
    82  		panic(err)
    83  	}
    84  	if err := ioutil.WriteFile("types.go", out, 0660); err != nil {
    85  		panic(err)
    86  	}
    87  }
    88  
    89  func lowerSlice(s []string) []string {
    90  	n := make([]string, len(s))
    91  	for i, v := range s {
    92  		n[i] = strings.ToLower(v)
    93  	}
    94  	return n
    95  }
    96  
    97  func inSlice(s []string, val string) bool {
    98  	for _, v := range s {
    99  		if v == val {
   100  			return true
   101  		}
   102  	}
   103  	return false
   104  }