♻️ Better grpc map encoder / decoder
This commit is contained in:
		
							
								
								
									
										14
									
								
								pkg/hyper/encode.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								pkg/hyper/encode.go
									
									
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
@@ -6,7 +6,6 @@ import (
 | 
			
		||||
	"reflect"
 | 
			
		||||
 | 
			
		||||
	"git.solsynth.dev/hydrogen/dealer/pkg/proto"
 | 
			
		||||
	jsoniter "github.com/json-iterator/go"
 | 
			
		||||
	"gorm.io/datatypes"
 | 
			
		||||
	"gorm.io/gorm"
 | 
			
		||||
)
 | 
			
		||||
@@ -43,7 +42,7 @@ func LinkRealm(tx *gorm.DB, table string, info *proto.RealmInfo) (BaseRealm, err
 | 
			
		||||
				Banner:       info.Banner,
 | 
			
		||||
				IsPublic:     info.IsPublic,
 | 
			
		||||
				IsCommunity:  info.IsCommunity,
 | 
			
		||||
				AccessPolicy: DecodeAccessPolicy(info.AccessPolicy),
 | 
			
		||||
				AccessPolicy: DecodeMap(info.AccessPolicy),
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			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.IsPublic = info.IsPublic
 | 
			
		||||
	realm.IsCommunity = info.IsCommunity
 | 
			
		||||
	realm.AccessPolicy = DecodeAccessPolicy(info.AccessPolicy)
 | 
			
		||||
	realm.AccessPolicy = DecodeMap(info.AccessPolicy)
 | 
			
		||||
 | 
			
		||||
	var err error
 | 
			
		||||
	if !reflect.DeepEqual(prev, realm) {
 | 
			
		||||
@@ -68,14 +67,3 @@ func LinkRealm(tx *gorm.DB, table string, info *proto.RealmInfo) (BaseRealm, 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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user