simple_riders.go 1015 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package stub
  2. import (
  3. "odf/model"
  4. "odf/xmlns"
  5. "reflect"
  6. "ypk/assert"
  7. "ypk/halt"
  8. )
  9. type sw struct {
  10. base *sm
  11. pos model.Leaf
  12. }
  13. func (w *sw) Base() model.Model {
  14. return w.base
  15. }
  16. func (w *sw) InitFrom(old model.Writer) {
  17. panic(126)
  18. }
  19. func (w *sw) Pos(p ...model.Leaf) model.Leaf {
  20. if len(p) == 1 {
  21. w.pos = p[0]
  22. }
  23. return w.pos
  24. }
  25. func (w *sw) Write(l model.Leaf) {
  26. assert.For(l != nil, 20)
  27. assert.For(w.pos != nil, 21)
  28. if _n, ok := w.pos.(model.Node); ok {
  29. if n, da := _n.(*sn); da {
  30. n.children = append(n.children, l)
  31. } else {
  32. halt.As(100, reflect.TypeOf(n))
  33. }
  34. }
  35. }
  36. func (w *sw) WritePos(l model.Leaf) model.Leaf {
  37. w.Write(l)
  38. return w.Pos(l)
  39. }
  40. func castAttr(n model.AttrName, i interface{}) (ret model.Attribute) {
  41. typ := xmlns.Typed[n]
  42. switch typ {
  43. case xmlns.NONE, xmlns.STRING:
  44. ret = &StringAttr{Value: i.(string)}
  45. default:
  46. halt.As(100, typ, reflect.TypeOf(i))
  47. }
  48. return ret
  49. }
  50. func (w *sw) Attr(n model.AttrName, val interface{}) {
  51. w.pos.Attr(n, castAttr(n, val))
  52. }