blob: 1a94844eec2f5c57ff7f5f6ad4431c8f6402b0c3 [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 (
"context"
"encoding/json"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
func (s *GitSubmodule) Resolve(ctx context.Context, repoRoot string) (map[string]any, error) {
res, err := protoToMap(s)
if err != nil {
return nil, err
}
res["url"], err = s.url(ctx, repoRoot)
if err != nil {
return nil, err
}
res["type"] = "submodule"
return res, nil
}
func (c *CIPDEnsureFile) Resolve(ctx context.Context, repoRoot string) (map[string]any, error) {
res, err := protoToMap(c)
if err != nil {
return nil, err
}
res["type"] = "cipd_ensure_file"
return res, nil
}
func (p *JiriProject) Resolve(ctx context.Context, repoRoot string) (map[string]any, error) {
res, err := protoToMap(p)
if err != nil {
return nil, err
}
res["type"] = "jiri_project"
return res, nil
}
func (p *JiriPackages) Resolve(ctx context.Context, repoRoot string) (map[string]any, error) {
res, err := protoToMap(p)
if err != nil {
return nil, err
}
res["type"] = "jiri_packages"
return res, nil
}
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
}