blob: 9eca643ed19cc0b72e98e92b800b0f0bcf2d8cab [file] [log] [blame]
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package proto
import (
"encoding/json"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
// ProtoToMap converts a proto message to a Go map.
func ProtoToMap(m proto.Message) (map[string]any, error) {
opts := protojson.MarshalOptions{
UseProtoNames: true,
EmitUnpopulated: true,
}
b, err := opts.Marshal(m)
if err != nil {
return nil, err
}
res := make(map[string]any)
if err := json.Unmarshal(b, &res); err != nil {
return nil, err
}
return res, nil
}