search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

0 answers
Q&A Persisting custom set data type using GORM golang
Persisting custom set data type using GORM golang
P粉006847750 2025-12-31 09:01:09 Views 57

0

I have created a custom Set Data type in go, which i am using to define one to many relationships. For example in my Schema, i have the following struct definition

type Doctor struct {
  firstName string
  lastName string
  capabilities commons.Set
 }

Here capabilities is a set of strings which have the following values chat, audio, video, with this setup i am trying to persist the above struct into MySQL using the GORM library, but when i do this i get the following error

panic: invalid sql type Set (interface) for mysql

goroutine 6 [running]:
catalog/vendor/github.com/jinzhu/gorm.(*mysql).DataTypeOf(0xc00027e8a0, 0xc00024d680, 0x8, 0x8)
    /home/kumard/go/src/catalog/vendor/github.com/jinzhu/gorm/dialect_mysql.go:123 +0xce9
catalog/vendor/github.com/jinzhu/gorm.(*Scope).createTable(0xc000169400, 0xc14e60)

I know that i have to implement certain methods in order to achieve this, but i am not able to figure out which method/callback to implement here.

ThreadUnsafeSet Definition:

type threadUnsafeSet map[interface{}]struct{}    

type OrderedPair struct {
    First interface{}
    Second interface{}
}    

func newThreadUnsafeSet() threadUnsafeSet {
    return make(threadUnsafeSet)
}    

func (pair *OrderedPair) Equal(other OrderedPair) bool {
    return pair.First == other.First && pair.Second == other.Second
}    

func (set *threadUnsafeSet) Add(i interface{}) bool {
    _, found := (*set)[i]
    if found {
        return false
    }
    (*set)[i] = struct{}{}
    return true
}    

func (set *threadUnsafeSet) Contains(i ...interface{}) bool {
    for _, val := range i {
        if _, ok := (*set)[val]; !ok {
            return false
        }
    }
    return true
}    

func (set *threadUnsafeSet) Cardinality() int {
    return len(*set)
}    

func (set *threadUnsafeSet) Equal(other Set) bool {
    _ = other.(*threadUnsafeSet)
    if set.Cardinality() != other.Cardinality() {
        return false
    }
    for elem := range *set {
        if !other.Contains(elem){
            return false
        }
    }
    return true
}    

func (set *threadUnsafeSet) IsSubSet(other Set) bool {
    _ = other.(*threadUnsafeSet)
    if set.Cardinality() > other.Cardinality() {
        return false
    }
    for elem := range *set {
        if !other.Contains(elem) {
            return false
        }
    }
    return true
}    

func (set *threadUnsafeSet) IsProperSubSet(other Set) bool {
    return set.IsSubSet(other) && !set.Equal(other)
}    

func (set *threadUnsafeSet) IsSuperSet(other Set) bool {
    return other.IsSubSet(set)
}    

func (set *threadUnsafeSet) IsProperSuperSet(other Set) bool {
    return set.IsSuperSet(other) && !set.Equal(other)
}    

func (set *threadUnsafeSet) Union(other Set) Set {
    o := other.(*threadUnsafeSet)
    result := newThreadUnsafeSet()
    for elem := range *set {
        result.Add(elem)
    }
    for elem := range *o {
        result.Add(elem)
    }
    return &result
}    


func (set *threadUnsafeSet) Intersect(other Set) Set {
    o := other.(*threadUnsafeSet)    

    intersection := newThreadUnsafeSet()    

    if set.Cardinality() 

Set Interface definition

type Set interface {
    Add(i interface{}) bool    

    Cardinality() int    

    Clear()    

    Clone() Set    

    Contains(i ...interface{}) bool    

    Difference(other Set) Set    

    Equal(other Set) bool    

    Intersect(other Set) Set    

    IsProperSubSet(other Set) bool    

    IsSubSet(other Set) bool    

    IsSuperSet(other Set) bool    

    Each(func(interface{}) bool)    

    Iter() 
Your Answer
submit

0 answers

Popular tool

vc9-vc14 (32+64 bit) runtime library collection (link below)

vc9-vc14 (32+64 bit) runtime library collection (link below)

Download the collection of runtime libraries required for phpStudy installation

VC9 32-bit

VC9 32-bit

VC9 32-bit phpstudy integrated installation environment runtime library

PHP programmer toolbox full version

PHP programmer toolbox full version

Programmer Toolbox v1.0 PHP Integrated Environment

VC11 32-bit

VC11 32-bit

VC11 32-bit phpstudy integrated installation environment runtime library

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use