type_toml.go (1929B)
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 { return string(btype) } 26 func (btype tomlBaseType) String() string { return btype.typeString() } 27 28 var ( 29 tomlInteger tomlBaseType = "Integer" 30 tomlFloat tomlBaseType = "Float" 31 tomlDatetime tomlBaseType = "Datetime" 32 tomlString tomlBaseType = "String" 33 tomlBool tomlBaseType = "Bool" 34 tomlArray tomlBaseType = "Array" 35 tomlHash tomlBaseType = "Hash" 36 tomlArrayHash tomlBaseType = "ArrayHash" 37 ) 38 39 // typeOfPrimitive returns a tomlType of any primitive value in TOML. 40 // Primitive values are: Integer, Float, Datetime, String and Bool. 41 // 42 // Passing a lexer item other than the following will cause a BUG message 43 // to occur: itemString, itemBool, itemInteger, itemFloat, itemDatetime. 44 func (p *parser) typeOfPrimitive(lexItem item) tomlType { 45 switch lexItem.typ { 46 case itemInteger: 47 return tomlInteger 48 case itemFloat: 49 return tomlFloat 50 case itemDatetime: 51 return tomlDatetime 52 case itemString, itemStringEsc: 53 return tomlString 54 case itemMultilineString: 55 return tomlString 56 case itemRawString: 57 return tomlString 58 case itemRawMultilineString: 59 return tomlString 60 case itemBool: 61 return tomlBool 62 } 63 p.bug("Cannot infer primitive type of lex item '%s'.", lexItem) 64 panic("unreachable") 65 }