Skip to content

Commit

Permalink
Merge pull request #2435 from hongwei1/refactor/cleanConnectorTrait
Browse files Browse the repository at this point in the history
Refactor/clean connector trait
  • Loading branch information
simonredfern authored Nov 5, 2024
2 parents d201ab9 + fe0de91 commit ca5b6b2
Show file tree
Hide file tree
Showing 20 changed files with 22,800 additions and 8,126 deletions.

This file was deleted.

10 changes: 4 additions & 6 deletions obp-api/src/main/scala/code/bankconnectors/ConnectorUtils.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package code.bankconnectors

import java.lang.reflect.Method

import code.api.util.{CallContext, CustomJsonFormats, OptionalFieldSerializer, OBPQueryParam}
import code.api.util.{CallContext, CustomJsonFormats, OBPQueryParam, OptionalFieldSerializer}
import com.openbankproject.commons.ExecutionContext.Implicits.global
import com.openbankproject.commons.dto.{InBoundTrait, OutInBoundTransfer}
import com.openbankproject.commons.model.TopicTrait
Expand All @@ -13,13 +11,13 @@ import net.liftweb.json.JsonDSL._
import net.liftweb.json.{Formats, JObject, JValue}
import net.sf.cglib.proxy.{Enhancer, MethodInterceptor, MethodProxy}
import org.apache.commons.lang3.StringUtils

import java.lang.reflect.Method
import scala.concurrent.Future
import scala.reflect.runtime.universe
import scala.reflect.ManifestFactory
import scala.reflect.runtime.universe

