Skip to content

Commit

Permalink
RecordOutput实现switch条件类型支持
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Sep 28, 2024
1 parent 9249bab commit 3af230c
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 21 deletions.
16 changes: 16 additions & 0 deletions nop-record/src/main/java/io/nop/record/RecordErrors.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public interface RecordErrors {

String ARG_MAX_LENGTH = "maxLength";

String ARG_CASE_VALUE = "caseValue";

String ARG_TYPE_NAME = "typeName";

ErrorCode ERR_RECORD_NO_ENOUGH_DATA =
define("nop.err.record.no-enough-data", "缺少数据,无法读取");

Expand All @@ -41,4 +45,16 @@ public interface RecordErrors {

ErrorCode ERR_RECORD_DECODE_LENGTH_IS_TOO_LONG = define("nop.err.record.decode-length-is-too-long",
ARG_LENGTH, ARG_MAX_LENGTH);

ErrorCode ERR_RECORD_UNKNOWN_FIELD = define("nop.err.record.unknown-field",
"未定义的字段:{fieldName}", ARG_FIELD_NAME);

ErrorCode ERR_RECORD_NO_SWITCH_ON_FIELD = define("nop.err.record.no-switch-on-field",
"条件类型无法确定类型值:{fieldName}", ARG_FIELD_NAME);

ErrorCode ERR_RECORD_NO_MATCH_FOR_CASE_VALUE = define("nop.err.record.no-match-for-case-value",
"字段[{fieldName}]的条件类型没有找到匹配类型:{caseValue}", ARG_FIELD_NAME, ARG_CASE_VALUE);

ErrorCode ERR_RECORD_CASE_VALUE_MAP_TO_UNKNOWN_TYPE = define("nop.err.record.case-value-map-to-unknown-type",
"字段[{fieldName}]的条件类型映射到未定义的类型:{caseValue}=>{typeName}", ARG_FIELD_NAME, ARG_CASE_VALUE, ARG_TYPE_NAME);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package io.nop.record.model;

import io.nop.api.core.exceptions.NopException;
import io.nop.api.core.util.ISourceLocationGetter;
import io.nop.commons.text.SimpleTextTemplate;
import io.nop.core.lang.eval.IEvalFunction;

import java.util.List;

public interface IRecordFieldsMeta {
import static io.nop.record.RecordErrors.ARG_FIELD_NAME;
import static io.nop.record.RecordErrors.ERR_RECORD_UNKNOWN_FIELD;

public interface IRecordFieldsMeta extends ISourceLocationGetter {

List<RecordFieldMeta> getFields();

Expand All @@ -15,5 +20,15 @@ public interface IRecordFieldsMeta {

SimpleTextTemplate getNormalizedTemplate();

RecordFieldMeta requireField(String fieldName);
default RecordFieldMeta requireField(String fieldName) {

RecordFieldMeta field = getField(fieldName);
if (field == null)
throw new NopException(ERR_RECORD_UNKNOWN_FIELD)
.source(this)
.param(ARG_FIELD_NAME, fieldName);
return field;
}

RecordFieldMeta getField(String fieldName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
import io.nop.record.codec.IFieldBinaryCodec;
import io.nop.record.codec.IFieldTextCodec;
import io.nop.record.model._gen._RecordFieldMeta;
import io.nop.xlang.expr.simple.SimpleExprParser;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class RecordFieldMeta extends _RecordFieldMeta implements IRecordFieldsMeta {
private IFieldTextCodec resolvedTextCodec;
private IFieldBinaryCodec resolvedBinaryCodec;

private SimpleTextTemplate normalizedTemplate;

private Charset charsetObj;

public RecordFieldMeta() {

}
Expand All @@ -31,6 +35,18 @@ public SimpleTextTemplate getNormalizedTemplate() {
return this.normalizedTemplate;
}

public Charset getCharsetObj() {
if (charsetObj == null) {
String charset = getCharset();
if (charset == null) {
charsetObj = StandardCharsets.UTF_8;
} else {
charsetObj = Charset.forName(charset);
}
}
return charsetObj;
}

public String getPropOrFieldName() {
String propName = getProp();
if (propName == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@

import io.nop.record.model._gen._RecordFieldSwitch;

public class RecordFieldSwitch extends _RecordFieldSwitch{
public RecordFieldSwitch(){
public class RecordFieldSwitch extends _RecordFieldSwitch {
public RecordFieldSwitch() {

}

public String getTypeByCaseValue(String caseValue) {
RecordFieldSwitchCase caseMeta = getCase(caseValue);
if (caseMeta != null) {
return caseMeta.getType();
}
return getDefault();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@
*/
package io.nop.record.model;

import io.nop.commons.util.StringHelper;
import io.nop.commons.text.SimpleTextTemplate;
import io.nop.record.model._gen._RecordObjectMeta;

public class RecordObjectMeta extends _RecordObjectMeta implements IRecordFieldsMeta {
private String normalizedTemplate;
private SimpleTextTemplate normalizedTemplate;

public RecordObjectMeta() {

}

public String getNormalizedTemplate() {
public SimpleTextTemplate getNormalizedTemplate() {
if (normalizedTemplate == null && getTemplate() != null) {
this.normalizedTemplate = StringHelper.normalizeTemplate(getTemplate());
this.normalizedTemplate = SimpleTextTemplate.normalize(getTemplate());
}
return this.normalizedTemplate;
}

@Override
public RecordFieldMeta requireField(String fieldName) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import io.nop.commons.bytes.ByteString;

import java.io.Closeable;
import java.io.Flushable;
import java.nio.ByteBuffer;

public interface IRecordBinaryOutput extends Closeable {
public interface IRecordBinaryOutput extends Closeable, Flushable {
void writeBytes(byte[] bytes);

void writeBytesPart(byte[] str, int start, int end);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,25 @@
import io.nop.record.codec.impl.DefaultFieldCodecContext;
import io.nop.record.model.IRecordFieldsMeta;
import io.nop.record.model.RecordFieldMeta;
import io.nop.record.model.RecordFieldSwitch;
import io.nop.record.model.RecordFileMeta;
import io.nop.record.model.RecordObjectMeta;
import io.nop.record.model.RecordPaginationMeta;
import io.nop.record.model.RecordTypeMeta;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Map;

import static io.nop.record.RecordErrors.ARG_CASE_VALUE;
import static io.nop.record.RecordErrors.ARG_FIELD_NAME;
import static io.nop.record.RecordErrors.ARG_TYPE_NAME;
import static io.nop.record.RecordErrors.ERR_RECORD_NO_MATCH_FOR_CASE_VALUE;
import static io.nop.record.RecordErrors.ERR_RECORD_NO_SWITCH_ON_FIELD;

public abstract class AbstractModelBasedRecordOutput<T> implements IRecordOutput<T> {
static final Logger LOG = LoggerFactory.getLogger(AbstractModelBasedRecordOutput.class);

Expand Down Expand Up @@ -135,7 +144,7 @@ public void writeObject(RecordObjectMeta recordMeta, Object record, String name)
if (!runIfExpr(recordMeta.getIfExpr(), record, name))
return;

writeTemplateOrFields(recordMeta, record);
writeTemplateOrFields(recordMeta, null, record);

if (recordMeta.getAfterWrite() != null)
recordMeta.getAfterWrite().call1(null, record, context.getEvalScope());
Expand All @@ -158,10 +167,10 @@ protected void writeField(RecordFieldMeta field, Object record) throws IOExcepti
if (record instanceof Collection) {
Collection<?> c = (Collection<?>) record;
for (Object o : c) {
writeVirtualField(field, o);
writeSwitch(field, o);
}
} else {
writeVirtualField(field, record);
writeSwitch(field, record);
}
}

Expand All @@ -177,6 +186,46 @@ boolean runIfExpr(IEvalFunction expr, Object record, String name) {
return true;
}

protected void writeSwitch(RecordFieldMeta field, Object record) throws IOException {
if (field.getSwitch() != null) {
RecordFieldSwitch switchMeta = field.getSwitch();
String onField = switchMeta.getOnField();
String onValue = null;
if (onField != null) {
onValue = ConvertHelper.toString(getPropByName(record, onField));
} else if (switchMeta.getOn() != null) {
onValue = ConvertHelper.toString(switchMeta.getOn().call1(null, record, context.getEvalScope()));
}

if (onValue == null)
throw new NopException(ERR_RECORD_NO_SWITCH_ON_FIELD)
.source(field)
.param(ARG_FIELD_NAME, field.getName());

String caseType = switchMeta.getTypeByCaseValue(onValue);
if (caseType == null)
throw new NopException(ERR_RECORD_NO_MATCH_FOR_CASE_VALUE)
.source(field)
.param(ARG_FIELD_NAME, field.getName())
.param(ARG_CASE_VALUE, onValue);

RecordTypeMeta typeMeta = fileMeta.getType(caseType);
if (typeMeta == null)
throw new NopException(ERR_RECORD_NO_MATCH_FOR_CASE_VALUE)
.source(field)
.param(ARG_FIELD_NAME, field.getName())
.param(ARG_CASE_VALUE, onValue)
.param(ARG_TYPE_NAME, caseType);

Object value = getProp(field, record);
writeTemplateOrFields(typeMeta, field.getCharsetObj(), value);

return;
}

writeVirtualField(field, record);
}

protected void writeVirtualField(RecordFieldMeta field, Object record) throws IOException {
if (field.isVirtual()) {
if (field.getFields() != null) {
Expand All @@ -186,15 +235,15 @@ protected void writeVirtualField(RecordFieldMeta field, Object record) throws IO
}
} else if (field.getFields() != null) {
Object value = getProp(field, record);
writeTemplateOrFields(field, value);
writeTemplateOrFields(field, field.getCharsetObj(), value);
} else {
writeField0(field, record);
}
if (field.getAfterWrite() != null)
field.getAfterWrite().call1(null, record, context.getEvalScope());
}

protected void writeTemplateOrFields(IRecordFieldsMeta fields, Object record) throws IOException {
protected void writeTemplateOrFields(IRecordFieldsMeta fields, Charset charset, Object record) throws IOException {
SimpleTextTemplate template = fields.getNormalizedTemplate();
if (template != null) {
for (Object part : template.getParts()) {
Expand All @@ -203,7 +252,7 @@ protected void writeTemplateOrFields(IRecordFieldsMeta fields, Object record) th
RecordFieldMeta field = fields.requireField(name);
writeField(field, record);
} else {
writeString(part.toString());
writeString(part.toString(), charset);
}
}
} else {
Expand All @@ -215,7 +264,7 @@ protected void writeTemplateOrFields(IRecordFieldsMeta fields, Object record) th

abstract protected void writeOffset(int offset) throws IOException;

abstract protected void writeString(String str) throws IOException;
abstract protected void writeString(String str, Charset charset) throws IOException;

abstract protected void writeField0(RecordFieldMeta field, Object record) throws IOException;

Expand All @@ -227,6 +276,10 @@ protected Object getProp(RecordFieldMeta field, Object record) {
return null;

String propName = field.getPropOrFieldName();
return getPropByName(record, propName);
}

protected Object getPropByName(Object record, String propName) {
if (record instanceof IVariableScope)
return ((IVariableScope) record).getValueByPropPath(propName);

Expand Down
Loading

0 comments on commit 3af230c

Please sign in to comment.