How to assign key,value to map[interface{}]interface{}?

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

How to assign key,value to map[interface{}]interface{}?

问题

var cache atomic.Value

func setResToCache(res *utils.InterfaceMap) error {
	resMap := res.ToInterfaceMap()
	val := resMap[constant.key] // constant.key is a constant string
	val, ok := val.(string)
	if !ok {
		return errors.New("类型断言失败")
	}
	someRes := model.someRes{
		Title:    val,
	}
	Cache.Store(someRes)
	return nil
}

关于 utils.InterfaceMap

type InterfaceMap sync.Map

//ToInterfaceMap
func (im *InterfaceMap) ToInterfaceMap() map[interface{}]interface{} {
	iim := make(map[interface{}]interface{})
	m := (*sync.Map)(im)
	m.Range(func(k, v interface{}) bool {
		iim[k] = v
		return true
	})
	return iim
}

我有一些类似上面的代码,当我想编写单元测试时遇到了问题。

{
	name: "test",
	args: args{
		res: &utils.InterfaceMap{
			// 在这里如何分配 k,v
		},
	},
	wantErr: false,
},

如何将键值对分配给 map[interface{}]interface{}

实际上它是 map[string]string,所以我使用断言,但传入的参数是 map[interface{}]interface{}

我想向 map 中添加一些 string: string,以便能够成功测试代码。

英文:
var cache atomic.Value

func setResToCache(res *utils.InterfaceMap) error {
	resMap := res.ToInterfaceMap()
	val := resMap[constant.key] // constant.key is a constant string
	val, ok := val.(string)
	if !ok {
		return errors.New("type assertion failed")
	}
	someRes := model.someRes{
		Title:    val,
	}
	Cache.Store(someRes)
	return nil
}

about utils.InterfaceMap

type InterfaceMap sync.Map

//ToInterfaceMap
func (im *InterfaceMap) ToInterfaceMap() map[interface{}]interface{} {
	iim := make(map[interface{}]interface{})
	m := (*sync.Map)(im)
	m.Range(func(k, v interface{}) bool {
		iim[k] = v
		return true
	})
	return iim
}

I have some code similar to the above, and I have a problem when I want to write a unit test.

	{
		name: "test",
		args: args{
			res: &utils.InterfaceMap{
				// How to assign k,v here
			},
		},
		wantErr: false,
	},

How to how to assign key,value to map[interface{}]interface{}?

It is actually map[string]string, so I use assert, but the parameter passed in is map[interface{}]interface{}.

I want to add some string: string to map so that I can successfully test the code.

答案1

得分: 2

在你的测试文件中创建一个简单的函数,用于输入map[string]string。在函数内部,将这些值写入sync.Map,并将其转换为InterfaceMap类型,然后返回引用。示例如下:

func createMockInterfaceMap(in map[string]string) *utils.InterfaceMap {
	mockMap := sync.Map{}
	for key, value := range in {
		mockMap.Store(key, value)
	}

	iMap := utils.InterfaceMap(mockMap)

	return &iMap
}
{
	name: "test",
	args: args{
		res: createMockInterfaceMap(map[string]string{
			`key1`: `value1`,
			`key2`: `value2`,
		}),
	},
	wantErr: false,
},

请注意,这只是一个示例代码片段,具体实现可能需要根据你的需求进行调整。

英文:

Create simple function in your test file to input map[string]string and inside the function, write those values to sync.Map and cast it to InterfaceMap type and return the reference. Example is as below.

func createMockInterfaceMap(in map[string]string) *utils.InterfaceMap{
	mockMap := sync.Map{}
	for key, value := range in {
		mockMap.Store(key, value)
	}

	iMap := utils.InterfaceMap(mockMap)

	return &iMap
}
{
	name: "test",
	args: args{
		res: createMockInterfaceMap(map[string]string{
			`key1`:`value1`,
			`key2`:`value2`,
		}),
	},
	wantErr: false,
},

答案2

得分: 1

要将map[string]string转换为map[interface{}]interface{},可以使用以下代码:

func MapConvert(mss map[string]string) map[interface{}]interface{} {
	ifaceMap := map[interface{}]interface{}{}
	for k, v := range mss {
		ifaceMap[k] = v
	}
	return ifaceMap
}

如果要使用类型定义,可以使用以下代码:

type InterfaceMap map[interface{}]interface{}

func MapConvert(mss map[string]string) *InterfaceMap {
	ifaceMap := InterfaceMap{}
	for k, v := range mss {
		ifaceMap[k] = v
	}
	return &ifaceMap
}

你可以在以下链接中找到一个完整的运行示例:

https://play.golang.org/p/UPKqqZnFis-

英文:

To pass in a map[interface{}]interface{}, it will need to be defined as such.

You can convert a map[string]string to a map[interface{}]interface{} using something like the following:

func MapConvert(mss map[string]string) map[interface{}]interface{} {
	ifaceMap := map[interface{}]interface{}{}
	for k, v := range mss {
		ifaceMap[k] = v
	}
	return ifaceMap
}

Here's an example using a type definition:

type InterfaceMap map[interface{}]interface{}

func MapConvert(mss map[string]string) *InterfaceMap {
	ifaceMap := InterfaceMap{}
	for k, v := range mss {
		ifaceMap[k] = v
	}
	return &ifaceMap
}

Here's a full running example:

https://play.golang.org/p/UPKqqZnFis-

huangapple
  • 本文由 发表于 2021年6月15日 10:53:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/67979403.html
匿名

发表评论

匿名网友

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

确定