-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(structutil): add protobuf struct util package
- Loading branch information
Showing
2 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package structutil | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"google.golang.org/protobuf/encoding/protojson" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
) | ||
|
||
func BytesToInterface(bytes []byte) (interface{}, error) { | ||
var obj map[string]interface{} | ||
if err := json.Unmarshal(bytes, &obj); err != nil { | ||
if err.Error() == "json: cannot unmarshal array into Go value of type map[string]interface {}" { | ||
var objArray []map[string]interface{} | ||
if err := json.Unmarshal(bytes, &objArray); err != nil { | ||
return nil, err | ||
} | ||
return objArray, nil | ||
} | ||
} | ||
|
||
return obj, nil | ||
} | ||
|
||
func MapToProtobufStruct(m map[string]interface{}) (*structpb.Struct, error) { | ||
b, err := json.Marshal(m) | ||
if err != nil { | ||
return nil, err | ||
} | ||
s := &structpb.Struct{} | ||
err = protojson.Unmarshal(b, s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return s, nil | ||
} | ||
|
||
func ProtobufStructToMap(s *structpb.Struct) (map[string]interface{}, error) { | ||
b, err := protojson.Marshal(s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
m := make(map[string]interface{}) | ||
err = json.Unmarshal(b, &m) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return m, nil | ||
} | ||
|
||
func StructToProtobufStruct(s interface{}) (*structpb.Struct, error) { | ||
return MapToProtobufStruct(s.(map[string]interface{})) | ||
} | ||
|
||
func ProtobufStructToStruct(s *structpb.Struct) (interface{}, error) { | ||
return ProtobufStructToMap(s) | ||
} |