我在trait
有以下classes
和Scala
:
sealed trait Algorithm {
val name: String
val formula: String
val parameters: Seq[AlgorithmParameter[Any]]
def enhanceAlgorithm[T](algorithm: T): Unit
}
case class LinearRegressionAlgorithm(override val name: String, override val formula: String) extends Algorithm {}
case class GeneralizedLinearRegressionAlgorithm(override val name: String, override val formula: String) extends Algorithm {}
sealed abstract class AlgorithmParameter[+T](val name: String, val value: T, val selectOptions: Seq[T]) extends EnumEntry
object AlgorithmParameter extends Enum[AlgorithmParameter[AnyVal]] with CirceEnum[AlgorithmParameter[AnyVal]] {
case object MaxIter extends AlgorithmParameter[Int]("maxIter", 100, Seq())
}
我在TypeScript
中创建了相应的类,如下所示:
export abstract class Algorithm {
readonly name: string;
readonly formula: string;
readonly parameters: AlgorithmParameter<any>[];
protected constructor(name: string, formula: string, parameters: AlgorithmParameter<any>[]) {
this.name = name;
this.formula = formula;
this.parameters = parameters;
}
}
export class LinearRegressionAlgorithm extends Algorithm {}
export class GeneralizedLinearRegressionAlgorithm extends Algorithm {}
export class AlgorithmParameter<T> {
readonly name: string;
readonly value: T;
readonly selectOptions: T[];
constructor(name: string, value: T, selectOptions: T[]) {
this.name = name;
this.value = value;
this.selectOptions = selectOptions;
}
}
我正在做的是向后端发送REST请求(Scala部分),它返回Seq[Algorithm]
类型的序列,我希望响应将被正确地转换为Typescript类,但它不会
REST方法如下所示:
recommend<T extends Algorithm>(body: RecommenderRequest): Observable<T[]> {
return this.http.post<T[]>(environment.baseUrl + this.recommenderPath + this.algoRecommendationsPath, JSON.stringify(body))
.catch((error: any) => Observable.throw(error.json().error || 'Server error'))
}
响应被转换为一个数组,如下所示:
似乎已经创建了TS algorithm-objects
,但它们被封装在另一个对象中,我无法轻松访问
为了得到第一个算法的name
,调用看起来如下:response[0].LinearRegressionAlgorithm.name
,但我希望以这样的方式创建Http响应数组,以便可以简单地编写response[0].name
Angular不会创建这些类型的对象。只是被告知打字是什么。您的Scala程序正在返回正在显示的数据。您需要对每个结果执行映射操作以获取正确的对象。
一个例子如下:
recommend<T extends Algorithm>(body: RecommenderRequest): Observable<T[]> {
return this.http.post<T[]>(environment.baseUrl + this.recommenderPath + this.algoRecommendationsPath, JSON.stringify(body)).pipe(
map(algorithms => algorithms.map(algorithm => this.toAlgorithm(algorithm)),
catchError(error => Observable.throw(error.json().error || 'Server error'))
);
}
private toAlgorithm(algorithm: { [type: string]: Partial<Algorithm> }): Algorithm {
const type = Object.keys(algorithm)[0];
const name = algorithm[type].name;
const formula = algorithm[type].formula;
const parameters = (algorithm[type].parameters || [])
.map(parameter => this.toParameter(parameter));
switch (type) {
case 'LinearRegressionAlgorithm':
return new LinearRegressionAlgorithm(name, formula, parameters);
case 'GeneralizedLinearRegressionAlgorithm':
return new GeneralizedLinearRegressionAlgorithm(name, formula, parameters);
default:
throw new Error('Unsupported algorithm');
}
}
private toParameter(paramerter: Partial<AlgorithmParameter<any>>): AlgorithmParameter<any> {
return new AlgorithmParameter<any>(parameter.name, parameter.value, parameter.selectedOptions);
}
如果您不需要这些算法的实际实例,并且您只需要算法中的值,则可以将Algorithm
类更改为接口,这样更简单:
recommend<T extends Algorithm>(body: RecommenderRequest): Observable<T[]> {
return this.http.post<T[]>(environment.baseUrl + this.recommenderPath + this.algoRecommendationsPath, JSON.stringify(body)).pipe(
map(algorithms => algorithms.map(algorithm => Object.values(algorithm)[0]),
catchError(error => Observable.throw(error.json().error || 'Server error'))
);
}
以这种方式执行操作会丢失有关算法类型的任何信息,因此您需要在算法接口上为该类型添加另一个属性,并在上面的映射过程中设置它。只是取决于你的需求。