| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- // package elem_base -- базовый объект
- package elem_base
- import (
- "encoding/json"
- "fmt"
- "gitp78su.ipnodns.ru/svi/goarch/lev0/alias"
- "gitp78su.ipnodns.ru/svi/goarch/pkg/elems/label"
- )
- // ElemBase -- базовый объект
- type ElemBase struct {
- *label.Label `json:"label"` // название объекта
- Id_ alias.Id `json:"id"` // Идентификатор объекта
- Type_ alias.Type `json:"type"` // Тип объекта
- Elem_ map[string]interface{} // Набор полей
- StrElem_ string // Строковое представление объекта
- }
- // NewElemBase -- возвращает новый базовый объект
- func NewElemBase(elem map[string]interface{}) (*ElemBase, error) {
- binElem, _ := json.MarshalIndent(elem, "", " ")
- _id0, isOk := elem["id"]
- if !isOk {
- return nil, fmt.Errorf("NewElemBase(): field 'id' not found<br>%+v", string(binElem))
- }
- id, isOk := _id0.(string)
- if !isOk {
- return nil, fmt.Errorf("NewElemBase(): field 'id'(%+v) not string<br>%+v", _id0, string(binElem))
- }
- if id == "" {
- return nil, fmt.Errorf("NewElemBase(): id is empty<br>%+v", string(binElem))
- }
- _type, isOk := elem["type"]
- if !isOk {
- return nil, fmt.Errorf("NewElemBase(): id=%q, field 'type' not found<br>%+v", id, string(binElem))
- }
- strType, isOk := _type.(string)
- if !isOk {
- return nil, fmt.Errorf("NewElemBase(): id=%q, field 'type'(%+v) not string<br>%+v", id, _type, string(binElem))
- }
- sf := &ElemBase{
- Elem_: elem,
- StrElem_: string(binElem),
- Label: label.NewLabel(elem),
- Id_: alias.Id(id),
- Type_: alias.Type(strType),
- }
- return sf, nil
- }
- // Elem -- возвращает элементы словаря
- func (sf *ElemBase) Elem() map[string]interface{} {
- return sf.Elem_
- }
- // Type -- возвращает тип объекта
- func (sf *ElemBase) Type() alias.Type {
- return sf.Type_
- }
- // Id -- возвращает ID объекта
- func (sf *ElemBase) Id() alias.Id {
- return sf.Id_
- }
|