metadata.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. // Copyright (c) 2015 The gocql 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. package gocql
  5. import (
  6. "encoding/hex"
  7. "encoding/json"
  8. "fmt"
  9. "log"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. )
  14. // schema metadata for a keyspace
  15. type KeyspaceMetadata struct {
  16. Name string
  17. DurableWrites bool
  18. StrategyClass string
  19. StrategyOptions map[string]interface{}
  20. Tables map[string]*TableMetadata
  21. }
  22. // schema metadata for a table (a.k.a. column family)
  23. type TableMetadata struct {
  24. Keyspace string
  25. Name string
  26. KeyValidator string
  27. Comparator string
  28. DefaultValidator string
  29. KeyAliases []string
  30. ColumnAliases []string
  31. ValueAlias string
  32. PartitionKey []*ColumnMetadata
  33. ClusteringColumns []*ColumnMetadata
  34. Columns map[string]*ColumnMetadata
  35. }
  36. // schema metadata for a column
  37. type ColumnMetadata struct {
  38. Keyspace string
  39. Table string
  40. Name string
  41. ComponentIndex int
  42. Kind string
  43. Validator string
  44. Type TypeInfo
  45. Order ColumnOrder
  46. Index ColumnIndexMetadata
  47. }
  48. // the ordering of the column with regard to its comparator
  49. type ColumnOrder bool
  50. const (
  51. ASC ColumnOrder = false
  52. DESC = true
  53. )
  54. type ColumnIndexMetadata struct {
  55. Name string
  56. Type string
  57. Options map[string]interface{}
  58. }
  59. // Column kind values
  60. const (
  61. PARTITION_KEY = "partition_key"
  62. CLUSTERING_KEY = "clustering_key"
  63. REGULAR = "regular"
  64. )
  65. // default alias values
  66. const (
  67. DEFAULT_KEY_ALIAS = "key"
  68. DEFAULT_COLUMN_ALIAS = "column"
  69. DEFAULT_VALUE_ALIAS = "value"
  70. )
  71. // queries the cluster for schema information for a specific keyspace
  72. type schemaDescriber struct {
  73. session *Session
  74. mu sync.Mutex
  75. cache map[string]*KeyspaceMetadata
  76. }
  77. func newSchemaDescriber(session *Session) *schemaDescriber {
  78. return &schemaDescriber{
  79. session: session,
  80. cache: map[string]*KeyspaceMetadata{},
  81. }
  82. }
  83. func (s *schemaDescriber) getSchema(keyspaceName string) (*KeyspaceMetadata, error) {
  84. s.mu.Lock()
  85. defer s.mu.Unlock()
  86. // TODO handle schema change events
  87. metadata, found := s.cache[keyspaceName]
  88. if !found {
  89. // refresh the cache for this keyspace
  90. err := s.refreshSchema(keyspaceName)
  91. if err != nil {
  92. return nil, err
  93. }
  94. metadata = s.cache[keyspaceName]
  95. }
  96. return metadata, nil
  97. }
  98. func (s *schemaDescriber) refreshSchema(keyspaceName string) error {
  99. var err error
  100. // query the system keyspace for schema data
  101. // TODO retrieve concurrently
  102. keyspace, err := getKeyspaceMetadata(s.session, keyspaceName)
  103. if err != nil {
  104. return err
  105. }
  106. tables, err := getTableMetadata(s.session, keyspaceName)
  107. if err != nil {
  108. return err
  109. }
  110. columns, err := getColumnMetadata(s.session, keyspaceName)
  111. if err != nil {
  112. return err
  113. }
  114. // organize the schema data
  115. compileMetadata(s.session.cfg.ProtoVersion, keyspace, tables, columns)
  116. // update the cache
  117. s.cache[keyspaceName] = keyspace
  118. return nil
  119. }
  120. // "compiles" keyspace, table, and column metadata for a keyspace together
  121. // linking the metadata objects together and calculating the partition key
  122. // and clustering key.
  123. func compileMetadata(
  124. protoVersion int,
  125. keyspace *KeyspaceMetadata,
  126. tables []TableMetadata,
  127. columns []ColumnMetadata,
  128. ) {
  129. keyspace.Tables = make(map[string]*TableMetadata)
  130. for i := range tables {
  131. tables[i].Columns = make(map[string]*ColumnMetadata)
  132. keyspace.Tables[tables[i].Name] = &tables[i]
  133. }
  134. // add columns from the schema data
  135. for i := range columns {
  136. // decode the validator for TypeInfo and order
  137. validatorParsed := parseType(columns[i].Validator)
  138. columns[i].Type = validatorParsed.types[0]
  139. columns[i].Order = ASC
  140. if validatorParsed.reversed[0] {
  141. columns[i].Order = DESC
  142. }
  143. table := keyspace.Tables[columns[i].Table]
  144. table.Columns[columns[i].Name] = &columns[i]
  145. }
  146. if protoVersion == 1 {
  147. compileV1Metadata(tables)
  148. } else {
  149. compileV2Metadata(tables)
  150. }
  151. }
  152. // V1 protocol does not return as much column metadata as V2+ so determining
  153. // PartitionKey and ClusterColumns is more complex
  154. func compileV1Metadata(tables []TableMetadata) {
  155. for i := range tables {
  156. table := &tables[i]
  157. // decode the key validator
  158. keyValidatorParsed := parseType(table.KeyValidator)
  159. // decode the comparator
  160. comparatorParsed := parseType(table.Comparator)
  161. // the partition key length is the same as the number of types in the
  162. // key validator
  163. table.PartitionKey = make([]*ColumnMetadata, len(keyValidatorParsed.types))
  164. // V1 protocol only returns "regular" columns from
  165. // system.schema_columns (there is no type field for columns)
  166. // so the alias information is used to
  167. // create the partition key and clustering columns
  168. // construct the partition key from the alias
  169. for i := range table.PartitionKey {
  170. var alias string
  171. if len(table.KeyAliases) > i {
  172. alias = table.KeyAliases[i]
  173. } else if i == 0 {
  174. alias = DEFAULT_KEY_ALIAS
  175. } else {
  176. alias = DEFAULT_KEY_ALIAS + strconv.Itoa(i+1)
  177. }
  178. column := &ColumnMetadata{
  179. Keyspace: table.Keyspace,
  180. Table: table.Name,
  181. Name: alias,
  182. Type: keyValidatorParsed.types[i],
  183. Kind: PARTITION_KEY,
  184. ComponentIndex: i,
  185. }
  186. table.PartitionKey[i] = column
  187. table.Columns[alias] = column
  188. }
  189. // determine the number of clustering columns
  190. size := len(comparatorParsed.types)
  191. if comparatorParsed.isComposite {
  192. if len(comparatorParsed.collections) != 0 ||
  193. (len(table.ColumnAliases) == size-1 &&
  194. comparatorParsed.types[size-1].Type == TypeVarchar) {
  195. size = size - 1
  196. }
  197. } else {
  198. if !(len(table.ColumnAliases) != 0 || len(table.Columns) == 0) {
  199. size = 0
  200. }
  201. }
  202. table.ClusteringColumns = make([]*ColumnMetadata, size)
  203. for i := range table.ClusteringColumns {
  204. var alias string
  205. if len(table.ColumnAliases) > i {
  206. alias = table.ColumnAliases[i]
  207. } else if i == 0 {
  208. alias = DEFAULT_COLUMN_ALIAS
  209. } else {
  210. alias = DEFAULT_COLUMN_ALIAS + strconv.Itoa(i+1)
  211. }
  212. order := ASC
  213. if comparatorParsed.reversed[i] {
  214. order = DESC
  215. }
  216. column := &ColumnMetadata{
  217. Keyspace: table.Keyspace,
  218. Table: table.Name,
  219. Name: alias,
  220. Type: comparatorParsed.types[i],
  221. Order: order,
  222. Kind: CLUSTERING_KEY,
  223. ComponentIndex: i,
  224. }
  225. table.ClusteringColumns[i] = column
  226. table.Columns[alias] = column
  227. }
  228. if size != len(comparatorParsed.types)-1 {
  229. alias := DEFAULT_VALUE_ALIAS
  230. if len(table.ValueAlias) > 0 {
  231. alias = table.ValueAlias
  232. }
  233. // decode the default validator
  234. defaultValidatorParsed := parseType(table.DefaultValidator)
  235. column := &ColumnMetadata{
  236. Keyspace: table.Keyspace,
  237. Table: table.Name,
  238. Name: alias,
  239. Type: defaultValidatorParsed.types[0],
  240. Kind: REGULAR,
  241. }
  242. table.Columns[alias] = column
  243. }
  244. }
  245. }
  246. // The simpler compile case for V2+ protocol
  247. func compileV2Metadata(tables []TableMetadata) {
  248. for i := range tables {
  249. table := &tables[i]
  250. partitionColumnCount := countColumnsOfKind(table.Columns, PARTITION_KEY)
  251. table.PartitionKey = make([]*ColumnMetadata, partitionColumnCount)
  252. clusteringColumnCount := countColumnsOfKind(table.Columns, CLUSTERING_KEY)
  253. table.ClusteringColumns = make([]*ColumnMetadata, clusteringColumnCount)
  254. for _, column := range table.Columns {
  255. if column.Kind == PARTITION_KEY {
  256. table.PartitionKey[column.ComponentIndex] = column
  257. } else if column.Kind == CLUSTERING_KEY {
  258. table.ClusteringColumns[column.ComponentIndex] = column
  259. }
  260. }
  261. }
  262. }
  263. func countColumnsOfKind(columns map[string]*ColumnMetadata, kind string) int {
  264. count := 0
  265. for _, column := range columns {
  266. if column.Kind == kind {
  267. count++
  268. }
  269. }
  270. return count
  271. }
  272. // query only for the keyspace metadata for the specified keyspace
  273. func getKeyspaceMetadata(
  274. session *Session,
  275. keyspaceName string,
  276. ) (*KeyspaceMetadata, error) {
  277. query := session.Query(
  278. `
  279. SELECT durable_writes, strategy_class, strategy_options
  280. FROM system.schema_keyspaces
  281. WHERE keyspace_name = ?
  282. `,
  283. keyspaceName,
  284. )
  285. keyspace := &KeyspaceMetadata{Name: keyspaceName}
  286. var strategyOptionsJSON []byte
  287. err := query.Scan(
  288. &keyspace.DurableWrites,
  289. &keyspace.StrategyClass,
  290. &strategyOptionsJSON,
  291. )
  292. if err != nil {
  293. return nil, fmt.Errorf("Error querying keyspace schema: %v", err)
  294. }
  295. err = json.Unmarshal(strategyOptionsJSON, &keyspace.StrategyOptions)
  296. if err != nil {
  297. return nil, fmt.Errorf(
  298. "Invalid JSON value '%s' as strategy_options for in keyspace '%s': %v",
  299. strategyOptionsJSON, keyspace.Name, err,
  300. )
  301. }
  302. return keyspace, nil
  303. }
  304. // query for only the table metadata in the specified keyspace
  305. func getTableMetadata(
  306. session *Session,
  307. keyspaceName string,
  308. ) ([]TableMetadata, error) {
  309. query := session.Query(
  310. `
  311. SELECT
  312. columnfamily_name,
  313. key_validator,
  314. comparator,
  315. default_validator,
  316. key_aliases,
  317. column_aliases,
  318. value_alias
  319. FROM system.schema_columnfamilies
  320. WHERE keyspace_name = ?
  321. `,
  322. keyspaceName,
  323. )
  324. iter := query.Iter()
  325. tables := []TableMetadata{}
  326. table := TableMetadata{Keyspace: keyspaceName}
  327. var keyAliasesJSON []byte
  328. var columnAliasesJSON []byte
  329. for iter.Scan(
  330. &table.Name,
  331. &table.KeyValidator,
  332. &table.Comparator,
  333. &table.DefaultValidator,
  334. &keyAliasesJSON,
  335. &columnAliasesJSON,
  336. &table.ValueAlias,
  337. ) {
  338. var err error
  339. // decode the key aliases
  340. if keyAliasesJSON != nil {
  341. table.KeyAliases = []string{}
  342. err = json.Unmarshal(keyAliasesJSON, &table.KeyAliases)
  343. if err != nil {
  344. iter.Close()
  345. return nil, fmt.Errorf(
  346. "Invalid JSON value '%s' as key_aliases for in table '%s': %v",
  347. keyAliasesJSON, table.Name, err,
  348. )
  349. }
  350. }
  351. // decode the column aliases
  352. if columnAliasesJSON != nil {
  353. table.ColumnAliases = []string{}
  354. err = json.Unmarshal(columnAliasesJSON, &table.ColumnAliases)
  355. if err != nil {
  356. iter.Close()
  357. return nil, fmt.Errorf(
  358. "Invalid JSON value '%s' as column_aliases for in table '%s': %v",
  359. columnAliasesJSON, table.Name, err,
  360. )
  361. }
  362. }
  363. tables = append(tables, table)
  364. table = TableMetadata{Keyspace: keyspaceName}
  365. }
  366. err := iter.Close()
  367. if err != nil && err != ErrNotFound {
  368. return nil, fmt.Errorf("Error querying table schema: %v", err)
  369. }
  370. return tables, nil
  371. }
  372. // query for only the table metadata in the specified keyspace
  373. func getColumnMetadata(
  374. session *Session,
  375. keyspaceName string,
  376. ) ([]ColumnMetadata, error) {
  377. // Deal with differences in protocol versions
  378. var stmt string
  379. var scan func(*Iter, *ColumnMetadata, *[]byte) bool
  380. if session.cfg.ProtoVersion == 1 {
  381. // V1 does not support the type column, and all returned rows are
  382. // of kind "regular".
  383. stmt = `
  384. SELECT
  385. columnfamily_name,
  386. column_name,
  387. component_index,
  388. validator,
  389. index_name,
  390. index_type,
  391. index_options
  392. FROM system.schema_columns
  393. WHERE keyspace_name = ?
  394. `
  395. scan = func(
  396. iter *Iter,
  397. column *ColumnMetadata,
  398. indexOptionsJSON *[]byte,
  399. ) bool {
  400. // all columns returned by V1 are regular
  401. column.Kind = REGULAR
  402. return iter.Scan(
  403. &column.Table,
  404. &column.Name,
  405. &column.ComponentIndex,
  406. &column.Validator,
  407. &column.Index.Name,
  408. &column.Index.Type,
  409. &indexOptionsJSON,
  410. )
  411. }
  412. } else {
  413. // V2+ supports the type column
  414. stmt = `
  415. SELECT
  416. columnfamily_name,
  417. column_name,
  418. component_index,
  419. validator,
  420. index_name,
  421. index_type,
  422. index_options,
  423. type
  424. FROM system.schema_columns
  425. WHERE keyspace_name = ?
  426. `
  427. scan = func(
  428. iter *Iter,
  429. column *ColumnMetadata,
  430. indexOptionsJSON *[]byte,
  431. ) bool {
  432. return iter.Scan(
  433. &column.Table,
  434. &column.Name,
  435. &column.ComponentIndex,
  436. &column.Validator,
  437. &column.Index.Name,
  438. &column.Index.Type,
  439. &indexOptionsJSON,
  440. &column.Kind,
  441. )
  442. }
  443. }
  444. // get the columns metadata
  445. columns := []ColumnMetadata{}
  446. column := ColumnMetadata{Keyspace: keyspaceName}
  447. var indexOptionsJSON []byte
  448. query := session.Query(stmt, keyspaceName)
  449. iter := query.Iter()
  450. for scan(iter, &column, &indexOptionsJSON) {
  451. var err error
  452. // decode the index options
  453. if indexOptionsJSON != nil {
  454. err = json.Unmarshal(indexOptionsJSON, &column.Index.Options)
  455. if err != nil {
  456. iter.Close()
  457. return nil, fmt.Errorf(
  458. "Invalid JSON value '%s' as index_options for column '%s' in table '%s': %v",
  459. indexOptionsJSON,
  460. column.Name,
  461. column.Table,
  462. err,
  463. )
  464. }
  465. }
  466. columns = append(columns, column)
  467. column = ColumnMetadata{Keyspace: keyspaceName}
  468. }
  469. err := iter.Close()
  470. if err != nil && err != ErrNotFound {
  471. return nil, fmt.Errorf("Error querying column schema: %v", err)
  472. }
  473. return columns, nil
  474. }
  475. // type definition parser state
  476. type typeParser struct {
  477. input string
  478. index int
  479. }
  480. // the type definition parser result
  481. type typeParserResult struct {
  482. isComposite bool
  483. types []TypeInfo
  484. reversed []bool
  485. collections map[string]TypeInfo
  486. }
  487. // Parse the type definition used for validator and comparator schema data
  488. func parseType(def string) typeParserResult {
  489. parser := &typeParser{input: def}
  490. return parser.parse()
  491. }
  492. const (
  493. REVERSED_TYPE = "org.apache.cassandra.db.marshal.ReversedType"
  494. COMPOSITE_TYPE = "org.apache.cassandra.db.marshal.CompositeType"
  495. COLLECTION_TYPE = "org.apache.cassandra.db.marshal.ColumnToCollectionType"
  496. LIST_TYPE = "org.apache.cassandra.db.marshal.ListType"
  497. SET_TYPE = "org.apache.cassandra.db.marshal.SetType"
  498. MAP_TYPE = "org.apache.cassandra.db.marshal.MapType"
  499. )
  500. // represents a class specification in the type def AST
  501. type typeParserClassNode struct {
  502. name string
  503. params []typeParserParamNode
  504. // this is the segment of the input string that defined this node
  505. input string
  506. }
  507. // represents a class parameter in the type def AST
  508. type typeParserParamNode struct {
  509. name *string
  510. class typeParserClassNode
  511. }
  512. func (t *typeParser) parse() typeParserResult {
  513. // parse the AST
  514. ast, ok := t.parseClassNode()
  515. if !ok {
  516. // treat this is a custom type
  517. return typeParserResult{
  518. isComposite: false,
  519. types: []TypeInfo{
  520. TypeInfo{
  521. Type: TypeCustom,
  522. Custom: t.input,
  523. },
  524. },
  525. reversed: []bool{false},
  526. collections: nil,
  527. }
  528. }
  529. // interpret the AST
  530. if strings.HasPrefix(ast.name, COMPOSITE_TYPE) {
  531. count := len(ast.params)
  532. // look for a collections param
  533. last := ast.params[count-1]
  534. collections := map[string]TypeInfo{}
  535. if strings.HasPrefix(last.class.name, COLLECTION_TYPE) {
  536. count--
  537. for _, param := range last.class.params {
  538. // decode the name
  539. var name string
  540. decoded, err := hex.DecodeString(*param.name)
  541. if err != nil {
  542. log.Printf(
  543. "Error parsing type '%s', contains collection name '%s' with an invalid format: %v",
  544. t.input,
  545. *param.name,
  546. err,
  547. )
  548. // just use the provided name
  549. name = *param.name
  550. } else {
  551. name = string(decoded)
  552. }
  553. collections[name] = param.class.asTypeInfo()
  554. }
  555. }
  556. types := make([]TypeInfo, count)
  557. reversed := make([]bool, count)
  558. for i, param := range ast.params[:count] {
  559. class := param.class
  560. reversed[i] = strings.HasPrefix(class.name, REVERSED_TYPE)
  561. if reversed[i] {
  562. class = class.params[0].class
  563. }
  564. types[i] = class.asTypeInfo()
  565. }
  566. return typeParserResult{
  567. isComposite: true,
  568. types: types,
  569. reversed: reversed,
  570. collections: collections,
  571. }
  572. } else {
  573. // not composite, so one type
  574. class := *ast
  575. reversed := strings.HasPrefix(class.name, REVERSED_TYPE)
  576. if reversed {
  577. class = class.params[0].class
  578. }
  579. typeInfo := class.asTypeInfo()
  580. return typeParserResult{
  581. isComposite: false,
  582. types: []TypeInfo{typeInfo},
  583. reversed: []bool{reversed},
  584. }
  585. }
  586. }
  587. func (class *typeParserClassNode) asTypeInfo() TypeInfo {
  588. if strings.HasPrefix(class.name, LIST_TYPE) {
  589. elem := class.params[0].class.asTypeInfo()
  590. return TypeInfo{
  591. Type: TypeList,
  592. Elem: &elem,
  593. }
  594. }
  595. if strings.HasPrefix(class.name, SET_TYPE) {
  596. elem := class.params[0].class.asTypeInfo()
  597. return TypeInfo{
  598. Type: TypeSet,
  599. Elem: &elem,
  600. }
  601. }
  602. if strings.HasPrefix(class.name, MAP_TYPE) {
  603. key := class.params[0].class.asTypeInfo()
  604. elem := class.params[1].class.asTypeInfo()
  605. return TypeInfo{
  606. Type: TypeMap,
  607. Key: &key,
  608. Elem: &elem,
  609. }
  610. }
  611. // must be a simple type or custom type
  612. info := TypeInfo{Type: getApacheCassandraType(class.name)}
  613. if info.Type == TypeCustom {
  614. // add the entire class definition
  615. info.Custom = class.input
  616. }
  617. return info
  618. }
  619. // CLASS := ID [ PARAMS ]
  620. func (t *typeParser) parseClassNode() (node *typeParserClassNode, ok bool) {
  621. t.skipWhitespace()
  622. startIndex := t.index
  623. name, ok := t.nextIdentifier()
  624. if !ok {
  625. return nil, false
  626. }
  627. params, ok := t.parseParamNodes()
  628. if !ok {
  629. return nil, false
  630. }
  631. endIndex := t.index
  632. node = &typeParserClassNode{
  633. name: name,
  634. params: params,
  635. input: t.input[startIndex:endIndex],
  636. }
  637. return node, true
  638. }
  639. // PARAMS := "(" PARAM { "," PARAM } ")"
  640. // PARAM := [ PARAM_NAME ":" ] CLASS
  641. // PARAM_NAME := ID
  642. func (t *typeParser) parseParamNodes() (params []typeParserParamNode, ok bool) {
  643. t.skipWhitespace()
  644. // the params are optional
  645. if t.index == len(t.input) || t.input[t.index] != '(' {
  646. return nil, true
  647. }
  648. params = []typeParserParamNode{}
  649. // consume the '('
  650. t.index++
  651. t.skipWhitespace()
  652. for t.input[t.index] != ')' {
  653. // look for a named param, but if no colon, then we want to backup
  654. backupIndex := t.index
  655. // name will be a hex encoded version of a utf-8 string
  656. name, ok := t.nextIdentifier()
  657. if !ok {
  658. return nil, false
  659. }
  660. hasName := true
  661. // TODO handle '=>' used for DynamicCompositeType
  662. t.skipWhitespace()
  663. if t.input[t.index] == ':' {
  664. // there is a name for this parameter
  665. // consume the ':'
  666. t.index++
  667. t.skipWhitespace()
  668. } else {
  669. // no name, backup
  670. hasName = false
  671. t.index = backupIndex
  672. }
  673. // parse the next full parameter
  674. classNode, ok := t.parseClassNode()
  675. if !ok {
  676. return nil, false
  677. }
  678. if hasName {
  679. params = append(
  680. params,
  681. typeParserParamNode{name: &name, class: *classNode},
  682. )
  683. } else {
  684. params = append(
  685. params,
  686. typeParserParamNode{class: *classNode},
  687. )
  688. }
  689. t.skipWhitespace()
  690. if t.input[t.index] == ',' {
  691. // consume the comma
  692. t.index++
  693. t.skipWhitespace()
  694. }
  695. }
  696. // consume the ')'
  697. t.index++
  698. return params, true
  699. }
  700. func (t *typeParser) skipWhitespace() {
  701. for t.index < len(t.input) && isWhitespaceChar(t.input[t.index]) {
  702. t.index++
  703. }
  704. }
  705. func isWhitespaceChar(c byte) bool {
  706. return c == ' ' || c == '\n' || c == '\t'
  707. }
  708. // ID := LETTER { LETTER }
  709. // LETTER := "0"..."9" | "a"..."z" | "A"..."Z" | "-" | "+" | "." | "_" | "&"
  710. func (t *typeParser) nextIdentifier() (id string, found bool) {
  711. startIndex := t.index
  712. for t.index < len(t.input) && isIdentifierChar(t.input[t.index]) {
  713. t.index++
  714. }
  715. if startIndex == t.index {
  716. return "", false
  717. }
  718. return t.input[startIndex:t.index], true
  719. }
  720. func isIdentifierChar(c byte) bool {
  721. return (c >= '0' && c <= '9') ||
  722. (c >= 'a' && c <= 'z') ||
  723. (c >= 'A' && c <= 'Z') ||
  724. c == '-' ||
  725. c == '+' ||
  726. c == '.' ||
  727. c == '_' ||
  728. c == '&'
  729. }