亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

創建需要其他重復邏輯作為先決條件的函數(干凈的代碼)

創建需要其他重復邏輯作為先決條件的函數(干凈的代碼)

Go
慕碼人8056858 2023-04-17 14:58:50
我有以下我需要的 yaml 文件parse(解析按預期工作)并且需要data從 yaml 文件內容中提供一個應該由以下解耦函數公開的文件我需要提供以下功能(這里是其中一些功能的示例,需要更多具有相同模式的功能......)獲取應用程序()獲取服務()GetApp(應用名稱)GetServiceForApp(應用名稱)這是代碼(有效...)var DMZ = []byte(`applications:  - name: app1    type: php    src: /app1    host: us    use:       - redis      - mysql  - name: app2    type: rust    src: /app2    host: eu    use:       - mongo      - mysql  - name: app3    type: golang    src: /app3    host: us    use:       - postgress      - mysqlservices:  - name: mongo    type: db    host: us  - name: mysql    type: db    host: eu  - name: postgress    type: db    host: us  - name: redis    type: db    host: us   `)
查看完整描述

3 回答

?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

解析指針類型結構值中的值,該值將存儲指針指向的結構中的值。然后 Create 方法接收所有你想從中獲取結構內應用程序或服務值的方法。


在所有方法中使用指針接收器,您將能夠訪問在解析 yaml 時更新的原始結構。


創建一個指向該結構的空實例的指針并將其作為方法接收器傳遞。然后在解組中訪問該結構以更新原始結構中的 yaml 數據。通過使用指向方法的指針接收器訪問原始結構的值來訪問每個函數中更新的結構。


package main


import (

    "fmt"

    "log"


    yaml "gopkg.in/yaml.v2"

)


var dmz = []byte(`

applications:

  - name: app1

    type: php

    src: /app1

    host: us

    use: 

      - redis

      - mysql


  - name: app2

    type: rust

    src: /app2

    host: eu

    use: 

      - mongo

      - mysql


  - name: app3

    type: golang

    src: /app3

    host: us

    use: 

      - postgress

      - mysql



services:

  - name: mongo

    type: db

    host: us


  - name: mysql

    type: db

    host: eu


  - name: postgress

    type: db

    host: us


  - name: redis

    type: db

    host: us   

`)


type DMZ struct {

    Applications []*Applications `yaml:"applications,omitempty"`

    Services     []*Services     `yaml:"services,omitempty"`

}


type Applications struct {

    Name string

    Type string

    Src  string `yaml:"src,omitempty"`

    use  []Use  `yaml:"use,omitempty"`

}

type Services struct {

    Name string

    Type string

    Host string `yaml:"host,omitempty"`

}

type Use struct {

    Name string `yaml:"name,omitempty"`

    host string `yaml:"host,omitempty"`

    Type string `yaml:"type,omitempty"`

}


func main() {

    dm := &DMZ{}

    result, err := dm.Parse(dmz)

    dm.getApp("app1")

    if err != nil {

        fmt.Println(err)

    }

    fmt.Println(result)

    fmt.Println(dm.getApp("app2"))

    fmt.Println(dm.GetServiceForApp("mongo"))

}


func (dmz *DMZ) getApps() []*Applications {

    return dmz.Applications

}


func (dmz *DMZ) getServices() []*Services {

    return dmz.Services

}


func (dmz *DMZ) getApp(appname string) *Applications {

    for _, value := range dmz.Applications {

        if appname == value.Name {

            fmt.Println((*value).Name)

            return value

        }

    }

    return nil

}


func (dmz *DMZ) GetServiceForApp(appname string) *Services {

    for _, value := range dmz.Services {

        if appname == value.Name {

            return value

        }

    }

    return nil

}


// Parse file

func (dmz *DMZ) Parse(yamlContent []byte) (out *DMZ, err error) {

    err = yaml.Unmarshal([]byte(yamlContent), &dmz)

    if err != nil {

        log.Fatal("Yaml file is not valid, Error: " + err.Error())

    }

    return dmz, err

}

游樂場上的工作代碼


如果你想讓你的代碼更干凈,那么你也可以跳過從 parse 函數返回的結構。由于我們正在傳遞一個指針類型的接收器并將原始結構更新為:


package main


import (

    "fmt"

    "log"


    yaml "gopkg.in/yaml.v2"

)


var dmz = []byte(`

applications:

  - name: app1

    type: php

    src: /app1

    host: us

    use: 

      - redis

      - mysql


  - name: app2

    type: rust

    src: /app2

    host: eu

    use: 

      - mongo

      - mysql


  - name: app3

    type: golang

    src: /app3

    host: us

    use: 

      - postgress

      - mysql



services:

  - name: mongo

    type: db

    host: us


  - name: mysql

    type: db

    host: eu


  - name: postgress

    type: db

    host: us


  - name: redis

    type: db

    host: us   

`)


