我正在尝试将使用 TypeScript 构建的对象建模工具转换为 Go。
我在 TypeScript 中拥有的是:
interface SchemaType {
[key: string]: {
type: string;
required?: boolean;
default?: any;
validate?: any[];
maxlength?: any[];
minlength?: any[],
transform?: Function;
};
};
class Schema {
private readonly schema;
constructor(schema: SchemaType) {
this.schema = schema;
};
public validate(data: object): Promise<object> {
// Do something with data
return data;
};
};
这样我就可以做:
const itemSchema = new Schema({
id: {
type: String,
required: true
},
createdBy: {
type: String,
required: true
}
});
我对 Go 的了解只有这么远:
type SchemaType struct {
Key string // I'm not sure about this bit
Type string
Required bool
Default func()
Validate [2]interface{}
Maxlength [2]interface{}
Minlength [2]interface{}
Transform func()
}
type Schema struct {
schema SchemaType
}
func (s *Schema) NewSchema(schema SchemaType) {
s.schema = schema
}
func (s *Schema) Validate(collection string, data map[string]interface{}) map[string]interface{} {
// do something with data
return data
}
我有点卡住了,主要是因为 SchemaType 接口中的动态“键”,我不确定如何在 Go 中复制它......
DIEA
相关分类