如何使用gocql将Go类型转换为Scylla/Cassandra类型?

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

How to convert a Go type to a Scylla/Cassandra type with gocql?

问题

有没有办法使用gocql将Go类型(如[]string)转换为set<text>?还是我需要自己创建一个映射器来实现?我知道gocql可以使用它们的编组器将Go值转换为Scylla/Cassandra值,但我想获取类型,而不是数据值(gocql返回一个字节数组)。

谢谢!

英文:

Is there a way to convert a Go type like []string to a set&lt;text&gt; using gocql? Or do I need to make my own mapper for that? I know gocql is able to convert a Go value to a Scylla/Cassandra value with their marshaller but I want to get the type, not the data value (gocql returns a byte array).

Thank you!

答案1

得分: 1

我发现gocqltable可以满足我的需求。
gocqltable内置了一个映射器,可以将Go类型转换为Cassandra类型,所以我将type.go文件复制到我的项目中(因为原始文件是未导出的),并按照table.go中的指示使用它。

你可以在这里找到我使用的stringTypeOf方法,并在这里找到它的用法。

英文:

I found that gocqltable does the trick for what I want.
gocqltable has a mapper built in to convert Go types to Cassandra types, so I copied the type.go file into my project (since the original is unexported) and used it as specified in table.go.

You can find the stringTypeOf method I used here, and the usage of it here.

huangapple
  • 本文由 发表于 2021年9月29日 22:47:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/69378655.html
匿名

发表评论

匿名网友

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

确定