♻️ Better grpc map encoder / decoder

This commit is contained in:
LittleSheep 2024-10-16 00:57:00 +08:00
parent 4de0c4c969
commit 60e4bbfd97
2 changed files with 16 additions and 14 deletions

14
pkg/hyper/encode.go Normal file
View File

@ -0,0 +1,14 @@
package hyper
import jsoniter "github.com/json-iterator/go"
func EncodeMap(policy map[string]any) []byte {
raw, _ := jsoniter.Marshal(policy)
return raw
}
func DecodeMap(raw []byte) map[string]any {
var out map[string]any
_ = jsoniter.Unmarshal(raw, &out)
return out
}

View File

@ -6,7 +6,6 @@ import (
"reflect" "reflect"
"git.solsynth.dev/hydrogen/dealer/pkg/proto" "git.solsynth.dev/hydrogen/dealer/pkg/proto"
jsoniter "github.com/json-iterator/go"
"gorm.io/datatypes" "gorm.io/datatypes"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -43,7 +42,7 @@ func LinkRealm(tx *gorm.DB, table string, info *proto.RealmInfo) (BaseRealm, err
Banner: info.Banner, Banner: info.Banner,
IsPublic: info.IsPublic, IsPublic: info.IsPublic,
IsCommunity: info.IsCommunity, IsCommunity: info.IsCommunity,
AccessPolicy: DecodeAccessPolicy(info.AccessPolicy), AccessPolicy: DecodeMap(info.AccessPolicy),
} }
return realm, tx.Table(table).Save(&realm).Error return realm, tx.Table(table).Save(&realm).Error
@ -59,7 +58,7 @@ func LinkRealm(tx *gorm.DB, table string, info *proto.RealmInfo) (BaseRealm, err
realm.Banner = info.Banner realm.Banner = info.Banner
realm.IsPublic = info.IsPublic realm.IsPublic = info.IsPublic
realm.IsCommunity = info.IsCommunity realm.IsCommunity = info.IsCommunity
realm.AccessPolicy = DecodeAccessPolicy(info.AccessPolicy) realm.AccessPolicy = DecodeMap(info.AccessPolicy)
var err error var err error
if !reflect.DeepEqual(prev, realm) { if !reflect.DeepEqual(prev, realm) {
@ -68,14 +67,3 @@ func LinkRealm(tx *gorm.DB, table string, info *proto.RealmInfo) (BaseRealm, err
return realm, err return realm, err
} }
func EncodeAccessPolicy(policy map[string]any) []byte {
raw, _ := jsoniter.Marshal(policy)
return raw
}
func DecodeAccessPolicy(raw []byte) map[string]any {
var policy map[string]any
_ = jsoniter.Unmarshal(raw, &policy)
return policy
}