rpc improvements, fix reading table_json_kv structs

This commit is contained in:
Pavel Merzlyakov 2023-06-07 12:28:27 +03:00
parent c1128ec7c4
commit d9ca9a8c75
3 changed files with 134 additions and 20 deletions

View File

@ -4,15 +4,26 @@ package {{ package }}
import (
"context"
"database/sql"
"database/sql/driver"
"encoding/json"
"net"
"sort"
"strconv"
"strings"
"git.bit5.ru/backend/meta"
"git.bit5.ru/backend/versioning"
"git.bit5.ru/gomodules/metadb"
"github.com/doug-martin/goqu/v9"
"github.com/doug-martin/goqu/v9/exp"
"github.com/pkg/errors"
)
var _ = driver.IsScanValue
var _ = json.Marshal
var _ = strconv.Atoi
var _ = strings.Clone
{% import "macros_enum.twig" as enum_macros %}
{% import "macros_struct.twig" as struct_macros %}
{% import "macros_rpc.twig" as rpc_macros %}
@ -60,7 +71,7 @@ func CreateRPC(code uint32) (Rpc, error) {
{%~ for u in rpcs %}
{%- set name = u.object.name|lower|camel|replace({'Rpc': ''}) ~ 'Rpc' %}
case {{ u.object.code }}:
return &{{ name }}{}, nil
return New{{ name }}(), nil
{%~ endfor %}
default:
return nil, errors.Errorf("Can't find rpc for code %d", code)
@ -70,7 +81,7 @@ func CreateRPC(code uint32) (Rpc, error) {
type RpcHandler interface {
{%~ for u in rpcs %}
{%- set name = u.object.name|lower|camel|replace({'Rpc': ''}) %}
{{ name }}({{ name ~ 'Rpc' }}) error
{{ name }}(RpcContext, {{ name ~ 'Rpc' }}) error
{%~ endfor %}
}
@ -81,10 +92,77 @@ type Rpc interface {
Error() (int32, string)
SetError(int32, string)
Execute(RpcHandler) error
Request() meta.Readable
Execute(RpcContext, RpcHandler) error
ReadRequest(meta.Reader) error
WriteResponse(meta.Writer) error
}
type CreateRpcFunc func(uint32) (Rpc, error)
type RpcContext struct {
ctx context.Context
ip net.IP
version versioning.Version
platform uint8
extras map[string]string
playerId uint32
extId string
}
func NewRpcContext(
ctx context.Context,
ip net.IP,
version versioning.Version,
platform uint8,
extras map[string]string,
) RpcContext {
return RpcContext{
ctx: ctx,
ip: ip,
version: version,
platform: platform,
extras: extras,
}
}
func (rctx RpcContext) Context() context.Context {
return rctx.ctx
}
func (rctx RpcContext) IP() net.IP {
return rctx.ip
}
func (rctx RpcContext) Version() versioning.Version {
return rctx.version
}
func (rctx RpcContext) Platform() uint8 {
return rctx.platform
}
func (rctx RpcContext) ExtraValue(key string) (string, bool) {
if rctx.extras == nil {
return "", false
}
v, ok := rctx.extras[key]
return v, ok
}
func (rctx *RpcContext) SetPlayer(playerId uint32, extId string) {
rctx.playerId = playerId
rctx.extId = extId
}
func (rctx RpcContext) PlayerId() uint32 {
return rctx.playerId
}
func (rctx RpcContext) ExtId() string {
return rctx.extId
}

View File

@ -7,13 +7,20 @@
{% set rsp_name = prefix ~ 'Response' %}
type {{ name }} struct {
req {{ req_name }}
rsp {{ rsp_name }}
Req *{{ req_name }}
Rsp *{{ rsp_name }}
errCode int32
errMsg string
}
func New{{ name }}() *{{ name }} {
rpc := {{ name }}{}
rpc.Req = New{{ req_name }}()
rpc.Rsp = New{{ rsp_name }}()
return &rpc
}
func (rpc {{ name }}) Name() string {
return "{{ name }}"
}
@ -31,24 +38,20 @@ func (rpc {{ name }}) Error() (int32, string) {
return rpc.errCode, rpc.errMsg
}
func (rpc {{ name }}) Execute(h RpcHandler) error {
return h.{{ prefix }}(rpc)
func (rpc {{ name }}) Execute(ctx RpcContext, h RpcHandler) error {
return h.{{ prefix }}(ctx, rpc)
}
func (rpc {{ name }}) Request() {{ req_name }} {
return rpc.req
}
func (rpc *{{ name }}) SetResponse(rsp {{ rsp_name }}) {
rpc.rsp = rsp
func (rpc {{ name }}) Request() meta.Readable {
return rpc.Req
}
func (rpc *{{ name }}) ReadRequest(reader meta.Reader) error {
return rpc.req.Read(reader)
return rpc.Req.Read(reader)
}
func (rpc *{{ name }}) WriteResponse(writer meta.Writer) error {
return rpc.rsp.Write(writer)
return rpc.Rsp.Write(writer)
}
type {{ req_name }} struct {
@ -57,7 +60,14 @@ type {{ req_name }} struct {
{%~ endfor %}
}
func New{{ req_name }}() *{{ req_name }} {
req := new({{ req_name }})
req.Reset()
return req
}
{{ struct_macros.struct_required_fields(req_name, r.req.fields)}}
{{ struct_macros.reset_method(r.req, req_name) }}
{{ struct_macros.meta_read(r.req, req_name) }}
type {{ rsp_name }} struct {
@ -66,5 +76,12 @@ type {{ rsp_name }} struct {
{%~ endfor %}
}
func New{{ rsp_name }}() *{{ rsp_name }} {
rsp := new({{ rsp_name }})
rsp.Reset()
return rsp
}
{{ struct_macros.reset_method(r.rsp, rsp_name) }}
{{ struct_macros.meta_write(r.rsp, rsp_name) }}
{% endmacro rpc %}

View File

@ -51,16 +51,25 @@ type {{ json_fields_type }} struct{
{%~ endfor %}
changedFields meta.ChangedFields
{% if has_token(s, 'bitfields') %}
fieldsMask meta.FieldsMask
{% endif %}
}
{{ _self.json_sql_impl(json_fields_type) }}
{{ _self.struct_methods(s.name, s.classid) }}
{{ _self.struct_required_fields(s.name, s.fields) }}
{{ _self.reset_method(s) }}
{{ _self.meta_read(s) }}
{{ _self.meta_write(s) }}
{# {{ _self.struct_identifier(s.name, pkey) }} #}
{% if has_token(s, 'bitfields') %}
{{ _self.bitfields_methods(s.name) }}
{% endif %}
{{ _self.table_save(_context) }}
{{ _self.table_load(_context) }}
{{ _self.table_delete(_context) }}
@ -547,11 +556,7 @@ func (v {{ type_name }}) WriteJsonSetExpr(b *strings.Builder) error {
{% endmacro json_set_expr %}
{% macro meta_read(o, name) %}
{% set fields_len = o.fields|length %}
{% set opt_fields_len = count_optional(o.fields) %}
{% set all_fields = get_all_fields(o) %}
{% macro reset_method(o, name) %}
func (s *{{ name|default(o.name) }}) Reset() {
{% if o.parent %}
s.{{o.parent.name}}.Reset()
@ -580,6 +585,13 @@ func (s *{{ name|default(o.name) }}) Reset() {
s.changedFields.Reset()
{% endif %}
}
{% endmacro reset_method %}
{% macro meta_read(o, name) %}
{% set fields_len = o.fields|length %}
{% set opt_fields_len = count_optional(o.fields) %}
{% set all_fields = get_all_fields(o) %}
func (s *{{ name|default(o.name) }}) Read(reader meta.Reader) error {
if err := reader.BeginContainer(""); err != nil {
@ -677,10 +689,16 @@ func (s *{{ name|default(o.name) }}) readFields(reader meta.Reader) error {
}
if !use_mask || (use_mask && s.FieldChanged({{ loop.index0 }})) {
{{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, has_token(f, 'optional')) }}
{% if has_token(o, 'table') %}
s.changedFields.SetChanged("{{ f|alias }}")
{% endif %}
}
{% else %}
contSize--
{{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, has_token(f, 'optional')) }}
{% if has_token(o, 'table') %}
s.changedFields.SetChanged("{{ f|alias }}")
{% endif %}
{% endif %}
{%- endfor -%}
@ -845,6 +863,7 @@ type {{ s.name }} struct {
{{ _self.struct_methods(s.name, s.classid) }}
{{ _self.struct_required_fields(s.name, s.fields) }}
{{ _self.reset_method(s) }}
{{ _self.meta_read(s) }}
{{ _self.meta_write(s) }}