Skip to content

Commit

Permalink
增加ProtoFileParser和ProtoFileGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jan 20, 2024
1 parent f53725d commit 5b5cf27
Show file tree
Hide file tree
Showing 29 changed files with 1,113 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public interface IRegex {
default boolean find(String text){
return exec(text) != null;
}

List<String> exec(String text);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,38 @@
import java.util.function.Predicate;

import static io.nop.api.core.ApiErrors.ARG_VALUE;
import static io.nop.commons.CommonErrors.*;
import static io.nop.commons.CommonErrors.ARG_CUR;
import static io.nop.commons.CommonErrors.ARG_EOF;
import static io.nop.commons.CommonErrors.ARG_EXPECTED;
import static io.nop.commons.CommonErrors.ARG_POS;
import static io.nop.commons.CommonErrors.ARG_READER_STATE;
import static io.nop.commons.CommonErrors.ARG_START_LOC;
import static io.nop.commons.CommonErrors.ERR_SCAN_BLANK_EXPECTED;
import static io.nop.commons.CommonErrors.ERR_SCAN_COMMENT_UNEXPECTED_EOF;
import static io.nop.commons.CommonErrors.ERR_SCAN_ILLEGAL_ESCAPE_CHAR;
import static io.nop.commons.CommonErrors.ERR_SCAN_INVALID_CHAR;
import static io.nop.commons.CommonErrors.ERR_SCAN_INVALID_DOUBLE_STRING;
import static io.nop.commons.CommonErrors.ERR_SCAN_INVALID_ESCAPE_UNICODE;
import static io.nop.commons.CommonErrors.ERR_SCAN_INVALID_FLOAT_STRING;
import static io.nop.commons.CommonErrors.ERR_SCAN_INVALID_HEX_INT_STRING;
import static io.nop.commons.CommonErrors.ERR_SCAN_INVALID_LONG_STRING;
import static io.nop.commons.CommonErrors.ERR_SCAN_INVALID_NUMBER_STRING;
import static io.nop.commons.CommonErrors.ERR_SCAN_INVALID_PROP_PATH;
import static io.nop.commons.CommonErrors.ERR_SCAN_INVALID_VAR;
import static io.nop.commons.CommonErrors.ERR_SCAN_INVALID_XML_NAME;
import static io.nop.commons.CommonErrors.ERR_SCAN_NEXT_UNTIL_UNEXPECTED_EOF;
import static io.nop.commons.CommonErrors.ERR_SCAN_NOT_ALLOW_TWO_SEPARATOR_IN_XML_NAME;
import static io.nop.commons.CommonErrors.ERR_SCAN_NOT_DIGIT;
import static io.nop.commons.CommonErrors.ERR_SCAN_NOT_END_PROPERLY;
import static io.nop.commons.CommonErrors.ERR_SCAN_NOT_HEX_CHAR;
import static io.nop.commons.CommonErrors.ERR_SCAN_NUMBER_NOT_INT;
import static io.nop.commons.CommonErrors.ERR_SCAN_STRING_NOT_END;
import static io.nop.commons.CommonErrors.ERR_SCAN_TOKEN_END_EXPECTED;
import static io.nop.commons.CommonErrors.ERR_SCAN_UNEXPECTED_CHAR;
import static io.nop.commons.CommonErrors.ERR_SCAN_UNEXPECTED_STR;
import static io.nop.commons.CommonErrors.ERR_SCAN_UNEXPECTED_TOKEN;
import static io.nop.commons.CommonErrors.ERR_TEXT_ILLEGAL_HEX_STRING;
import static io.nop.commons.CommonErrors.ERR_TEXT_NUMBER_STARTS_WITH_ZERO;
import static io.nop.commons.util.StringHelper.isGraphQLNamePart;
import static io.nop.commons.util.StringHelper.isGraphQLNameStart;

Expand Down Expand Up @@ -723,6 +754,44 @@ public String nextJavaVar() {
return buf.toString();
}

static boolean isWordStart(int c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c == '_');
}

static boolean isWordPart(int c) {
return isWordStart(c) || (c >= '0' && c <= '9');
}

public String nextWord() {
MutableString buf = useBuf();

if (!isWordStart(cur)) {
throw newError(ERR_SCAN_INVALID_VAR).param(ARG_VALUE, buf.toString());
}
consumeToBuf(buf);

while (isWordPart(cur)) {
consumeToBuf(buf);
}

return buf.toString();
}

public String nextWordPath() {
MutableString buf = useBuf();

if (!isWordStart(cur)) {
throw newError(ERR_SCAN_INVALID_VAR).param(ARG_VALUE, buf.toString());
}
consumeToBuf(buf);

while (isWordPart(cur) || cur == '.') {
consumeToBuf(buf);
}

return buf.toString();
}

/**
* 读取一个java变量名
*/
Expand Down Expand Up @@ -1212,6 +1281,10 @@ public int nextHexInt() {
return ch - '0';
}

public NopException newUnexpectedError() {
return newError(ERR_SCAN_UNEXPECTED_CHAR).param(ARG_CUR, cur);
}

