package com.qianwen.core.tool.metadata.types;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import java.util.Collection;
|
import java.util.Collections;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
import com.qianwen.core.tool.metadata.Converter;
|
import com.qianwen.core.tool.metadata.DataType;
|
import com.qianwen.core.tool.metadata.ValidateResult;
|
|
/* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/metadata/types/ArrayType.class */
|
public class ArrayType extends AbstractType<ArrayType> implements DataType, Converter<List<Object>> {
|
public static final String ID = "array";
|
private DataType elementType;
|
|
public void setElementType(final DataType elementType) {
|
this.elementType = elementType;
|
}
|
|
public DataType getElementType() {
|
return this.elementType;
|
}
|
|
@Override // org.springblade.core.tool.metadata.Metadata
|
public String getId() {
|
return ID;
|
}
|
|
@Override // org.springblade.core.tool.metadata.Metadata
|
public String getName() {
|
return "数组";
|
}
|
|
public ArrayType elementType(DataType elementType) {
|
this.elementType = elementType;
|
return this;
|
}
|
|
@Override // org.springblade.core.tool.metadata.DataType
|
public ValidateResult validate(Object value) {
|
List<Object> listValue = convert(value);
|
if (this.elementType != null && (value instanceof Collection)) {
|
for (Object data : listValue) {
|
ValidateResult result = this.elementType.validate(data);
|
if (!result.isSuccess()) {
|
return result;
|
}
|
}
|
}
|
return ValidateResult.success(listValue);
|
}
|
|
@Override // org.springblade.core.tool.metadata.FormatSupport
|
public Object format(Object value) {
|
if (this.elementType != null && (value instanceof Collection)) {
|
Collection<?> collection = (Collection) value;
|
return new JSONArray((List) collection.stream().map(data -> {
|
return this.elementType.format(data);
|
}).collect(Collectors.toList()));
|
}
|
return JSON.toJSON(value);
|
}
|
|
/* JADX WARN: Can't rename method to resolve collision */
|
@Override // org.springblade.core.tool.metadata.Converter
|
public List<Object> convert(Object value) {
|
if (value instanceof Collection) {
|
return (List) ((Collection) value).stream().map(val -> {
|
if (this.elementType instanceof Converter) {
|
return ((Converter) this.elementType).convert(val);
|
}
|
return val;
|
}).collect(Collectors.toList());
|
}
|
if (value instanceof String) {
|
return JSON.parseArray(String.valueOf(value));
|
}
|
return Collections.singletonList(value);
|
}
|
}
|