type DMZ struct {

    Applications []*Applications `yaml:"applications,omitempty"`

    Services     []*Services     `yaml:"services,omitempty"`

}


type Applications struct {

    Name string

    Type string

    Src  string `yaml:"src,omitempty"`

    use  []Use  `yaml:"use,omitempty"`

}

type Services struct {

    Name string

    Type string

    Host string `yaml:"host,omitempty"`

}

type Use struct {

    Name string `yaml:"name,omitempty"`

    host string `yaml:"host,omitempty"`

    Type string `yaml:"type,omitempty"`

}


func main() {

    dm := &DMZ{}

    dm.Parse(dmz)

    fmt.Println(dm.getApp("app2"))

    fmt.Println(dm.GetServiceForApp("mongo"))

}


func (dmz *DMZ) getApps() []*Applications {

    return dmz.Applications

}


func (dmz *DMZ) getServices() []*Services {

    return dmz.Services

}


func (dmz *DMZ) getApp(appname string) *Applications {

    for _, value := range dmz.Applications {

        if appname == value.Name {

            fmt.Println((*value).Name)

            return value

        }

    }

    return nil

}


func (dmz *DMZ) GetServiceForApp(appname string) *Services {

    for _, value := range dmz.Services {

        if appname == value.Name {

            return value

        }

    }

    return nil

}


// Parse file

func (dmz *DMZ) Parse(yamlContent []byte) {

    if err := yaml.Unmarshal([]byte(yamlContent), &dmz); err != nil {

        log.Fatal("Yaml file is not valid, Error: " + err.Error())

    }

}

我們可以注意到 Parse 函數將變得更加干凈,因為我們沒有從它返回任何東西,我們只是使用方法 receiver 更新原始結構,這是實現您一直試圖實現的目標的更好方法。


您還可以選擇通過將 struct 中的方法定義為您嘗試為其實現接口的接收器來實現接口:


package main


import (

    "fmt"

    "log"


    yaml "gopkg.in/yaml.v2"

)


type DI interface {

    GetApps() []*Applications

    GetServices() *Services

}


var dmz = []byte(`

applications:

  - name: app1

    type: php

    src: /app1

    host: us

    use: 

      - redis

      - mysql


  - name: app2

    type: rust

    src: /app2

    host: eu

    use: 

      - mongo

      - mysql


  - name: app3

    type: golang

    src: /app3

    host: us

    use: 

      - postgress

      - mysql



services:

  - name: mongo

    type: db

    host: us


  - name: mysql

    type: db

    host: eu


  - name: postgress

    type: db

    host: us


  - name: redis

    type: db

    host: us   

`)


type DMZ struct {

    Applications []*Applications `yaml:"applications,omitempty"`

    Services     []*Services     `yaml:"services,omitempty"`

}


type Applications struct {

    Name string

    Type string

    Src  string `yaml:"src,omitempty"`

    use  []Use  `yaml:"use,omitempty"`

}

type Services struct {

    Name string

    Type string

    Host string `yaml:"host,omitempty"`

}

type Use struct {

    Name string `yaml:"name,omitempty"`

    host string `yaml:"host,omitempty"`

    Type string `yaml:"type,omitempty"`

}


func main() {

    dm := &DMZ{}

    dm.Parse(dmz)

    fmt.Println(dm.getApp("app2"))

    fmt.Println(dm.GetServiceForApp("mongo"))

}


func (dmz *DMZ) GetApps() []*Applications {

    return dmz.Applications

}


func (dmz *DMZ) GetServices() []*Services {

    return dmz.Services

}


func (dmz *DMZ) getApp(appname string) *Applications {

    for _, value := range dmz.Applications {

        if appname == value.Name {

            fmt.Println((*value).Name)

            return value

        }

    }

    return nil

}


func (dmz *DMZ) GetServiceForApp(appname string) *Services {

    for _, value := range dmz.Services {

        if appname == value.Name {

            return value

        }

    }

    return nil

}


// Parse file

func (dmz *DMZ) Parse(yamlContent []byte) {

    if err := yaml.Unmarshal([]byte(yamlContent), &dmz); err != nil {

        log.Fatal("Yaml file is not valid, Error: " + err.Error())

    }

}

筆記:


泛型很方便,但它們的代價是類型系統和運行時的復雜性。我們還沒有找到一種設計,讓價值與復雜性成正比,盡管我們繼續考慮它。同時,Go 的內置映射和切片,加上使用空接口構造容器(顯式拆箱)的能力,意味著在許多情況下,即使不那么流暢,也可以編寫實現泛型能夠實現的功能的代碼。


