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.

80 line
1.6KB

  1. package template
  2. import (
  3. "git.wtrh.nl/patterns/gopatterns/pkg/dimensions"
  4. "git.wtrh.nl/patterns/gopatterns/pkg/path"
  5. "git.wtrh.nl/patterns/gopatterns/pkg/pattern/panel"
  6. "git.wtrh.nl/patterns/gopatterns/pkg/point"
  7. "git.wtrh.nl/patterns/gopatterns/pkg/util"
  8. )
  9. type Template struct {
  10. Name string `yaml:"name"`
  11. Points Points `yaml:"points"`
  12. Lines Lines `yaml:"lines"`
  13. Panels Panels `yaml:"panels"`
  14. Version string `yaml:"version"`
  15. }
  16. type Request struct {
  17. Dims dimensions.Dimensions
  18. Panel string
  19. }
  20. type request struct {
  21. dimensions dimensions.Dimensions
  22. name string
  23. lines map[util.ID]path.Path
  24. points map[util.ID]point.Point
  25. }
  26. func (t Template) GetPanel(req Request) (panel.Panel, error) {
  27. p, ok := t.Panels[req.Panel]
  28. if !ok {
  29. return panel.Panel{}, ErrPanelNotFound
  30. }
  31. r := request{
  32. dimensions: req.Dims,
  33. name: req.Panel,
  34. lines: make(map[util.ID]path.Path),
  35. points: make(map[util.ID]point.Point),
  36. }
  37. result := panel.Panel{
  38. Name: req.Panel,
  39. Lines: make(map[util.ID]path.Path),
  40. Points: make(map[util.ID]point.Point),
  41. Dimensions: req.Dims,
  42. }
  43. for id := range p.Lines {
  44. line, err := t.getOrCreateLine(id, r, 0)
  45. if err != nil {
  46. return panel.Panel{}, err
  47. }
  48. result.Lines[id] = line
  49. }
  50. for id := range p.Points {
  51. newPoint, err := t.getOrCreatePoint(id, r, 0)
  52. if err != nil {
  53. return panel.Panel{}, err
  54. }
  55. result.Points[id] = newPoint
  56. }
  57. for id := range t.Points {
  58. newPoint, err := t.getOrCreatePoint(id, r, 0)
  59. if err != nil {
  60. return panel.Panel{}, err
  61. }
  62. result.Points[id] = newPoint
  63. }
  64. return result, nil
  65. }