CLI and configuration library built especially for the parallelcoin pod
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validators.go 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. package tri
  2. import (
  3. "time"
  4. "reflect"
  5. "errors"
  6. "fmt"
  7. "unicode"
  8. )
  9. // Validate checks to ensure the contents of this node type satisfy constraints.
  10. // Brief only contains one thing, so we make sure it has it - one string. This string may not contain any type of control characters, and is limited to 80 characters in length.
  11. func (r *Brief) Validate() error {
  12. R := (*r)
  13. if len(R) != 1 {
  14. return errors.New("Brief field must have (only) one item")
  15. }
  16. s, ok := R[0].(string)
  17. if !ok {
  18. return errors.New("Brief's mandatory field is not a string")
  19. }
  20. if len(s) > 80 {
  21. return errors.New("Brief's text may not be over 80 characters in length")
  22. }
  23. for i, x := range s {
  24. if unicode.IsControl(x) {
  25. return fmt.Errorf("Brief text may not contain any control characters, one found at position %d", i)
  26. }
  27. }
  28. return nil
  29. }
  30. // Validate checks to ensure the contents of this node type satisfy constraints.
  31. // This validator only has to check the elements of the slice are zero or more Command items, and a valid name at index 0.
  32. func (r *Command) Validate() error {
  33. R := *r
  34. if len(R) < 1 {
  35. return errors.New("empty Command")
  36. }
  37. s, ok := R[0].(string)
  38. if !ok {
  39. return fmt.Errorf("first element of Command must be a string")
  40. }
  41. if e := ValidName(s); e != nil {
  42. return fmt.Errorf("error in name of Command: %v", e)
  43. }
  44. // validSet is an array of 4 elements that represent the presence of the 4 mandatory parts.
  45. var validSet [2]bool
  46. brief, handler := 0, 1
  47. var singleSet [4]bool
  48. usage, short, help, examples := 0, 1, 2, 3
  49. for i, x := range R[1:] {
  50. switch c := x.(type) {
  51. case Short:
  52. if singleSet[short] {
  53. return fmt.Errorf("only one Short field allowed in Command")
  54. }
  55. singleSet[short] = true
  56. e := c.Validate()
  57. if e != nil {
  58. return fmt.Errorf("error in Command at index %d: %v", i, e)
  59. }
  60. case Brief:
  61. if validSet[brief] {
  62. return fmt.Errorf("only one Brief permitted in a Command, second found at index %d", i)
  63. }
  64. validSet[brief] = true
  65. e := c.Validate()
  66. if e != nil {
  67. return fmt.Errorf("error in Command at index %d: %v", i, e)
  68. }
  69. case Usage:
  70. if singleSet[usage] {
  71. return fmt.Errorf("only one Usage field allowed in Command")
  72. }
  73. singleSet[usage] = true
  74. e := c.Validate()
  75. if e != nil {
  76. return fmt.Errorf("error in Command at index %d: %v", i, e)
  77. }
  78. case Help:
  79. if singleSet[help] {
  80. return fmt.Errorf("only one Help field allowed in Command")
  81. }
  82. singleSet[help] = true
  83. e := c.Validate()
  84. if e != nil {
  85. return fmt.Errorf("error in Command at index %d: %v", i, e)
  86. }
  87. case Examples:
  88. if singleSet[examples] {
  89. return fmt.Errorf("only one Examples field allowed in Command")
  90. }
  91. singleSet[examples] = true
  92. e := c.Validate()
  93. if e != nil {
  94. return fmt.Errorf("error in Command at index %d: %v", i, e)
  95. }
  96. case Var:
  97. e := c.Validate()
  98. if e != nil {
  99. return fmt.Errorf("error in Command at index %d: %v", i, e)
  100. }
  101. case Trigger:
  102. e := c.Validate()
  103. if e != nil {
  104. return fmt.Errorf("error in Command at index %d: %v", i, e)
  105. }
  106. case func(*Tri) int:
  107. if validSet[handler] {
  108. return fmt.Errorf("only one Handler permitted in a Command, second found at index %d", i)
  109. }
  110. validSet[handler] = true
  111. if c == nil {
  112. return fmt.Errorf("nil handler in Command found at index %d", i)
  113. }
  114. default:
  115. return fmt.Errorf("invalid type present in Command: %v", reflect.TypeOf(c))
  116. }
  117. }
  118. if !validSet[brief] {
  119. return errors.New("Brief field must be present")
  120. }
  121. if !validSet[handler] {
  122. return errors.New("Command must have a handler")
  123. }
  124. return nil
  125. }
  126. // Validate checks to ensure the contents of this node type satisfy constraints.
  127. // This validator only triggers the validator on its elements.
  128. func (r *Commands) Validate() error {
  129. R := (*r)
  130. for i, x := range R {
  131. e := x.Validate()
  132. if e != nil {
  133. return fmt.Errorf("error in element %d of Commands list: %v", i, e)
  134. }
  135. }
  136. return nil
  137. }
  138. // Validate checks to ensure the contents of this node type satisfy constraints.
  139. // The only constraint on the Default subtype is that it contains at only one element, the value is checked for correct typing by the Commands validator.
  140. func (r *Default) Validate() error {
  141. R := (*r)
  142. if len(R) != 1 {
  143. return errors.New("the Default container must only contain one element")
  144. }
  145. return nil
  146. }
  147. // Validate checks to ensure the contents of this node type satisfy constraints.
  148. // The constraint of DefaultCommand is that it has at least one element, and that the 0 element is a string. The check for the command name's presence in the Commands set is in the Tri validator.
  149. func (r *DefaultCommand) Validate() error {
  150. R := (*r)
  151. if len(R) != 1 {
  152. return errors.New(
  153. "the DefaultCommand element must contain only one element")
  154. }
  155. s, ok := R[0].(string)
  156. if !ok {
  157. return errors.New("element 0 of DefaultCommand must be a string")
  158. }
  159. if e := ValidName(s); e != nil {
  160. return fmt.Errorf("error in DefaultCommand: %v", e)
  161. }
  162. return nil
  163. }
  164. // Validate checks to ensure the contents of this node type satisfy constraints.
  165. // RunAfter is a simple flag that indicates by existence of an empty value, so it is an error if it has anything inside it.
  166. func (r *DefaultOn) Validate() error {
  167. R := *r
  168. if len(R) > 0 {
  169. return errors.New(
  170. "DefaultOn may not contain anything, empty declaration only")
  171. }
  172. return nil
  173. }
  174. // Validate checks to ensure the contents of this node type satisfy constraints.
  175. // The constraints of examples are minimum two elements and all elements are strings. The intent is the even numbered items are snippets showing invocation and a description string of the same format as Brief{}.
  176. func (r *Examples) Validate() error {
  177. R := *r
  178. if len(R) < 2 {
  179. return errors.New("Examples field may not be empty")
  180. }
  181. if len(R)%2 != 0 {
  182. return fmt.Errorf(
  183. "Examples must be in pairs, odd number of elements found")
  184. }
  185. for i, x := range R {
  186. _, ok := x.(string)
  187. if !ok {
  188. return fmt.Errorf(
  189. "Examples elements may only be strings, element %d is not a string", i)
  190. }
  191. }
  192. for i := 1; i <= len(R)-1; i += 2 {
  193. if len(R[i-1].(string)) > 40 {
  194. return errors.New(
  195. "Examples example text may not be over 4 characters in length")
  196. }
  197. if len(R[i].(string)) > 80 {
  198. return errors.New(
  199. "Examples explainer text may not be over 80 characters in length")
  200. }
  201. for i, x := range R[i].(string) {
  202. if unicode.IsControl(x) {
  203. return fmt.Errorf(
  204. "Examples even numbered field string may not contain control characters, one found at index %d", i)
  205. }
  206. }
  207. }
  208. return nil
  209. }
  210. // Validate checks to ensure the contents of this node type satisfy constraints.
  211. // A group must contain one string, anything else is invalid. It also has the same limitation as a name - only letters.
  212. func (r *Group) Validate() error {
  213. R := *r
  214. if len(R) != 1 {
  215. return errors.New("Group must (only) contain one element")
  216. }
  217. s, ok := R[0].(string)
  218. if !ok {
  219. return errors.New("Group element must be a string")
  220. }
  221. if e := ValidName(s); e != nil {
  222. return fmt.Errorf("error in name of Group: %v", e)
  223. }
  224. return nil
  225. }
  226. // Validate checks to ensure the contents of this node type satisfy constraints.
  227. // Help may only contain one string. It will be parsed as markdown format and possibly can be set to style it with ANSI codes.
  228. func (r *Help) Validate() error {
  229. R := *r
  230. if len(R) != 1 {
  231. return errors.New("Help field must contain (only) one item")
  232. }
  233. _, ok := R[0].(string)
  234. if !ok {
  235. return errors.New("Help field element is not a string")
  236. }
  237. return nil
  238. }
  239. // Validate checks to ensure the contents of this node type satisfy constraints.
  240. // RunAfter is a simple flag that indicates by existence of an empty value, so it is an error if it has anything inside it.
  241. func (r *RunAfter) Validate() error {
  242. R := *r
  243. if len(R) > 0 {
  244. return errors.New(
  245. "RunAfter may not contain anything, empty declaration only")
  246. }
  247. return nil
  248. }
  249. // Validate checks to ensure the contents of this node type satisfy constraints.
  250. // Short names contain only a single Rune variable.
  251. func (r *Short) Validate() error {
  252. R := *r
  253. if len(R) != 1 {
  254. return errors.New("Short name item must contain (only) one item")
  255. }
  256. s, ok := R[0].(rune)
  257. if !ok {
  258. return errors.New("Short's element must be a rune (enclose in '')")
  259. }
  260. if !(unicode.IsDigit(s) || unicode.IsLetter(s)) {
  261. return errors.New("Short element is not a letter or number")
  262. }
  263. return nil
  264. }
  265. // Validate checks to ensure the contents of this node type satisfy constraints.
  266. // Slot may only contain one type of element. The type check is in the Var, here we only ensure the slots contain pointers to the same type, the parser will put the final parsed value in all of them. Multiple variables are permitted here to enable the configuration of more than one application.
  267. func (r *Slot) Validate() error {
  268. R := *r
  269. var slotTypes []reflect.Type
  270. for _, x := range R {
  271. slotTypes = append(slotTypes, reflect.TypeOf(x))
  272. }
  273. for i, x := range slotTypes {
  274. if i > 0 {
  275. if slotTypes[i] != slotTypes[i-1] {
  276. return fmt.Errorf("slot contains more than one type of variable, found %v at index %d", x, i)
  277. }
  278. }
  279. }
  280. for _, x := range R {
  281. if reflect.ValueOf(x).Kind() != reflect.Ptr {
  282. return fmt.Errorf("slot contains non-pointer type")
  283. }
  284. }
  285. return nil
  286. }
  287. // Validate checks to ensure the contents of this node type satisfy constraints.
  288. // Terminates is a flag value, and may not contain anything.
  289. func (r *Terminates) Validate() error {
  290. R := *r
  291. if len(R) > 0 {
  292. return errors.New("Terminates type may not contain anything")
  293. }
  294. return nil
  295. }
  296. // Validate checks to ensure the contents of this node type satisfy constraints.
  297. // A Tri, the base type, in a declaration must contain a name as first element, a Brief, Version and a Commands item, and only one of each. Also, this and several other subtypes of Tri.
  298. func (r *Tri) Validate() error {
  299. R := *r
  300. if len(R) < 3 {
  301. return errors.New("a Tri must contain at least 3 elements: name, Brief and Version")
  302. }
  303. // validSet is an array of 4 elements that represent the presence of the 4 mandatory parts.
  304. var validSet [2]bool
  305. brief, version := 0, 1
  306. var singleSet [3]bool
  307. defcom, commands := 0, 1
  308. n, ok := R[0].(string)
  309. if !ok {
  310. return errors.New("first element of a Tri must be a string")
  311. }
  312. if e := ValidName(n); e != nil {
  313. return fmt.Errorf("error in name of Tri: %v", e)
  314. }
  315. // The mandatory elements also may not be repeated:
  316. for i, x := range R {
  317. if i == 0 {
  318. continue
  319. }
  320. switch y := x.(type) {
  321. case Brief:
  322. if validSet[brief] {
  323. return fmt.Errorf(
  324. "Tri contains more than one Brief, second found at index %d", i)
  325. }
  326. validSet[brief] = true
  327. if e := y.Validate(); e != nil {
  328. return fmt.Errorf("Tri field %d: %s", i, e)
  329. }
  330. case Version:
  331. if validSet[version] {
  332. return fmt.Errorf(
  333. "Tri contains more than one Version, second found at index %d", i)
  334. }
  335. validSet[version] = true
  336. if e := y.Validate(); e != nil {
  337. return fmt.Errorf("Tri field %d: %s", i, e)
  338. }
  339. validSet[version] = true
  340. case Commands:
  341. if singleSet[commands] {
  342. return fmt.Errorf(
  343. "Tri contains more than one Commands, second found at index %d", i)
  344. }
  345. singleSet[commands] = true
  346. e := y.Validate()
  347. if e != nil {
  348. return fmt.Errorf("error in Tri field %d: %s", i, e)
  349. }
  350. case Var:
  351. e := y.Validate()
  352. if e != nil {
  353. return fmt.Errorf("error in Tri at index %d: %v", i, e)
  354. }
  355. case Trigger:
  356. e := y.Validate()
  357. if e != nil {
  358. return fmt.Errorf("error in Tri at index %d: %v", i, e)
  359. }
  360. case DefaultCommand:
  361. if singleSet[defcom] {
  362. return fmt.Errorf(
  363. "Tri contains more than one DefaultCommand, second found at index %d", i)
  364. }
  365. singleSet[defcom] = true
  366. e := y.Validate()
  367. if e != nil {
  368. return fmt.Errorf("Tri field %d: %s", i, e)
  369. }
  370. commname := y[0].(string)
  371. // DefaultCommand must match in its name one of the Command items in also present Commands array
  372. foundComm := false
  373. foundDefComm := false
  374. for _, a := range R {
  375. switch c := a.(type) {
  376. case Commands:
  377. foundComm = true
  378. for _, b := range c {
  379. if b[0].(string) == commname {
  380. foundDefComm = true
  381. }
  382. }
  383. default:
  384. }
  385. }
  386. if !foundComm {
  387. return errors.New("DefaultCommand with no Commands array present")
  388. } else if !foundDefComm {
  389. return errors.New("DefaultCommand found with no matching Command")
  390. }
  391. default:
  392. return fmt.Errorf(
  393. "Tri contains an element type it may not contain at index %d", i)
  394. }
  395. }
  396. switch {
  397. case !validSet[brief]:
  398. return errors.New("Tri is missing its Brief field")
  399. case !validSet[version]:
  400. return errors.New("Tri is missing its Version field")
  401. }
  402. return nil
  403. }
  404. // Validate checks to ensure the contents of this node type satisfy constraints.
  405. // Trigger must contain (one) name, Brief and Handler, and nothing other than these and Short, Usage, Help, Default, Terminates, RunAfter.
  406. func (r *Trigger) Validate() error {
  407. R := *r
  408. if len(R) < 3 {
  409. return errors.New(
  410. "Trigger must contain a name, Brief and Handler at minimum")
  411. }
  412. name, ok := R[0].(string)
  413. if !ok {
  414. return errors.New("first element of Trigger must be the name")
  415. } else if e := ValidName(name); e != nil {
  416. return fmt.Errorf("Invalid Name in Trigger at index 0: %v", e)
  417. }
  418. // validSet is an array that represent the presence of the mandatory parts.
  419. var validSet [2]bool
  420. brief, handler := 0, 1
  421. var singleSet [7]bool
  422. short, usage, help, defon, terminates, runafter, group := 0, 1, 2, 3, 4, 5, 6
  423. for i, x := range R[1:] {
  424. switch y := x.(type) {
  425. case Brief:
  426. if validSet[brief] {
  427. return fmt.Errorf("Trigger may (only) contain one Brief, second found at index %d", i)
  428. } else {
  429. validSet[brief] = true
  430. }
  431. if e := y.Validate(); e != nil {
  432. return fmt.Errorf(
  433. "Trigger contains invalid element at %d - %s", i, e)
  434. }
  435. case func(*Tri) int:
  436. if validSet[handler] {
  437. return fmt.Errorf(
  438. "Trigger may (only) contain one Handler, second found at index %d", i)
  439. } else {
  440. validSet[handler] = true
  441. }
  442. if y == nil {
  443. return fmt.Errorf("Handler at index %d may not be nil", i)
  444. }
  445. case Short:
  446. if singleSet[short] {
  447. return fmt.Errorf("Trigger may only contain one Short, extra found at index %d", i)
  448. }
  449. singleSet[short] = true
  450. if e := y.Validate(); e != nil {
  451. return fmt.Errorf(
  452. "Trigger contains invalid element at %d - %s", i, e)
  453. }
  454. case Usage:
  455. if singleSet[usage] {
  456. return fmt.Errorf("Trigger may only contain one Usage, extra found at index %d", i)
  457. }
  458. singleSet[usage] = true
  459. if e := y.Validate(); e != nil {
  460. return fmt.Errorf(
  461. "Trigger contains invalid element at %d - %s", i, e)
  462. }
  463. case Help:
  464. if singleSet[help] {
  465. return fmt.Errorf("Trigger may only contain one Help, extra found at index %d", i)
  466. }
  467. singleSet[help] = true
  468. if e := y.Validate(); e != nil {
  469. return fmt.Errorf(
  470. "Trigger contains invalid element at %d - %s", i, e)
  471. }
  472. case DefaultOn:
  473. if singleSet[defon] {
  474. return fmt.Errorf("Trigger may only contain one DefaultOn, extra found at index %d", i)
  475. }
  476. singleSet[defon] = true
  477. if e := y.Validate(); e != nil {
  478. return fmt.Errorf(
  479. "Trigger contains invalid element at %d - %s", i, e)
  480. }
  481. case Terminates:
  482. if singleSet[terminates] {
  483. return fmt.Errorf("Trigger may only contain one Terminates, extra found at index %d", i)
  484. }
  485. singleSet[terminates] = true
  486. if e := y.Validate(); e != nil {
  487. return fmt.Errorf(
  488. "Trigger contains invalid element at %d - %s", i, e)
  489. }
  490. case RunAfter:
  491. if singleSet[runafter] {
  492. return fmt.Errorf("Trigger may only contain one RunAfter, extra found at index %d", i)
  493. }
  494. singleSet[runafter] = true
  495. if e := y.Validate(); e != nil {
  496. return fmt.Errorf(
  497. "Trigger contains invalid element at %d - %s", i, e)
  498. }
  499. case Group:
  500. if singleSet[group] {
  501. return fmt.Errorf(
  502. "Trigger may only contain one Group, extra found at index %d", i)
  503. }
  504. singleSet[group] = true
  505. if e := y.Validate(); e != nil {
  506. return fmt.Errorf(
  507. "Trigger contains invalid element at %d - %s", i, e)
  508. }
  509. default:
  510. return fmt.Errorf(
  511. "found invalid item type at element %d in a Trigger", i)
  512. }
  513. }
  514. if !(validSet[brief] && validSet[handler]) {
  515. return errors.New("Trigger must contain one each of Brief and Handler")
  516. }
  517. return nil
  518. }
  519. // Validate checks to ensure the contents of this node type satisfy constraints.
  520. // Usage fields contain only one string of no more than 80 characters and no control characters.
  521. func (r *Usage) Validate() error {
  522. R := *r
  523. if len(R) != 1 {
  524. return errors.New("Usage field must contain (only) one element")
  525. }
  526. s, ok := R[0].(string)
  527. if !ok {
  528. return errors.New("Usage field element is not a string")
  529. }
  530. if ll := len(s); ll > 80 {
  531. return fmt.Errorf("Usage string is %d chars long, may not be longer than 80", ll)
  532. }
  533. for i, x := range s {
  534. if unicode.IsControl(x) {
  535. return fmt.Errorf(
  536. "Usage field string may not contain control characters, one found at index %d", i)
  537. }
  538. }
  539. return nil
  540. }
  541. // Validate checks to ensure the contents of this node type satisfy constraints.
  542. // Var must contain name, Brief and Slot, and optionally, Short, Usage, Help and Default. The type in the Slot and the Default must be the same.
  543. func (r *Var) Validate() error {
  544. R := *r
  545. if len(R) < 3 {
  546. return errors.New(
  547. "Var must contain a name, Brief and Slot at minimum")
  548. }
  549. name, ok := R[0].(string)
  550. if !ok {
  551. return errors.New("first element of Var must be the name")
  552. } else if e := ValidName(name); e != nil {
  553. return fmt.Errorf("Invalid Name in Var at index 0: %v", e)
  554. }
  555. // validSet is an array that represent the presence of the mandatory parts.
  556. var validSet [2]bool
  557. brief, slot := 0, 1
  558. // singleSet is an array representing the optional elements that may not be more than one inside a Var
  559. var singleSet [5]bool
  560. short, usage, help, def, group := 0, 1, 2, 3, 4
  561. for i, x := range R[1:] {
  562. switch y := x.(type) {
  563. case Brief:
  564. if validSet[brief] {
  565. return fmt.Errorf("Var may must (only) contain one Brief, second found at index %d", i)
  566. } else {
  567. validSet[brief] = true
  568. }
  569. if e := y.Validate(); e != nil {
  570. return fmt.Errorf(
  571. "Var contains invalid element at %d - %s", i, e)
  572. }
  573. case Short:
  574. if singleSet[short] {
  575. return fmt.Errorf("Var may only contain one Short, extra found at index %d", i)
  576. }
  577. singleSet[short] = true
  578. if e := y.Validate(); e != nil {
  579. return fmt.Errorf(
  580. "Var contains invalid element at %d - %s", i, e)
  581. }
  582. case Usage:
  583. if singleSet[usage] {
  584. return fmt.Errorf("Var may only contain one Usage, extra found at index %d", i)
  585. }
  586. singleSet[usage] = true
  587. if e := y.Validate(); e != nil {
  588. return fmt.Errorf(
  589. "Var contains invalid element at %d - %s", i, e)
  590. }
  591. case Help:
  592. if singleSet[help] {
  593. return fmt.Errorf("Var may only contain one Help, extra found at index %d", i)
  594. }
  595. singleSet[help] = true
  596. if e := y.Validate(); e != nil {
  597. return fmt.Errorf(
  598. "Var contains invalid element at %d - %s", i, e)
  599. }
  600. case Default:
  601. if singleSet[def] {
  602. return fmt.Errorf("Var may only contain one Default, extra found at index %d", i)
  603. }
  604. singleSet[def] = true
  605. if e := y.Validate(); e != nil {
  606. return fmt.Errorf(
  607. "Var contains invalid element at %d - %s", i, e)
  608. }
  609. for _, z := range R {
  610. s, ok := z.(Slot)
  611. if ok {
  612. switch s[0].(type) {
  613. case *string:
  614. _, ok := y[0].(string)
  615. if !ok {
  616. return errors.New("slot is not same type as default")
  617. }
  618. case *int:
  619. _, ok := y[0].(int)
  620. if !ok {
  621. return errors.New("slot is not same type as default")
  622. }
  623. case *uint32:
  624. _, ok := y[0].(uint32)
  625. if !ok {
  626. return errors.New("slot is not same type as default")
  627. }
  628. case *float64:
  629. _, ok := y[0].(float64)
  630. if !ok {
  631. return errors.New("slot is not same type as default")
  632. }
  633. case *[]string:
  634. _, ok := y[0].([]string)
  635. if !ok {
  636. return errors.New("slot is not same type as default")
  637. }
  638. case *time.Duration:
  639. _, ok := y[0].(time.Duration)
  640. if !ok {
  641. return errors.New("slot is not same type as default")
  642. }
  643. }
  644. // *s[0] = *y[0]
  645. }
  646. }
  647. case Slot:
  648. if validSet[slot] {
  649. return fmt.Errorf("Var may only contain one Slot, extra found at index %d", i)
  650. }
  651. validSet[slot] = true
  652. if e := y.Validate(); e != nil {
  653. return fmt.Errorf(
  654. "Var contains invalid element at %d - %s", i, e)
  655. }
  656. case Group:
  657. if singleSet[group] {
  658. return fmt.Errorf(
  659. "Var may only contain one Group, extra found at index %d", i)
  660. }
  661. singleSet[group] = true
  662. if e := y.Validate(); e != nil {
  663. return fmt.Errorf(
  664. "Var contains invalid element at %d - %s", i, e)
  665. }
  666. default:
  667. return fmt.Errorf(
  668. "found invalid item type at element %d in a Var", i)
  669. }
  670. }
  671. if !(validSet[brief] && validSet[slot]) {
  672. return errors.New("Var must contain one each of Brief and Slot")
  673. }
  674. // TODO: check that Default value can be assigned to dereferenced Slot variable
  675. return nil
  676. }
  677. // Validate checks to ensure the contents of this node type satisfy constraints.
  678. // A version item contains three integers and an optional (less than 16 character) string, and the numbers may not be more than 99.
  679. func (r *Version) Validate() error {
  680. R := *r
  681. if len(R) > 4 {
  682. return errors.New("Version field may not contain more than 4 fields")
  683. }
  684. if len(R) < 3 {
  685. return errors.New("Version field must contain at least 3 fields")
  686. }
  687. for i, x := range R[:3] {
  688. n, ok := x.(int)
  689. if !ok {
  690. return fmt.Errorf("Version field %d is not an integer: %d", i, n)
  691. }
  692. if n > 99 {
  693. return fmt.Errorf("Version field %d value is over 99: %d", i, n)
  694. }
  695. }
  696. if len(R) > 3 {
  697. s, ok := R[3].(string)
  698. if !ok {
  699. return fmt.Errorf("optional field 4 of Version is not a string")
  700. } else {
  701. for i, x := range s {
  702. if !(unicode.IsLetter(x) || unicode.IsDigit(x)) {
  703. return fmt.Errorf(
  704. "optional field 4 of Version contains other than letters and numbers at position %d: '%v,", i, x)
  705. }
  706. }
  707. }
  708. }
  709. return nil
  710. }
  711. // ValidName checks that a Tri name element that should be a name only contains letters.
  712. func ValidName(s string) error {
  713. if len(s) < 3 {
  714. return errors.New("name is less than 3 characters long")
  715. }
  716. for i, x := range s {
  717. if !unicode.IsLetter(x) {
  718. return fmt.Errorf(
  719. "element %d, '%v' of name is not a letter", i, x)
  720. }
  721. }
  722. return nil
  723. }