mirror of https://github.com/halo-dev/halo
Extract generateListResultClass into ListResult (#2333)
#### What type of PR is this? /kind improvement /area core /milestone 2.0 /release-note-none #### What this PR does / why we need it: Extract generateListResultClass into ListResult to make it easy to use in other places.pull/2350/head
parent
9b3b49d028
commit
3f12d0108e
|
@ -7,6 +7,8 @@ import java.util.Collections;
|
|||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
import net.bytebuddy.ByteBuddy;
|
||||
import net.bytebuddy.description.type.TypeDescription;
|
||||
import org.springframework.data.util.Streamable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
|
@ -84,4 +86,23 @@ public class ListResult<T> implements Streamable<T> {
|
|||
public boolean isEmpty() {
|
||||
return Streamable.super.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate generic ListResult class. Like {@code ListResult<User>}, {@code ListResult<Post>},
|
||||
* etc.
|
||||
*
|
||||
* @param scheme scheme of the generic type.
|
||||
* @return generic ListResult class.
|
||||
*/
|
||||
public static Class<?> generateGenericClass(Scheme scheme) {
|
||||
var generic =
|
||||
TypeDescription.Generic.Builder.parameterizedType(ListResult.class, scheme.type())
|
||||
.build();
|
||||
return new ByteBuddy()
|
||||
.subclass(generic)
|
||||
.name(scheme.groupVersionKind().kind() + "List")
|
||||
.make()
|
||||
.load(ListResult.class.getClassLoader())
|
||||
.getLoaded();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,8 +5,6 @@ import static org.springdoc.core.fn.builders.parameter.Builder.parameterBuilder;
|
|||
import static org.springdoc.core.fn.builders.requestbody.Builder.requestBodyBuilder;
|
||||
|
||||
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
||||
import net.bytebuddy.ByteBuddy;
|
||||
import net.bytebuddy.description.type.TypeDescription;
|
||||
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.web.reactive.function.server.HandlerFunction;
|
||||
|
@ -55,7 +53,7 @@ public class ExtensionRouterFunctionFactory {
|
|||
.tag(tagName)
|
||||
.response(responseBuilder().responseCode("200")
|
||||
.description("Response " + scheme.plural())
|
||||
.implementation(generateListResultClass()));
|
||||
.implementation(ListResult.generateGenericClass(scheme)));
|
||||
QueryParamBuildUtil.buildParametersFromType(builder, ListRequest.class);
|
||||
})
|
||||
.POST(createHandler.pathPattern(), createHandler,
|
||||
|
@ -130,15 +128,4 @@ public class ExtensionRouterFunctionFactory {
|
|||
|
||||
}
|
||||
|
||||
private Class<?> generateListResultClass() {
|
||||
var generic =
|
||||
TypeDescription.Generic.Builder.parameterizedType(ListResult.class, scheme.type())
|
||||
.build();
|
||||
return new ByteBuddy()
|
||||
.subclass(generic)
|
||||
.name(scheme.groupVersionKind().kind() + "List")
|
||||
.make()
|
||||
.load(this.getClass().getClassLoader())
|
||||
.getLoaded();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package run.halo.app.extension;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ListResultTest {
|
||||
|
||||
@Test
|
||||
void generateGenericClass() {
|
||||
var fakeListClass =
|
||||
ListResult.generateGenericClass(Scheme.buildFromType(FakeExtension.class));
|
||||
assertTrue(ListResult.class.isAssignableFrom(fakeListClass));
|
||||
assertSame(FakeExtension.class, ((ParameterizedType) fakeListClass.getGenericSuperclass())
|
||||
.getActualTypeArguments()[0]);
|
||||
assertEquals("FakeList", fakeListClass.getSimpleName());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue