How can I return two different concrete types from a single method in Go 1.18?

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

How can I return two different concrete types from a single method in Go 1.18?

问题

让我们假设我有这段代码:

type Type1 struct {
    Name string `json:"name,omitempty"`
    Path string `json:"path"`
    File string `json:"file"`
    Tag  int    `json:"tag"`
    Num  int    `json:"num"`
}

func LoadConfiguration(data []byte) (*Type1, error) {
    config, err := loadConf1(data)
    if err != nil {
        return nil, err
    }
    confOther, err := loadConfOther1()
    if err != nil {
        return nil, err
    }
    // 处理 confOther
    fmt.Println("confOther", confOther)
    if confOther.Tag == 0 {
        config.Num = 5
    }

    // 处理 Type1 的属性
    if config.Tag == 0 {
        config.Tag = 5
    }
    if config.Num == 0 {
        config.Num = 4
    }

    return config, nil
}

func loadConf1(bytes []byte) (*Type1, error) {
    config := &Type1{}
    if err := json.Unmarshal(bytes, config); err != nil {
        return nil, fmt.Errorf("无法加载配置:%v", err)
    }

    return config, nil
}

func loadConfOther1() (*Type1, error) {
    // 返回特定类型的值
    flatconfig := &Type1{}

    // 读取文件内容作为 []byte
    // 这里简化示例,将其写为固定数组
    fileContent := []byte{10, 22, 33, 44, 55}

    if err := json.Unmarshal(fileContent, flatconfig); err != nil {
        return nil, fmt.Errorf("无法读取配置:%v", err)
    }

    return flatconfig, nil
}

唯一的公共函数是 LoadConfiguration

这段代码基于真实代码,用于将 JSON 数据读取为特定的结构体。如果有些部分看起来无用,那是因为我简化了原始代码。

上面的代码是可以工作的,但现在我想创建另一个名为 Type2 的结构体类型,并且在不复制和粘贴所有内容的情况下重用相同的方法来读取数据到 Type2 中。

type Type2 struct {
    Name  string                  `json:"name,omitempty"`
    Path  string                  `json:"path"`
    Map   *map[string]interface{} `json:"map"`
    Other string                  `json:"other"`
}

基本上,我希望能够调用 LoadConfiguration 来获取 Type2。我可以接受调用一个特定的方法,比如 LoadConfiguration2,但我不想复制和粘贴 loadConf1loadConfOther1

在 Go 1.18 中,有没有一种符合惯用方式的方法来实现这个需求呢?

英文:

Let say that I have this code:

type Type1 struct {
Name string `json:"name,omitempty"`
Path string `json:"path"`
File string `json:"file"`
Tag  int    `json:"tag"`
Num  int    `json:"num"`
}
func LoadConfiguration(data []byte) (*Type1, error) {
config, err := loadConf1(data)
if err != nil {
return nil, err
}
confOther, err := loadConfOther1()
if err != nil {
return nil, err
}
// do something with confOther
fmt.Println("confOther", confOther)
if confOther.Tag == 0 {
config.Num = 5
}
// do something with config attributes of type1
if config.Tag == 0 {
config.Tag = 5
}
if config.Num == 0 {
config.Num = 4
}
return config, nil
}
func loadConf1(bytes []byte) (*Type1, error) {
config := &Type1{}
if err := json.Unmarshal(bytes, config); err != nil {
return nil, fmt.Errorf("cannot load config: %v", err)
}
return config, nil
}
func loadConfOther1() (*Type1, error) {
// return value of this specific type
flatconfig := &Type1{}
// read a file as []byte
// written as a fixed array to simplify this example
fileContent := []byte{10, 22, 33, 44, 55}
if err := json.Unmarshal(fileContent, flatconfig); err != nil {
return nil, fmt.Errorf("cannot read config %v", err)
}
return flatconfig, nil
}

The only public function is LoadConfiguration.

It's based on a real code and It's used to read a json data as a specific struct. If something seems useless, it's because I simplified the original code.

The code above is ok, but now I want to create another struct type called "Type2" and re-use the same methods to read data into Type2 without copying and pasting everything.

type Type2 struct {
Name  string                  `json:"name,omitempty"`
Path  string                  `json:"path"`
Map   *map[string]interface{} `json:"map"`
Other string                  `json:"other"`
}

Basically, I want to be able to call LoadConfiguration to get also Type2. I can accept to call a specific method like LoadConfiguration2, but I don't want to copy and paste also loadConf1 and loadConfOther1.
Is there a way to do that in an idiomatic way in Go 1.18?

答案1

得分: 1

实际上,你在问题中展示的代码除了将一个类型传递给json.Unmarshal并格式化一个错误之外,没有做任何其他操作,所以你可以重写你的函数,使其行为与它完全相同:

func LoadConfiguration(data []byte) (*Type1, error) {
    config := &Type1{}
    if err := loadConf(data, config); err != nil {
        return nil, err
    }
    // ...
}

