在Go语言中,可以将map存储到变量中并进行断言操作。

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

golang storing map to variable and do asserts

问题

我正在尝试使用Avi Go SDK来获取gslb配置。

首先,按照以下方式初始化与控制器的连接:

aviClient, err := clients.NewAviClient("mycontroller", "USER",
	session.SetPassword("PASSS"),
	session.SetTenant("TENANT"),
	session.SetInsecure)
if err != nil {
	t.Error(err)
}

然后,按照以下方式获取gslb的详细信息:

// 获取gslb
var obj interface{}
err = aviClient.AviSession.GetObjectByName("gslbservice", "mygslbname", &obj)
fmt.Printf("%v\n", obj)

这将打印如下的详细信息:

map[_last_modified:1650419741251661 controller_health_status_enabled:true created_by:null description:null domain_names:[xxxxxxxxxxxxx] down_response:map[type:GSLB_SERVICE_DOWN_RESPONSE_ALL_RECORDS] enabled:true groups:[map[algorithm:GSLB_ALGORITHM_ROUND_ROBIN members:[map[cluster_uuid:xxxxxxxxxxxxxxxxxxxx enabled:true fqdn:xxxxxxxxxxxxxxxxxxxxxxxxx ip:map[addr:xxxxxxxxxxxxxxx type:V4] location:map[source:GSLB_LOCATION_SRC_INHERIT_FROM_SITE] ratio:1]] name:xxxxxxxxxxxxxxxxxxxxxxx priority:10]] health_monitor_refs:[https://xxxxxxxxxxxxxxxx] health_monitor_scope:GSLB_SERVICE_HEALTH_MONITOR_ALL_MEMBERS name:xxxxxxxxxxxxxxxx num_dns_ip:1 tenant_ref:https://xxxxxxxxxxxxxxxxxxx ttl:30 url:https://xxxxxxxxxxxxxxxxxxxx use_edns_client_subnet:true uuid:xxxxxxxxxxxxxxxxxxx wildcard_match:false]

现在,我想将整个map存储到变量中,以便我可以进行断言和测试,如下所示:

var obj1 interface{}	
appDeployment := aviClient.AviSession.GetObjectByName("gslbservice", "mygslbname", &obj1)
if err != nil {
	t.Error(err)
}
assert.Equal(t, appDeployment.domain_names, "mygslbname")

我得到以下错误:

appDeployment.domain_names undefined (type error has no field or method domain_names)

需要帮助吗?或者如何从map中打印任何值,例如如何从上述map中打印ttl值?

英文:

I am trying to use Avi Go SDK to fetch a gslb configuration.

First initilizing a connection to the controller as below,

aviClient, err := clients.NewAviClient("mycontroller", "USER",
	session.SetPassword("PASSS"),
	session.SetTenant("TENANT"),
	session.SetInsecure)
if err != nil {
	t.Error(err)
}

Then trying to grab a gslb details as below,

//Fetch gslb
var obj interface{}
err = aviClient.AviSession.GetObjectByName("gslbservice", "mygslbname", &obj)
fmt.Printf("%v\n", obj)

and that print the details as below,

map[_last_modified:1650419741251661 controller_health_status_enabled:true created_by:null description:null domain_names:[xxxxxxxxxxxxx] down_response:map[type:GSLB_SERVICE_DOWN_RESPONSE_ALL_RECORDS] enabled:true groups:[map[algorithm:GSLB_ALGORITHM_ROUND_ROBIN members:[map[cluster_uuid:xxxxxxxxxxxxxxxxxxxx enabled:true fqdn:xxxxxxxxxxxxxxxxxxxxxxxxx ip:map[addr:xxxxxxxxxxxxxxx type:V4] location:map[source:GSLB_LOCATION_SRC_INHERIT_FROM_SITE] ratio:1]] name:xxxxxxxxxxxxxxxxxxxxxxx priority:10]] health_monitor_refs:[https://xxxxxxxxxxxxxxxx] health_monitor_scope:GSLB_SERVICE_HEALTH_MONITOR_ALL_MEMBERS name:xxxxxxxxxxxxxxxx num_dns_ip:1 tenant_ref:https://xxxxxxxxxxxxxxxxxxx ttl:30 url:https://xxxxxxxxxxxxxxxxxxxx use_edns_client_subnet:true uuid:xxxxxxxxxxxxxxxxxxx wildcard_match:false]

Now I would like to store that whole map to variable so that I can do asserts and test as below,

var obj1 interface{}	
appDeployment := aviClient.AviSession.GetObjectByName("gslbservice", "mygslbname", &obj1)
if err != nil {
	t.Error(err)
}
assert.Equal(t, appDeployment.domain_names, "mygslbname")

I get below error,

appDeployment.domain_names undefined (type error has no field or method domain_names)

Any assitance?
Or How do print any values from the map, for eg how to print ttl value from above map?

答案1

得分: 1

你有两个问题:

  1. GetObjectByName 返回了一个错误。所以,你的 appDeployment 实际上是一个错误。
  2. 你必须通过字符串键访问映射。所以,即使 appDeployment 是一个有效的映射,你也必须通过字符串键访问 domain_names

以下代码应该解决这两个问题:

var obj1 map[string]interface{}

err = aviClient.AviSession.GetObjectByName("gslbservice", "mygslbname", &obj1)
if err != nil {
    t.Error(err)
}
assert.Equal(t, obj1["domain_names"], "mygslbname")
英文:

You have two problems:

  1. GetObjectByName returns error.
    So, your appDeployment is actually an error
  2. You have to access the map by string keys. So, even if appDeployment were a valid map, you would have to access domain_names by string key.

This should solve both problems:

    var obj1 map[string]interface{}

    err = aviClient.AviSession.GetObjectByName("gslbservice", "mygslbname", &obj1)
    if err != nil {
        t.Error(err)
    }
    assert.Equal(t, obj1["domain_names"], "mygslbname")

答案2

得分: 0

是因为aviClient.AviSession.GetObjectByName返回了一个错误吗?你是不是想要写入obj1

英文:

Is it because aviClient.AviSession.GetObjectByName is returning an error? Did you mean to write to obj1?

huangapple
  • 本文由 发表于 2022年4月21日 11:56:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/71948770.html
匿名

发表评论

匿名网友

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

确定