查看完整回答
反對 回復 2023-04-17
?
阿波羅的戰車

TA貢獻1862條經驗 獲得超6個贊

您可以定義一個接口并在結構中提供實現


type DMZI interface {

    GetApps() []Application

    GetService() []Service

    GetApp(name string) (Application, error)

    GetServiceForApp(name string) ([]string, error)

}


type DMZ struct {

    Application []Application `yaml:"applications,omitempty"`

    Service     []Service     `yaml:"services,omitempty"`

}


func (dmz DMZ) GetApps() []Application {

    return dmz.Application

}


func (dmz DMZ) GetService() []Service {

    return dmz.Service

}


func (dmz DMZ) GetApp(name string) (Application, error) {

    for _, app := range dmz.Application {

        if app.Name == name {

            return app, nil

        }

    }

    return Application{}, fmt.Errorf("Did not find application with name %s", name)

}


func (dmz DMZ) GetServiceForApp(name string) ([]string, error) {

    app, err := dmz.GetApp(name)

    if err != nil {

        return []string{}, err

    }

    return app.Use, nil

}


type Application struct {

    Name string

    Type string

    Src  string   `yaml:"src,omitempty"`

    Use  []string `yaml:"use,omitempty"`

}

type Service struct {

    Name string

    Type string

    Host string `yaml:"host,omitempty"`

}


// Parse file

func Parse(yamlContent []byte) (out DMZI, err error) {

    dmz := DMZ{}

    err = yaml.Unmarshal([]byte(yamlContent), &dmz)

    if err != nil {

        fmt.Println("Yaml file is not valid, Error: " + err.Error())

    }

    return dmz, err

}

因此,您可以調用返回接口上的方法,例如


fmt.Printf("Apps : %+v\n", dmz.GetApps())

fmt.Printf("Service : %+v\n", dmz.GetService())

更新


評論中要求的主要方法


func main() {

    dmz, err := Parse([]byte(ymlStr))

    if err != nil {

        panic(err)

    }

    fmt.Printf("Apps : %+v\n", dmz.GetApps())

    fmt.Printf("Service : %+v\n", dmz.GetService())

}

將打印


Apps : [{Name:app1 Type:php Src:/app1 Use:[redis mysql]} {Name:app2 Type:rust Src:/app2 Use:[mongo mysql]} {Name:app3 Type:golang Src:/app3 Use:[postgress mysql]}]

Service : [{Name:mongo Type:db Host:us} {Name:mysql Type:db Host:eu} {Name:postgress Type:db Host:us} {Name:redis Type:db Host:us}]



查看完整回答
反對 回復 2023-04-17
?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

我已經稍微更改了您的代碼并完成了我認為您所要求的,即每當您創建 DMZ 的新實例時始終調用“Parse”。這就是您使用Dargo執行此操作的方式。查看底部以了解代碼如何將 DMZ 綁定到 ServiceLocator 以及如何獲取 DMZ 的新實例

type DMZ struct {

? ? Applications []*Applications `yaml:"applications,omitempty"`

? ? Services? ? ?[]*Services? ? ?`yaml:"services,omitempty"`

}


// This method is called every time DMZ is created via the Dargo API

func (dmz *DMZ) DargoInitialize(ioc.Descriptor) error {

? ? // get ur bytes from... wherever

? ? Parse(dmz, []byte{})

}


type Applications struct {

? ? Name string

? ? Type string

? ? Src? string `yaml:"src,omitempty"`

? ? use? []Use? `yaml:"use,omitempty"`

}

type Services struct {

? ? Name string

? ? Type string

? ? Host string `yaml:"host,omitempty"`

}

type Use struct {

? ? Name string `yaml:"name,omitempty"`

? ? host string `yaml:"host,omitempty"`

? ? Type string `yaml:"type,omitempty"`

}


// Parse file

func Parse(dmz *DMZ, yamlContent []byte) (out *DMZ, err error) {

? ? err = yaml.Unmarshal([]byte(yamlContent), &dmz)

? ? if err != nil {

? ? ? ? logs.Error("Yaml file is not valid, Error: " + err.Error())

? ? }

? ? return dmz, err

}


func useDargo() error {

? ? locator, err := ioc.CreateAndBind("example", func(binder ioc.Binder) error {

? ? ? ? binder.Bind("DMZ", &DMZ{}).InScope(ioc.PerLookup)

? ? })

? ? if err != nil {

? ? ? ? return err

? ? }


? ? // This is how you would get your instances of DMZ

? ? locator.GetDService("DMZ")

}


查看完整回答
反對 回復 2023-04-17
  • 3 回答
  • 0 關注
  • 183 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號