This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils/proto: consolidate proto helpers
Consolidates helpers into a separate file in the utils pkg for reusability. The proto marshalling to YAML will be required in a subsequent change that builds the envoy bootstrap config in a generic way for code reuse between the injector and OSM gateway. Signed-off-by: Shashank Ram <[email protected]>
- Loading branch information
1 parent
4c390a5
commit ee23bc0
Showing
3 changed files
with
50 additions
and
42 deletions.
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
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,44 @@ | ||
package utils | ||
|
||
import ( | ||
"google.golang.org/protobuf/encoding/protojson" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
yaml "gopkg.in/yaml.v2" | ||
) | ||
|
||
// ProtoToYAML converts a Proto message to it's YAML representation in bytes | ||
func ProtoToYAML(m protoreflect.ProtoMessage) ([]byte, error) { | ||
marshalOptions := protojson.MarshalOptions{ | ||
UseProtoNames: true, | ||
} | ||
configJSON, err := marshalOptions.Marshal(m) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
configYAML, err := jsonToYAML(configJSON) | ||
if err != nil { | ||
log.Error().Err(err).Msgf("Error marshaling xDS struct into YAML") | ||
return nil, err | ||
} | ||
return configYAML, err | ||
} | ||
|
||
// jsonToYAML converts a JSON representation in bytes to the corresponding YAML representation in bytes | ||
// Reference impl taken from https://github.com/ghodss/yaml/blob/master/yaml.go#L87 | ||
func jsonToYAML(jb []byte) ([]byte, error) { | ||
// Convert the JSON to an object. | ||
var jsonObj interface{} | ||
// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the | ||
// Go JSON library doesn't try to pick the right number type (int, float, | ||
// etc.) when unmarshalling to interface{}, it just picks float64 | ||
// universally. go-yaml does go through the effort of picking the right | ||
// number type, so we can preserve number type throughout this process. | ||
err := yaml.Unmarshal([]byte(jb), &jsonObj) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Marshal this object into YAML. | ||
return yaml.Marshal(jsonObj) | ||
} |