object ConnectorUtils {

lazy val proxyConnector: Connector = {
val excludeProxyMethods = Set("getDynamicEndpoints", "dynamicEntityProcess", "setAccountHolder", "updateUserAccountViewsOld")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package code.bankconnectors.akka

import code.bankconnectors.ConnectorBuilderUtil._
import code.bankconnectors.generator.ConnectorBuilderUtil._

import scala.language.postfixOps

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package code.bankconnectors.generator

import code.bankconnectors.generator.ConnectorBuilderUtil._

import scala.reflect.runtime.{universe => ru}

object CommonsCaseClassGenerator extends App {

val returnModels: Iterable[ru.Type] = connectorDeclsMethodsReturnOBPRequiredType
.map(it => it.asMethod.returnType)
.filter(it => {
val symbol = it.typeSymbol
val isAbstract = symbol.isAbstract
isAbstract //Depends here, maybe no need this guard for some commons classes.
})
.toSet

returnModels.map(_.typeSymbol.fullName).foreach(it => println(s"import $it"))

def mkClass(tp: ru.Type) = {
val varibles = tp.decls.map(it => s"${it.name} :${it.typeSignature.typeSymbol.name}").mkString(", \n ")

s"""
|case class ${tp.typeSymbol.name}Commons(
| $varibles) extends ${tp.typeSymbol.name}
|object ${tp.typeSymbol.name}Commons extends Converter[${tp.typeSymbol.name}, ${tp.typeSymbol.name}Commons]
""".stripMargin
}
// private val str: String = ru.typeOf[Bank].decls.map(it => s"${it.name} :${it.typeSignature.typeSymbol.name}").mkString(", \n")
returnModels.map(mkClass).foreach(println)
println()

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package code.bankconnectors
package code.bankconnectors.generator

import java.io.File
import java.util.Date

import code.api.util.{APIUtil, CallContext}
import code.api.util.CodeGenerateUtils.createDocExample
import code.api.util.{APIUtil, CallContext}
import code.bankconnectors.Connector
import code.bankconnectors.vSept2018.KafkaMappedConnector_vSept2018
import com.openbankproject.commons.util.ReflectUtils
import org.apache.commons.io.FileUtils
import org.apache.commons.lang3.StringUtils.uncapitalize

import scala.collection.immutable.List
import java.io.File
import java.util.Date
import scala.language.postfixOps
import scala.reflect.runtime.universe._
import scala.reflect.runtime.{universe => ru}
Expand All @@ -31,9 +30,38 @@ object ConnectorBuilderUtil {
// if(ctClass != null) ctClass.detach()
}

private val mirror: ru.Mirror = ru.runtimeMirror(getClass().getClassLoader)
private val clazz: ru.ClassSymbol = ru.typeOf[Connector].typeSymbol.asClass
/**
* //def getAdapterInfo(callContext: Option[CallContext]) : Future[Box[(InboundAdapterInfoInternal, Option[CallContext])]] = ???
* //def validateAndCheckIbanNumber(iban: String, callContext: Option[CallContext]): OBPReturnType[Box[IbanChecker]] = ???
*
* This method will only extact the first return class from the method, this is the OBP pattern.
* so we can use it for gernerate the commons case class.
*
* eg: getAdapterInfo --> return InboundAdapterInfoInternal
* validateAndCheckIbanNumber -->return IbanChecker
*/
def extractReturnModel(tp: ru.Type): ru.Type = {
if (tp.typeArgs.isEmpty) {
tp
} else {
extractReturnModel(tp.typeArgs(0))
}
}

val mirror: ru.Mirror = ru.runtimeMirror(this.getClass.getClassLoader)
val clazz: ru.ClassSymbol = mirror.typeOf[Connector].typeSymbol.asClass
val connectorDecls: MemberScope = mirror.typeOf[Connector].decls
val connectorDeclsMethods: Iterable[Symbol] = connectorDecls.filter(symbol => {
val isMethod = symbol.isMethod && !symbol.asMethod.isVal && !symbol.asMethod.isVar && !symbol.asMethod.isConstructor && !symbol.isProtected
isMethod})
val connectorDeclsMethodsReturnOBPRequiredType: Iterable[MethodSymbol] = connectorDeclsMethods
.map(it => it.asMethod)
.filter(it => {
extractReturnModel(it.returnType).typeSymbol.fullName.matches("((code\\.|com.openbankproject\\.).+)|(scala\\.Boolean)") //to make sure, it returned the OBP class and Boolean.
})

private val classMirror: ru.ClassMirror = mirror.reflectClass(clazz)

/*
* generateMethods and buildMethods has the same function, only responseExpression parameter type
* different, because overload method can't compile for different responseExpression parameter.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,10 @@
package code.bankconnectors
package code.bankconnectors.generator

import code.api.util.{APIUtil, NewStyle}
import org.apache.commons.io.FileUtils

import java.io.File
import java.util.Date
import scala.concurrent.Future
import scala.reflect.runtime.{universe => ru}
import code.bankconnectors.generator.ConnectorBuilderUtil._

object InOutCaseClassGenerator extends App {

def extractReturnModel(tp: ru.Type): ru.Type = {
if (tp.typeArgs.isEmpty) {
tp
} else {
extractReturnModel(tp.typeArgs(0))
}
}

private val mirror: ru.Mirror = ru.runtimeMirror(this.getClass.getClassLoader)
private val clazz: ru.ClassSymbol = mirror.typeOf[Connector].typeSymbol.asClass
private val returnObpClassMethods1= mirror.typeOf[Connector].decls
private val returnObpClassMethods2= returnObpClassMethods1.filter(symbol => {
val isMethod = symbol.isMethod && !symbol.asMethod.isVal && !symbol.asMethod.isVar && !symbol.asMethod.isConstructor && !symbol.isProtected
isMethod})
private val returnObpClassMethods3 = returnObpClassMethods2.map(it => it.asMethod)
.filter(it => {
extractReturnModel(it.returnType).typeSymbol.fullName.matches("((code\\.|com.openbankproject\\.).+)|(scala\\.Boolean)") //to make sure, it returned the OBP class.
})

val code = returnObpClassMethods3.map(it => {
val code = connectorDeclsMethodsReturnOBPRequiredType.map(it => {
val returnType = it.returnType
val tp = extractReturnModel(returnType)
val isCaseClass = tp.typeSymbol.asClass.isCaseClass
Expand All @@ -54,6 +29,8 @@ object InOutCaseClassGenerator extends App {
""".stripMargin
})
code.foreach(println)
println()

println("#################################Finished########################################################################")
println("Please copy and compair the result to obp-commons/src/main/scala/com/openbankproject/commons/model/CommonModel.scala")

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package code.bankconnectors.rabbitmq.Adapter

import code.api.ResourceDocs1_4_0.MessageDocsSwaggerDefinitions.{outboundAdapterAuthInfo, successStatus}
import code.api.ResourceDocs1_4_0.MessageDocsSwaggerDefinitions.successStatus
import code.api.util.APIUtil.MessageDoc
import code.api.util.CustomJsonFormats.formats
import code.api.util.{APIUtil, NewStyle, OptionalFieldSerializer}
import code.bankconnectors.{Connector, ConnectorBuilderUtil}
import code.bankconnectors.Connector.connector
import code.bankconnectors.ConnectorBuilderUtil._
import code.api.util.{APIUtil, NewStyle}
import code.bankconnectors.generator.ConnectorBuilderUtil
import code.bankconnectors.generator.ConnectorBuilderUtil._
import code.bankconnectors.rabbitmq.RabbitMQConnector_vOct2024
import com.openbankproject.commons.model.{Status, TopicTrait}
import com.openbankproject.commons.util.Functions
Expand All @@ -24,7 +23,7 @@ import scala.collection.mutable.ArrayBuffer
* create ms sql server stored procedure according messageDocs.
*/
object AdapterStubBuilder {
specialMethods// do not delete this line, it is to modify "MappedWebUiPropsProvider"
commonMethodNames// do not delete this line, it is to modify "MappedWebUiPropsProvider", to avoid access DB cause dataSource not found exception
object StatusSerializer extends Serializer[Status] {

override def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), Status] = Functions.doNothing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package code.bankconnectors.rabbitmq

import code.bankconnectors.ConnectorBuilderUtil._
import code.bankconnectors.generator.ConnectorBuilderUtil._
import net.liftweb.util.StringHelpers

import scala.language.postfixOps
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package code.bankconnectors.rest

import code.bankconnectors.ConnectorBuilderUtil._
import code.bankconnectors.generator.ConnectorBuilderUtil._

import scala.language.postfixOps

Expand Down
Loading

0 comments on commit ca5b6b2

Please sign in to comment.