github.com/grailbio/bigslice@v0.0.0-20230519005545-30c4c12152ad/frame/genops.go (about) 1 // Copyright 2018 GRAIL, Inc. All rights reserved. 2 // Use of this source code is governed by the Apache 2.0 3 // license that can be found in the LICENSE file. 4 5 // +build ignore 6 7 // Gentype generates kernels for standard types. 8 // This program is run via "go generate". 9 10 package main 11 12 import ( 13 "bytes" 14 "go/format" 15 "io/ioutil" 16 "log" 17 "strings" 18 "text/template" 19 ) 20 21 var types = []string{ 22 "string", 23 24 "uint", 25 "uint8", // also byte 26 "uint16", 27 "uint32", 28 "uint64", 29 30 "int", 31 "int8", 32 "int16", 33 "int32", // also rune 34 "int64", 35 36 "float32", 37 "float64", 38 39 "uintptr", 40 } 41 42 func main() { 43 log.SetFlags(0) 44 log.SetPrefix("") 45 46 type typeInfo struct { 47 Type string 48 TypeCap string 49 ValueMethod string 50 } 51 infos := make([]typeInfo, len(types)) 52 for i, typ := range types { 53 infos[i].Type = typ 54 infos[i].TypeCap = strings.Title(typ) 55 switch { 56 case strings.HasPrefix(typ, "uint"): 57 infos[i].ValueMethod = "Uint" 58 case strings.HasPrefix(typ, "int"): 59 infos[i].ValueMethod = "Int" 60 case strings.HasPrefix(typ, "float"): 61 infos[i].ValueMethod = "Float" 62 case typ == "string": 63 infos[i].ValueMethod = "String" 64 default: 65 log.Fatalf("no value method for type %s", typ) 66 } 67 } 68 for _, file := range []string{"ops_builtin.go"} { 69 tmpl, err := template.ParseFiles(file + "template") 70 if err != nil { 71 log.Fatal(err) 72 } 73 var b bytes.Buffer 74 if err := tmpl.Execute(&b, infos); err != nil { 75 log.Fatal(err) 76 } 77 src, err := format.Source(b.Bytes()) 78 if err != nil { 79 log.Println(b.String()) 80 log.Fatalf("generated code is invalid: %v", err) 81 } 82 if err := ioutil.WriteFile(file, src, 0644); err != nil { 83 log.Fatal(err) 84 } 85 } 86 }