// "神奇地"接受任何类型
// 实际上,你可以完全摆脱中间函数
func loadConf(bytes []byte, config any) error {
    if err := json.Unmarshal(bytes, config); err != nil {
        return fmt.Errorf("无法加载配置:%v", err)
    }
    return nil
}

如果代码实际上比只是将指针传递给json.Unmarshal做更多的事情,那么它可以从类型参数中受益。

type Configurations interface {
    Type1 | Type2
}

func loadConf[T Configurations](bytes []byte) (*T, error) {
    config := new(T)
    if err := json.Unmarshal(bytes, config); err != nil {
        return nil, fmt.Errorf("无法加载配置:%v", err)
    }
    return config, nil
}

func loadConfOther[T Configurations]() (*T, error) {
    flatconfig := new(T)
    // ... 代码
    return flatconfig, nil
}

在这些情况下,你可以使用new(T)创建一个新的指针,然后json.Unmarshal将负责将字节切片或文件的内容反序列化到其中 - 前提是JSON实际上可以反序列化为任一结构体。

顶层函数中的特定类型代码仍然应该是不同的,特别是因为你想要使用显式具体类型实例化通用函数。所以我建议保留LoadConfiguration1LoadConfiguration2

func LoadConfiguration1(data []byte) (*Type1, error) {
    config, err := loadConf[Type1](data)
    if err != nil {
        return nil, err
    }
    confOther, err := loadConfOther[Type1]()
    if err != nil {
        return nil, err
    }

    // ... 特定类型的代码

    return config, nil
}

然而,如果特定类型的代码只是其中的一小部分,你可能可以通过类型切换来解决,尽管在你的情况下这似乎不是一个可行的选项。它可能如下所示:

func LoadConfiguration[T Configuration](data []byte) (*T, error) {
    config, err := loadConf[T](data)
    if err != nil {
        return nil, err
    }
    // 假设只有一个类型参数类型的值
    // 特定类型的代码
    switch t := config.(type) {
        case *Type1:
            // ... 一些 *Type1 特定的代码
        case *Type2:
            // ... 一些 *Type2 特定的代码
        default:
            // 实际上不可能发生,因为 T 被限制为 Configuration,但如果你扩展了联合并忘记添加相应的 case,它有助于捕获错误
            panic("无效的类型")
    }

    return config, nil
}

最小示例 playground:https://go.dev/play/p/-rhIgoxINTZ

英文:

Actually the code shown in your question doesn't do anything more than passing a type into json.Unmarshal and format an error so you can rewrite your function to behave just like it:

func LoadConfiguration(data []byte) (*Type1, error) {
config := &Type1{}
if err := loadConf(data, config); err != nil {
return nil, err
}
// ...
}
// "magically" accepts any type
// you could actually get rid of the intermediate function altogether
func loadConf(bytes []byte, config any) error {
if err := json.Unmarshal(bytes, config); err != nil {
return fmt.Errorf("cannot load config: %v", err)
}
return nil
}

In case the code actually does something more than just passing a pointer into json.Unmarshal, it can benefit from type parameters.

type Configurations interface {
Type1 | Type2
}
func loadConf[T Configurations](bytes []byte) (*T, error) {
config := new(T)
if err := json.Unmarshal(bytes, config); err != nil {
return nil, fmt.Errorf("cannot load config: %v", err)
}
return config, nil
}
func loadConfOther[T Configurations]() (*T, error) {
flatconfig := new(T)
// ... code
return flatconfig, nil
}

In these cases you can create a new pointer of either type with new(T) and then json.Unmarshal will take care of deserializing the content of the byte slice or file into it — provided the JSON can be actually unmarshalled into either struct.

The type-specific code in the top-level function should still be different, especially because you want to instantiate the generic functions with an explicit concrete type. So I advise to keep LoadConfiguration1 and LoadConfiguration2.

func LoadConfiguration1(data []byte) (*Type1, error) {
config, err := loadConf[Type1](data)
if err != nil {
return nil, err
}
confOther, err := loadConfOther[Type1]()
if err != nil {
return nil, err
}
// ... type specific code
return config, nil
}

However if the type-specific code is a small part of it, you can probably get away with a type-switch for the specific part, though it doesn't seem a viable option in your case. I would look like:

func LoadConfiguration[T Configuration](data []byte) (*T, error) {
config, err := loadConf[T](data)
if err != nil {
return nil, err
}
// let's pretend there's only one value of type parameter type
// type-specific code
switch t := config.(type) {
case *Type1:
// ... some *Type1 specific code
case *Type2:
// ... some *Type2 specific code
default:
// can't really happen because T is restricted to Configuration but helps catch errors if you extend the union and forget to add a corresponding case
panic("invalid type")
}
return config, nil
}

Minimal example playground: https://go.dev/play/p/-rhIgoxINTZ

huangapple
  • 本文由 发表于 2022年7月4日 16:05:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/72853519.html
匿名

发表评论

匿名网友

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

确定