Skip to content

Commit

Permalink
Merge branch 'master' into 4.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Jan 3, 2025
2 parents 7ea8323 + 6e3c198 commit 0d0575f
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Map;

@Setter
public class DefaultExtensible implements Extensible {
public class DefaultExtendable implements Extendable {

private Map<String, Object> extensions;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.hswebframework.ezorm.core;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.Map;

/**
* 可扩展的对象,用于动态拓展实体字段属性
*
* @author zhouhao
* @since 4.1.3
*/
public interface Extendable {

/**
* 获取所有扩展属性
*
* @return 扩展属性
*/
@JsonAnyGetter
Map<String, Object> extensions();

/**
* 获取扩展属性
*
* @param property 属性名
* @return 属性值
*/
default Object getExtension(String property) {
Map<String, Object> ext = extensions();
return ext == null ? null : ext.get(property);
}

/**
* 设置扩展属性
*
* @param property 属性名
* @param value 属性值
*/
@JsonAnySetter
void setExtension(String property, Object value);

default void setExtension(String property, int value) {
setExtension(property, (Object) value);
}

default void setExtension(String property, long value) {
setExtension(property, (Object) value);
}

default void setExtension(String property, double value) {
setExtension(property, (Object) value);
}

default void setExtension(String property, float value) {
setExtension(property, (Object) value);
}

default void setExtension(String property, boolean value) {
setExtension(property, (Object) value);
}

default void setExtension(String property, byte value) {
setExtension(property, (Object) value);
}

default void setExtension(String property, char value) {
setExtension(property, (Object) value);
}

default void setExtension(String property, short value) {
setExtension(property, (Object) value);
}


/**
* 方法引用方式设置扩展属性
*
* @param property 属性名
* @param value 属性值
* @param <T> 属性值类型
*/
default <T> void withExtension(StaticMethodReferenceColumn<T> property, T value) {
setExtension(property.getColumn(), value);
}

/**
* 方法引用方式设置扩展属性
*
* @param property 属性名
* @param <T> 属性值类型
*/
default <T> void withExtension(MethodReferenceColumn<T> property) {
setExtension(property.getColumn(), property.get());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @author zhouhao
* @since 4.1.3
*/
@Deprecated
public interface Extensible {

/**
Expand Down Expand Up @@ -41,20 +42,55 @@ default Object getExtension(String property) {
@JsonAnySetter
void setExtension(String property, Object value);

default void setExtension(String property, int value) {
setExtension(property, (Object) value);
}

default void setExtension(String property, long value) {
setExtension(property, (Object) value);
}

default void setExtension(String property, double value) {
setExtension(property, (Object) value);
}

default void setExtension(String property, float value) {
setExtension(property, (Object) value);
}

default void setExtension(String property, boolean value) {
setExtension(property, (Object) value);
}

default void setExtension(String property, byte value) {
setExtension(property, (Object) value);
}

default void setExtension(String property, char value) {
setExtension(property, (Object) value);
}

default void setExtension(String property, short value) {
setExtension(property, (Object) value);
}


/**
* 方法引用方式设置扩展属性
*
* @param property 属性名
* @param value 属性值
* @param <T> 属性值类型
* @param value 属性值
* @param <T> 属性值类型
*/
default <T> void setExtension(StaticMethodReferenceColumn<T> property, T value) {
setExtension(property.getColumn(), value);
}

/**
* 方法引用方式设置扩展属性
*
* @param property 属性名
* @param <T> 属性值类型
* @param <T> 属性值类型
*/
default <T> void setExtension(MethodReferenceColumn<T> property) {
setExtension(property.getColumn(), property.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@

import static org.junit.Assert.*;

public class DefaultExtensibleTest {
public class DefaultExtendableTest {


@Test
@SneakyThrows
public void testJson() {
DefaultExtensible entity = new DefaultExtensible();
DefaultExtendable entity = new DefaultExtendable();

entity.setExtension("extName", "test");

ObjectMapper mapper = new ObjectMapper();

String json = mapper.writerFor(DefaultExtensible.class).writeValueAsString(entity);
String json = mapper.writerFor(DefaultExtendable.class).writeValueAsString(entity);

System.out.println(json);
DefaultExtensible decoded = mapper.readerFor(DefaultExtensible.class).readValue(json);
DefaultExtendable decoded = mapper.readerFor(DefaultExtendable.class).readValue(json);

assertNotNull(decoded.getExtension("extName"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import lombok.Getter;
import lombok.Setter;
import org.hswebframework.ezorm.core.Extensible;
import org.hswebframework.ezorm.core.GlobalConfig;
import org.hswebframework.ezorm.rdb.events.ContextKeyValue;
import org.hswebframework.ezorm.rdb.events.ContextKeys;
import org.hswebframework.ezorm.rdb.executor.wrapper.ResultWrapper;
import org.hswebframework.ezorm.rdb.mapping.EntityColumnMapping;
import org.hswebframework.ezorm.rdb.mapping.EntityPropertyDescriptor;
import org.hswebframework.ezorm.rdb.mapping.LazyEntityColumnMapping;
import org.hswebframework.ezorm.rdb.mapping.MappingFeatureType;
import org.hswebframework.ezorm.rdb.mapping.events.EventResultOperator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.hswebframework.ezorm.rdb.mapping.wrapper;

import org.hswebframework.ezorm.core.Extensible;
import org.hswebframework.ezorm.core.Extendable;
import org.hswebframework.ezorm.core.GlobalConfig;
import org.hswebframework.ezorm.rdb.executor.wrapper.ColumnWrapperContext;
import org.hswebframework.ezorm.rdb.executor.wrapper.ResultWrapper;
Expand Down Expand Up @@ -55,10 +55,10 @@ public void wrapColumn(ColumnWrapperContext<E> context) {
}

protected void setProperty(RDBColumnMetadata col, E instance, String label, Object val) {
if (instance instanceof Extensible && (col == null || !col
if (instance instanceof Extendable && (col == null || !col
.getFeature(EntityPropertyDescriptor.ID)
.isPresent())) {
((Extensible) instance).setExtension(label, val);
((Extendable) instance).setExtension(label, val);
} else {
GlobalConfig.getPropertyOperator().setProperty(instance, label, val);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.hswebframework.ezorm.rdb.utils;

import org.hswebframework.ezorm.core.Extensible;
import org.hswebframework.ezorm.core.Extendable;
import org.hswebframework.ezorm.core.GlobalConfig;
import org.hswebframework.ezorm.core.ObjectPropertyOperator;
import org.hswebframework.ezorm.rdb.mapping.EntityColumnMapping;
Expand All @@ -12,22 +12,22 @@ public class PropertyUtils {

public static Optional<Object> getProperty(Object entity, String property, EntityColumnMapping mapping) {
ObjectPropertyOperator opt = GlobalConfig.getPropertyOperator();
if (entity instanceof Extensible && isExtensibleColumn(property, mapping)) {
return Optional.ofNullable(((Extensible) entity).getExtension(property));
if (entity instanceof Extendable && isExtendableColumn(property, mapping)) {
return Optional.ofNullable(((Extendable) entity).getExtension(property));
}
return opt.getProperty(entity, property);
}

public static boolean isExtensibleColumn(String property, EntityColumnMapping mapping) {
public static boolean isExtendableColumn(String property, EntityColumnMapping mapping) {
return mapping
.getColumnByProperty(property)
.map(c -> !c.getFeature(EntityPropertyDescriptor.ID).isPresent())
.orElse(true);
}

public static void setProperty(Object entity, String property, Object value, EntityColumnMapping mapping) {
if (entity instanceof Extensible && isExtensibleColumn(property, mapping)) {
((Extensible) entity).setExtension(property, value);
if (entity instanceof Extendable && isExtendableColumn(property, mapping)) {
((Extendable) entity).setExtension(property, value);
} else {
GlobalConfig.getPropertyOperator().setProperty(entity, property, value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.hswebframework.ezorm.rdb.supports;

import lombok.*;
import org.hswebframework.ezorm.core.DefaultValueGenerator;
import org.hswebframework.ezorm.core.Extensible;
import org.hswebframework.ezorm.core.Extendable;
import org.hswebframework.ezorm.rdb.mapping.annotation.ColumnType;
import org.hswebframework.ezorm.rdb.mapping.annotation.DefaultValue;
import org.hswebframework.ezorm.rdb.mapping.annotation.EnumCodec;
Expand All @@ -21,7 +20,7 @@
@NoArgsConstructor
@EqualsAndHashCode(exclude = {"createTime", "address"})
@ToString
public class BasicTestEntity implements Serializable, Extensible {
public class BasicTestEntity implements Serializable, Extendable {

@Column(length = 32)
@Id
Expand Down

0 comments on commit 0d0575f

Please sign in to comment.