Simplifying decision tree when working with map which has two keys and value as a slice

亡梦爱人 提交于 2020-04-17 20:29:26

问题


Can this logic not be simplified more, the code works, but it has become too much of a spaghetti. The requirements are that there should always be one connection if possible in available state, but I need to take all the available states down and re-create them. these paths I have to cover:

  1. if both vifs down, call create
  2. if both vifs available, take them down and up one-by-one
  3. if one down and one available, bring first up the down one, then take down and bring up the available one
  4. if only one down, get it up, then create the second one
  5. if only one up, create the second one, then delete and re-create the first one
var vifsMap = make(map[string][]*directconnect.VirtualInterface)
var vifsStateAvailable, vifsStateDown []*directconnect.VirtualInterface
for _, item := range convertToDirectconnect(lagPair, amz.dcLags) {
    vis, err := amz.dcRead.DescribeVirtualInterfaces(&directconnect.DescribeVirtualInterfacesInput{ConnectionId: item.LagId})
    if err != nil {
        return err
    }

    for _, vi := range vis.VirtualInterfaces {
        if *vi.OwnerAccount == cfg.RemoteAccountID {
            if aws.StringValue(vi.VirtualInterfaceState) == directconnect.VirtualInterfaceStateAvailable {
                vifsStateAvailable = append(vifsStateAvailable, vi)
            } else if aws.StringValue(vi.VirtualInterfaceState) == directconnect.VirtualInterfaceStateDown {
                vifsStateDown = append(vifsStateDown, vi)
            }
        }
    }
}
vifsMap["available"] = vifsStateAvailable
vifsMap["down"] = vifsStateDown

if len(vifsStateDown) > 1 {
    fmt.Println("contains 2 down elements")
} else if len(vifsStateAvailable) > 1 {
    fmt.Println("contains 2 up elements")
} else if len(vifsStateDown) == 1 && len(vifsStateAvailable) == 1 {
    fmt.Println("contains 1 vif down and 1 up")
} else if len(vifsStateAvailable) == 1 && len(vifsStateDown) == 1 {
    fmt.Println("contains 1 vif up and 1 down")
} else if len(vifsStateDown) == 1 && len(vifsStateAvailable) == 0 {
    fmt.Println("contains 1 down only")
} else if len(vifsStateAvailable) == 1 && len(vifsStateDown) == 0 {
    fmt.Println("contains 1 up only")
}

来源:https://stackoverflow.com/questions/61213337/simplifying-decision-tree-when-working-with-map-which-has-two-keys-and-value-as

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!