如何将TypeScript接口转换为Go结构体?

huangapple go评论93阅读模式
英文:

How to convert TypeScript interface to Go struct?

问题

我正在尝试将我的对象建模工具从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 // 我不确定这一部分
  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{} {
	// 处理数据
	return data
}

我有些困惑,主要是因为SchemaType接口中的动态"key",不确定如何在Go中复制这个功能...

英文:

I'm trying to convert my object modeling tool, built with TypeScript, to Go.

What I have in TypeScript is:

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&lt;object&gt; {
    // Do something with data
    return data;
  };
};

So that I can then do:

const itemSchema = new Schema({
  id: {
    type: String,
    required: true
  },
  createdBy: {
    type: String,
    required: true
  }
});

I've only gotten this far with Go:

type SchemaType struct {
  Key       string // I&#39;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
}

I'm a little stuck mainly because of the dynamic "key" in the SchemaType interface and am not sure how to replicate this in Go...

答案1

得分: 1

[key string]: 部分表示这是一个键类型为 string 的字典。在 Go 中,这将是一个 map[string]<某种类型>

type SchemaType map[string]SchemaTypeEntry

type SchemaTypeEntry struct {
  Type      string
  Required  bool
  // ...
}

或者,可以去掉 SchemaType 类型,并修改 Schema

type Schema struct {
    schema map[string]SchemaTypeEntry
}

关于其他字段,根据你的定义,它们看起来有些奇怪,并且很可能无法按照你在这里展示的方式工作。

Default 应该是一个值,而不是 func()(一个不返回任何内容的函数)。由于你不知道值的类型,所以类型应该是 interface{}any(从 Go 1.18 开始,anyinterface{} 的别名)。

Transform - 这可能是一个接受一个值,对其进行转换并返回一个值的函数 - func(interface{}) interface{}

对于 MinLengthMaxLengthValidate,在这个上下文中不清楚它们代表什么意思 - 不清楚为什么它们在 JavaScript 中是数组,并且你如何确保它们在 Go 中的长度恰好为 2。

英文:

The [key string]: part means that it's a dictionary with keys of type string. In Go that would be a map[string]&lt;some type&gt;.

type SchemaType map[string]SchemaTypeEntry

type SchemaTypeEntry struct {
  Type      string
  Required  bool
  // ...
}

or, drop the SchemaType type and change Schema:

type Schema struct {
    schema map[string]SchemaTypeEntry
}

Now, about the other fields, the look odd as you have defined them, and it's likely that it will not work the way you are showing here.

Default would be a value, not a func() (a function that doesn't return anything). You don't know what type the value is, so the type should be interface {} or any (since Go 1.18 - an alias for interface {}).

Transform - that would likely be a function that takes a value, transforms it, and returns a value - func(interface{}) interface{}

No idea what MinLength, MaxLength and Validate stand for in this context - it is unclear why they are arrays in Javascript, and how you can be sure that they are of exactly length 2 in Go.

huangapple
  • 本文由 发表于 2022年11月26日 11:47:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/74579581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定