github.com/go-xe2/third@v1.0.3/golang.org/x/text/internal/cldrtree/option.go (about)

     1  // Copyright 2017 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 cldrtree
     6  
     7  import (
     8  	"reflect"
     9  
    10  	"github.com/go-xe2/third/golang.org/x/text/unicode/cldr"
    11  )
    12  
    13  // An Option configures an Index.
    14  type Option func(*options)
    15  
    16  type options struct {
    17  	parent *Index
    18  
    19  	name  string
    20  	alias *cldr.Common
    21  
    22  	sharedType  *typeInfo
    23  	sharedEnums *enum
    24  }
    25  
    26  func (o *options) fill(opt []Option) {
    27  	for _, f := range opt {
    28  		f(o)
    29  	}
    30  }
    31  
    32  // aliasOpt sets an alias from the given node, if the node defines one.
    33  func (o *options) setAlias(n Element) {
    34  	if n != nil && !reflect.ValueOf(n).IsNil() {
    35  		o.alias = n.GetCommon()
    36  	}
    37  }
    38  
    39  // Enum defines a enumeration type. The resulting option may be passed for the
    40  // construction of multiple Indexes, which they will share the same enum values.
    41  // Calling Gen on a Builder will generate the Enum for the given name. The
    42  // optional values fix the values for the given identifier to the argument
    43  // position (starting at 0). Other values may still be added and will be
    44  // assigned to subsequent values.
    45  func Enum(name string, value ...string) Option {
    46  	return EnumFunc(name, nil, value...)
    47  }
    48  
    49  // EnumFunc is like Enum but also takes a function that allows rewriting keys.
    50  func EnumFunc(name string, rename func(string) string, value ...string) Option {
    51  	enum := &enum{name: name, rename: rename, keyMap: map[string]enumIndex{}}
    52  	for _, e := range value {
    53  		enum.lookup(e)
    54  	}
    55  	return func(o *options) {
    56  		found := false
    57  		for _, e := range o.parent.meta.b.enums {
    58  			if e.name == enum.name {
    59  				found = true
    60  				break
    61  			}
    62  		}
    63  		if !found {
    64  			o.parent.meta.b.enums = append(o.parent.meta.b.enums, enum)
    65  		}
    66  		o.sharedEnums = enum
    67  	}
    68  }
    69  
    70  // SharedType returns an option which causes all Indexes to which this option is
    71  // passed to have the same type.
    72  func SharedType() Option {
    73  	info := &typeInfo{}
    74  	return func(o *options) { o.sharedType = info }
    75  }
    76  
    77  func useSharedType() Option {
    78  	return func(o *options) {
    79  		sub := o.parent.meta.typeInfo.keyTypeInfo
    80  		if sub == nil {
    81  			sub = &typeInfo{}
    82  			o.parent.meta.typeInfo.keyTypeInfo = sub
    83  		}
    84  		o.sharedType = sub
    85  	}
    86  }