bean.sbn 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. {{~if ___top_module != ''~}}
  2. package {{__top_module}};
  3. {{~end~}}
  4. {{~
  5. go_full_name = full_name __this
  6. parent_def_type = __this.parent_def_type
  7. is_abstract_type = __this.is_abstract_type
  8. hierarchy_fields = __this.hierarchy_export_fields
  9. hierarchy_not_abstract_children = __this.hierarchy_not_abstract_children
  10. ~}}
  11. import (
  12. "{{__luban_module_name}}"
  13. )
  14. {{collect_import __this}}
  15. type {{go_full_name}} struct {
  16. {{~for field in hierarchy_fields ~}}
  17. {{format_field_name __code_style field.name}} {{declaring_type_name field.ctype}}
  18. {{~end~}}
  19. }
  20. const TypeId_{{go_full_name}} = {{__this.id}}
  21. func (*{{go_full_name}}) GetTypeId() int32 {
  22. return {{__this.id}}
  23. }
  24. {{~if is_abstract_type~}}
  25. func New{{go_full_name}}(_buf *luban.ByteBuf) (interface{}, error) {
  26. var id int32
  27. var err error
  28. if id, err = _buf.ReadInt() ; err != nil {
  29. return nil, err
  30. }
  31. switch id {
  32. {{~for child in hierarchy_not_abstract_children~}}
  33. case {{child.id}}: _v, err := New{{full_name child}}(_buf); if err != nil { return nil, errors.New("{{child.full_name|string.downcase}}") } else { return _v, nil }
  34. {{~end~}}
  35. default: return nil, errors.New("unknown type id")
  36. }
  37. }
  38. {{~else~}}
  39. func New{{go_full_name}}(_buf *luban.ByteBuf) (_v *{{go_full_name}}, err error) {
  40. _v = &{{go_full_name}}{}
  41. {{~for field in hierarchy_fields ~}}
  42. {{deserialize_field field.ctype ("_v." + (format_field_name __code_style field.name)) '_buf' 'err'}}
  43. {{~end~}}
  44. return
  45. }
  46. {{~end~}}