vitess.io/vitess@v0.16.2/go/mysql/collations/tools/makecolldata/main.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"encoding/json"
    21  	"log"
    22  	"os"
    23  	"path"
    24  	"path/filepath"
    25  	"sort"
    26  
    27  	"github.com/spf13/pflag"
    28  
    29  	"vitess.io/vitess/go/mysql/collations/internal/charset"
    30  	"vitess.io/vitess/go/mysql/collations/internal/uca"
    31  	"vitess.io/vitess/go/mysql/collations/tools/makecolldata/codegen"
    32  )
    33  
    34  type TailoringWeights map[string][]uint16
    35  
    36  type AllMetadata []*CollationMetadata
    37  
    38  type CollationMetadata struct {
    39  	Name    string
    40  	Charset string
    41  	Flags   struct {
    42  		Binary  bool
    43  		ASCII   bool
    44  		Default bool
    45  	}
    46  	CollationImpl  string
    47  	Number         uint
    48  	CType          []byte
    49  	ToLower        []byte
    50  	ToUpper        []byte
    51  	SortOrder      []byte
    52  	TabToUni       []uint16
    53  	TabFromUni     []charset.UnicodeMapping
    54  	UCAVersion     int
    55  	Weights        TailoringWeights
    56  	Contractions   []uca.Contraction
    57  	Reorder        [][4]uint16
    58  	UpperCaseFirst bool
    59  }
    60  
    61  var Mysqldata = pflag.String("mysqldata", "testdata/mysqldata", "")
    62  var Embed = pflag.Bool("embed", false, "")
    63  
    64  func loadMysqlMetadata() (all AllMetadata) {
    65  	mysqdata, err := filepath.Glob(path.Join(*Mysqldata, "*.json"))
    66  	if err != nil {
    67  		log.Fatal(err)
    68  	}
    69  
    70  	if len(mysqdata) == 0 {
    71  		log.Fatalf("no files under %q (did you run colldump locally?)", *Mysqldata)
    72  	}
    73  
    74  	for _, path := range mysqdata {
    75  		rf, err := os.Open(path)
    76  		if err != nil {
    77  			log.Fatal(err)
    78  		}
    79  
    80  		var meta CollationMetadata
    81  		if err := json.NewDecoder(rf).Decode(&meta); err != nil {
    82  			log.Fatal(err)
    83  		}
    84  		_ = rf.Close()
    85  
    86  		if _, aliased := CharsetAliases[meta.Charset]; aliased {
    87  			meta.Charset = CharsetAliases[meta.Charset]
    88  		}
    89  
    90  		all = append(all, &meta)
    91  	}
    92  
    93  	sort.Slice(all, func(i, j int) bool {
    94  		return all[i].Number < all[j].Number
    95  	})
    96  	return
    97  }
    98  
    99  func (all AllMetadata) get(name string) *CollationMetadata {
   100  	for _, meta := range all {
   101  		if meta.Name == name {
   102  			return meta
   103  		}
   104  	}
   105  	log.Fatalf("missing collation: %s", name)
   106  	return nil
   107  }
   108  
   109  const PkgCollations codegen.Package = "vitess.io/vitess/go/mysql/collations"
   110  const PkgCharset codegen.Package = "vitess.io/vitess/go/mysql/collations/internal/charset"
   111  
   112  func main() {
   113  	pflag.Parse()
   114  	metadata := loadMysqlMetadata()
   115  	maketables(*Embed, ".", metadata)
   116  	makeversions(".")
   117  	makemysqldata(".", metadata)
   118  }