def response_payload(gen: Generator, peer): for meb in peer["requests"]: if len(peer["requests"][meb]["response"]) > 0: struct(gen, camelcase(peer["type"]) + camelcase(meb) + "Response", peer["requests"][meb]["response"]) gen.emit('')
def message_payloads(gen: Generator, peer): requests_and_signals = dict(**peer["requests"], **peer["signals"]) for meb in requests_and_signals: if len(requests_and_signals[meb]["arguments"]) > 0: struct(gen, camelcase(peer["type"]) + camelcase(meb) + "Message", requests_and_signals[meb]["arguments"]) gen.emit('')
def _get_plugin_class(plugin_name): """dynamically loads a plugin class Example: For a plugin named volume_manager a module named twister.plugins.volume_manager should exist and define a class named VolumeManager.""" plugin_module = importlib.import_module('plugins.%s' % plugin_name) plugin_class = getattr(plugin_module, camelcase(plugin_name)) return plugin_class
def unzip_return_value(go_cls): cls_type = go_cls.type if cls_type == 'simple': return '"{}"'.format(go_cls.get_value()) elif cls_type == 'dict': ret = "&pb.{}{{\n".format( utils.camelcase(go_cls.get_struct_name())) for sub_cls in go_cls.attrs: ret += "{}:{},\n".format(utils.camelcase(sub_cls.get_name()), TpGoStruct.unzip_return_value(sub_cls)) ret += "}," return ret elif cls_type == 'list': tp_go_cls = go_cls.val ret = "[]*pb.{}{{\n".format( utils.camelcase(tp_go_cls.get_struct_name())) ret += "{}\n".format(TpGoStruct.unzip_return_value(tp_go_cls)) ret += "}" return ret
def _get_plugin_class(plugin_name): """dynamically loads a plugin class Example: For a plugin named volume_manager a module named twister.plugins.volume_manager should exist and define a class named VolumeManager.""" plugin_module = importlib.import_module( 'plugins.%s' % plugin_name) plugin_class = getattr(plugin_module, camelcase(plugin_name)) return plugin_class
def protocol(gen: Generator, name: str, protocol): gen.emit("#pragma once") # u mad molly ? gen.emit("") gen.emit("// Don't edit this code !") gen.emit("// It was generated by ipc-compiler.py") gen.emit("") gen.emit("namespace protocols") gen.emit("{") if len(protocol["enumerations"]) > 0: gen.emit_section("Enumerations") for e in protocol["enumerations"]: enumeration(gen, e, protocol["enumerations"][e]) if len(protocol["structures"]) > 0: gen.emit_section("Structures") print(protocol["structures"]) for s in protocol["structures"]: struct(gen, s, protocol["structures"][s]) gen.emit("") gen.emit("class " + camelcase(protocol["properties"]["name"])) gen.emit("{") gen.push_ident() gen.emit( f"static constexpr int MAGIC = 0x{protocol['properties']['magic']};") gen.emit("") if "client" in protocol: gen.emit_section("Client") peer_request_and_response(gen, protocol["client"]) if "server" in protocol: gen.emit_section("Server") peer_request_and_response(gen, protocol["server"]) gen.pop_ident() gen.emit("};") gen.emit("") gen.emit("}")
def response_packed(gen: Generator, peer): gen.emit("struct Response") gen.emit("{") gen.push_ident() gen.emit("uint32_t magic;") gen.emit(f"{camelcase(peer['type'])}ResponseType type;") if response_any_payload(peer): gen.emit("union") gen.emit("{") gen.push_ident() requests_and_signals = peer["requests"] for meb in requests_and_signals: if len(peer["requests"][meb]["response"]) > 0: gen.emit(camelcase(meb) + "Response", " ", meb) gen.pop_ident() gen.emit("};") gen.pop_ident() gen.emit("};")
def on_bind_field(self, field_name, field_obj): field_obj.data_key = camelcase(field_obj.data_key or field_name)
def build_funcs(func_json, cls_json): file_str = """ package server import ( pb "base_req" "context" "fmt" "git.code.oa.com/gobase/config" pb "git.code.oa.com/TiMatrix/hello/pb3/TiMatrix/pbhello" "git.code.oa.com/gobase/gobase" "git.code.oa.com/gobase/logging" _ "github.com/lib/pq" ) type Server struct{} #SVCSTR# var cgiConf struct { Mysql struct { DBUser string `default:""` DBPwd string `default:""` DBHost string `default:""` DBPort string `default:""` DBName string `default:""` } } func main() { config.Parse(&cgiConf) gobase.GoBaseInit() defer gobase.GoBaseFin() pb.ListenAndServeNonVehicleSvc(&Server{}) } """ tp_str = """ // #FUNCNAME# 写点注释 func (s *Server) #FUNCNAME#(ctx context.Context, in *pb.#INPUTCLS#) (*pb.#OUTPUTCLS#, error) { #INPUTATTRS# // 写点逻辑 return #OUTPUTCLSDETAIL# nil } """ svc_str = "" for func_name in func_json: func_obj = TpGoFunc(func_name, cls_json, func_json) func_input_name = func_obj.get_inputs().get_name() func_return_name = func_obj.get_returns().get_name() temp_tp_str = tp_str.replace("#FUNCNAME#", func_obj.get_name()) temp_tp_str = temp_tp_str.replace("#INPUTCLS#", func_input_name) temp_tp_str = temp_tp_str.replace("#OUTPUTCLS#", func_return_name) # 生成INPUTCLSATTR str_attr = "" input_attrs = func_obj.get_inputs().get_attrs() for item in input_attrs: attr_name = item.get_name() str_attr += "{} := in.{}\n".format(utils.first_lower_camelcase(attr_name), utils.camelcase(attr_name)) temp_tp_str = temp_tp_str.replace("#INPUTATTRS#", str_attr) str_attr = "" return_cls = func_obj.get_returns() str_attr += TpGoStruct.unzip_return_value(return_cls) temp_tp_str = temp_tp_str.replace("#OUTPUTCLSDETAIL#", str_attr) svc_str += temp_tp_str return file_str.replace("#SVCSTR#", svc_str)