public void checkNotNull(Object o, ErrorCode errorCode) {
if (o == null)
throw newError(errorCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public StdDataType toStdDataType() {
}
}

public String getJavaTypeName(boolean mandatory) {
StdDataType dataType = toStdDataType();
return mandatory ? dataType.getMandatoryJavaTypeName() : dataType.getJavaTypeName();
}

public BinaryScalarType toProtoBufType() {
if (isProtoBufType())
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public Object parseJsonDoc(TextScanner sc) {
map(sc);
break;
default:
handler.value(sc.location(), literal(sc));
handler.value(sc.location(), parseLiteral(sc));
}
if (!sc.isEnd())
throw sc.newError(ERR_JSON_DOC_NOT_END_PROPERLY);
Expand All @@ -138,12 +138,12 @@ void object(TextScanner sc) {
return;
default:
SourceLocation loc = sc.location();
Object value = literal(sc);
Object value = parseLiteral(sc);
handler.value(loc, value);
}
}

Object literal(TextScanner sc) {
public Object parseLiteral(TextScanner sc) {
switch (sc.cur) {
case '\"':
case '\'':
Expand Down Expand Up @@ -254,13 +254,13 @@ boolean matchComma(TextScanner sc) {
return false;
}

void skipBlankAndComment(TextScanner sc){
void skipBlankAndComment(TextScanner sc) {
sc.skipBlank();
skipComment(sc);
}

void skipComment(TextScanner sc){
if(!strictMode){
void skipComment(TextScanner sc) {
if (!strictMode) {
parseComment(sc);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.nop.rpc.grpc;

public interface GrpcConstants {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
package io.nop.rpc.model;

import io.nop.core.type.IGenericType;
import io.nop.rpc.model._gen._ApiMessageFieldModel;
import io.nop.xlang.xmeta.ISchema;
import io.nop.xlang.xmeta.impl.SchemaImpl;
import io.nop.xlang.xmeta.impl.SchemaNodeImpl;

public class ApiMessageFieldModel extends _ApiMessageFieldModel{
public ApiMessageFieldModel(){
public class ApiMessageFieldModel extends _ApiMessageFieldModel implements IWithOptions {
public ApiMessageFieldModel() {

}

public IGenericType getType() {
ISchema schema = getSchema();
return schema == null ? null : schema.getType();
}

public void setType(IGenericType type) {
ISchema schema = getSchema();
if (schema == null) {
schema = new SchemaImpl();
setSchema(schema);
}
((SchemaNodeImpl) schema).setType(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.nop.rpc.model._gen._ApiMessageModel;

public class ApiMessageModel extends _ApiMessageModel{
public class ApiMessageModel extends _ApiMessageModel implements IWithOptions{
public ApiMessageModel(){

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.nop.rpc.model._gen._ApiMethodModel;

public class ApiMethodModel extends _ApiMethodModel{
public ApiMethodModel(){
public class ApiMethodModel extends _ApiMethodModel implements IWithOptions {
public ApiMethodModel() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.nop.rpc.model._gen._ApiModel;

public class ApiModel extends _ApiModel{
public class ApiModel extends _ApiModel implements IWithOptions{
public ApiModel(){

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.nop.rpc.model._gen._ApiServiceModel;

public class ApiServiceModel extends _ApiServiceModel{
public class ApiServiceModel extends _ApiServiceModel implements IWithOptions{
public ApiServiceModel(){

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.nop.rpc.model;

import java.util.List;

public interface IWithOptions {
String getDescription();
List<ApiOptionModel> getOptions();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.nop.rpc.model;

public interface RpcModelConstants {
String EXT_PROTO_VERSION = "ext:protoVersion";


String PROTO_TYPE_EMPTY = "google.protobuf.Empty";

String PROTO_TYPE_ANY = "google.protobuf.Any";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.nop.rpc.model;

import io.nop.api.core.exceptions.ErrorCode;

import static io.nop.api.core.exceptions.ErrorCode.define;

public interface RpcModelErrors {
String ARG_VERSION = "version";
String ARG_PATH = "path";

ErrorCode ERR_RPC_UNSUPPORTED_PROTO_VERSION =
define("nop.err.rpc.unsupported-proto-version", "不支持的协议版本: {version}", ARG_VERSION);

ErrorCode ERR_RPC_INVALID_IMPORT_PATH =
define("nop.err.rpc.invalid-import-path", "无效的导入路径: {path}", ARG_PATH);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

// tell cpd to start ignoring code - CPD-OFF
/**
* generate from [18:10:0:0]/nop/schema/api.xdef <p>
* generate from [21:10:0:0]/nop/schema/api.xdef <p>
* 数据域定义。orm模型解析完毕之后,domain的定义会合并到column上。如果设置了domain是以domain的设置为准
*/
@SuppressWarnings({"PMD.UselessOverridingMethod","PMD.UnusedLocalVariable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

// tell cpd to start ignoring code - CPD-OFF
/**
* generate from [7:10:0:0]/nop/schema/api.xdef <p>
* generate from [9:6:0:0]/nop/schema/api.xdef <p>
*
*/
@SuppressWarnings({"PMD.UselessOverridingMethod","PMD.UnusedLocalVariable",
Expand All @@ -23,6 +23,13 @@ public abstract class _ApiImportModel extends io.nop.core.resource.component.Abs
*/
private java.lang.String _as ;

/**
*
* xml name: description
*
*/
private java.lang.String _description ;

/**
*
* xml name: from
Expand Down Expand Up @@ -56,6 +63,25 @@ public void setAs(java.lang.String value){
}


/**
*
* xml name: description
*
*/

public java.lang.String getDescription(){
return _description;
}


public void setDescription(java.lang.String value){
checkAllowChange();

this._description = value;

}


/**
*
* xml name: from
Expand Down Expand Up @@ -110,6 +136,7 @@ protected void outputJson(IJsonHandler out){
super.outputJson(out);

out.put("as",this.getAs());
out.put("description",this.getDescription());
out.put("from",this.getFrom());
out.put("public",this.getPublic());
}
Expand All @@ -124,6 +151,7 @@ protected void copyTo(ApiImportModel instance){
super.copyTo(instance);

instance.setAs(this.getAs());
instance.setDescription(this.getDescription());
instance.setFrom(this.getFrom());
instance.setPublic(this.getPublic());
}
Expand Down
Loading

0 comments on commit 5b5cf27

Please sign in to comment.