elem_base.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // package elem_base -- базовый объект
  2. package elem_base
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "gitp78su.ipnodns.ru/svi/goarch/lev0/alias"
  7. "gitp78su.ipnodns.ru/svi/goarch/pkg/elems/label"
  8. )
  9. // ElemBase -- базовый объект
  10. type ElemBase struct {
  11. *label.Label `json:"label"` // название объекта
  12. Id_ alias.Id `json:"id"` // Идентификатор объекта
  13. Type_ alias.Type `json:"type"` // Тип объекта
  14. Elem_ map[string]interface{} // Набор полей
  15. StrElem_ string // Строковое представление объекта
  16. }
  17. // NewElemBase -- возвращает новый базовый объект
  18. func NewElemBase(elem map[string]interface{}) (*ElemBase, error) {
  19. binElem, _ := json.MarshalIndent(elem, "", " ")
  20. _id0, isOk := elem["id"]
  21. if !isOk {
  22. return nil, fmt.Errorf("NewElemBase(): field 'id' not found<br>%+v", string(binElem))
  23. }
  24. id, isOk := _id0.(string)
  25. if !isOk {
  26. return nil, fmt.Errorf("NewElemBase(): field 'id'(%+v) not string<br>%+v", _id0, string(binElem))
  27. }
  28. if id == "" {
  29. return nil, fmt.Errorf("NewElemBase(): id is empty<br>%+v", string(binElem))
  30. }
  31. _type, isOk := elem["type"]
  32. if !isOk {
  33. return nil, fmt.Errorf("NewElemBase(): id=%q, field 'type' not found<br>%+v", id, string(binElem))
  34. }
  35. strType, isOk := _type.(string)
  36. if !isOk {
  37. return nil, fmt.Errorf("NewElemBase(): id=%q, field 'type'(%+v) not string<br>%+v", id, _type, string(binElem))
  38. }
  39. sf := &ElemBase{
  40. Elem_: elem,
  41. StrElem_: string(binElem),
  42. Label: label.NewLabel(elem),
  43. Id_: alias.Id(id),
  44. Type_: alias.Type(strType),
  45. }
  46. return sf, nil
  47. }
  48. // Elem -- возвращает элементы словаря
  49. func (sf *ElemBase) Elem() map[string]interface{} {
  50. return sf.Elem_
  51. }
  52. // Type -- возвращает тип объекта
  53. func (sf *ElemBase) Type() alias.Type {
  54. return sf.Type_
  55. }
  56. // Id -- возвращает ID объекта
  57. func (sf *ElemBase) Id() alias.Id {
  58. return sf.Id_
  59. }