git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/toml/type_toml.go (about)

     1  package toml
     2  
     3  // tomlType represents any Go type that corresponds to a TOML type.
     4  // While the first draft of the TOML spec has a simplistic type system that
     5  // probably doesn't need this level of sophistication, we seem to be militating
     6  // toward adding real composite types.
     7  type tomlType interface {
     8  	typeString() string
     9  }
    10  
    11  // typeEqual accepts any two types and returns true if they are equal.
    12  func typeEqual(t1, t2 tomlType) bool {
    13  	if t1 == nil || t2 == nil {
    14  		return false
    15  	}
    16  	return t1.typeString() == t2.typeString()
    17  }
    18  
    19  func typeIsTable(t tomlType) bool {
    20  	return typeEqual(t, tomlHash) || typeEqual(t, tomlArrayHash)
    21  }
    22  
    23  type tomlBaseType string
    24  
    25  func (btype tomlBaseType) typeString() string {
    26  	return string(btype)
    27  }
    28  
    29  func (btype tomlBaseType) String() string {
    30  	return btype.typeString()
    31  }
    32  
    33  var (
    34  	tomlInteger   tomlBaseType = "Integer"
    35  	tomlFloat     tomlBaseType = "Float"
    36  	tomlDatetime  tomlBaseType = "Datetime"
    37  	tomlString    tomlBaseType = "String"
    38  	tomlBool      tomlBaseType = "Bool"
    39  	tomlArray     tomlBaseType = "Array"
    40  	tomlHash      tomlBaseType = "Hash"
    41  	tomlArrayHash tomlBaseType = "ArrayHash"
    42  )
    43  
    44  // typeOfPrimitive returns a tomlType of any primitive value in TOML.
    45  // Primitive values are: Integer, Float, Datetime, String and Bool.
    46  //
    47  // Passing a lexer item other than the following will cause a BUG message
    48  // to occur: itemString, itemBool, itemInteger, itemFloat, itemDatetime.
    49  func (p *parser) typeOfPrimitive(lexItem item) tomlType {
    50  	switch lexItem.typ {
    51  	case itemInteger:
    52  		return tomlInteger
    53  	case itemFloat:
    54  		return tomlFloat
    55  	case itemDatetime:
    56  		return tomlDatetime
    57  	case itemString:
    58  		return tomlString
    59  	case itemMultilineString:
    60  		return tomlString
    61  	case itemRawString:
    62  		return tomlString
    63  	case itemRawMultilineString:
    64  		return tomlString
    65  	case itemBool:
    66  		return tomlBool
    67  	}
    68  	p.bug("Cannot infer primitive type of lex item '%s'.", lexItem)
    69  	panic("unreachable")
    70  }