blob: e1aa83a110ba431fadec7301b3b3623c44d7fdcc [file] [log] [blame]
// Copyright 2018 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.
// This file implements output formatting for the cobalt config parser.
package config_parser
import (
"bytes"
"config"
"encoding/base64"
"fmt"
"github.com/golang/protobuf/proto"
)
type OutputFormatter func(c *config.CobaltConfig) (outputBytes []byte, err error)
// Outputs the serialized proto.
func BinaryOutput(c *config.CobaltConfig) (outputBytes []byte, err error) {
return proto.Marshal(c)
}
// Outputs the serialized proto base64 encoded.
func Base64Output(c *config.CobaltConfig) (outputBytes []byte, err error) {
configBytes, err := BinaryOutput(c)
if err != nil {
return outputBytes, err
}
encoder := base64.StdEncoding
outLen := encoder.EncodedLen(len(configBytes))
outputBytes = make([]byte, outLen, outLen)
encoder.Encode(outputBytes, configBytes)
return outputBytes, nil
}
// Returns an output formatter that will output the contents of a C++ header
// file that contains a variable declaration for a string literal that contains
// the base64-encoding of the serialized proto.
//
// varName will be the name of the variable containing the base64-encoded serialized proto.
// namespace is a list of nested namespaces inside of which the variable will be defined.
// configLocation is the location of the YAML that was parsed.
func CppOutputFactory(varName string, namespace []string, configLocation string) OutputFormatter {
return func(c *config.CobaltConfig) (outputBytes []byte, err error) {
b64Bytes, err := Base64Output(c)
if err != nil {
return outputBytes, err
}
out := new(bytes.Buffer)
out.WriteString("// Copyright 2018 The Fuchsia Authors. All rights reserved.\n")
out.WriteString("// Use of this source code is governed by a BSD-style license that can be\n")
out.WriteString("// found in the LICENSE file.\n\n")
out.WriteString("#pragma once\n\n")
out.WriteString("// This file was generated by Cobalt's Config Parser based on the\n")
out.WriteString("// configuration YAML in the following location:\n")
out.WriteString(fmt.Sprintf("// %s\n", configLocation))
out.WriteString("// Edit the YAML at that location to make changes.\n\n")
for _, name := range namespace {
out.WriteString("namespace ")
out.WriteString(name)
out.WriteString(" {\n")
}
out.WriteString("// The base64 encoding of the bytes of a serialized CobaltConfig proto message.\n")
out.WriteString("const char ")
out.WriteString(varName)
out.WriteString("[] = \"")
out.Write(b64Bytes)
out.WriteString("\";\n")
for _, name := range namespace {
out.WriteString("} // ")
out.WriteString(name)
out.WriteString("\n")
}
return out.Bytes(), nil
}
}