|
| 1 | +/** @file |
| 2 | +
|
| 3 | + Shared remap configuration parsing and marshalling. |
| 4 | +
|
| 5 | + @section license License |
| 6 | +
|
| 7 | + Licensed to the Apache Software Foundation (ASF) under one |
| 8 | + or more contributor license agreements. See the NOTICE file |
| 9 | + distributed with this work for additional information |
| 10 | + regarding copyright ownership. The ASF licenses this file |
| 11 | + to you under the Apache License, Version 2.0 (the |
| 12 | + "License"); you may not use this file except in compliance |
| 13 | + with the License. You may obtain a copy of the License at |
| 14 | +
|
| 15 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | +
|
| 17 | + Unless required by applicable law or agreed to in writing, software |
| 18 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | + See the License for the specific language governing permissions and |
| 21 | + limitations under the License. |
| 22 | +*/ |
| 23 | + |
| 24 | +#pragma once |
| 25 | + |
| 26 | +#include <string> |
| 27 | +#include <string_view> |
| 28 | + |
| 29 | +#include <yaml-cpp/yaml.h> |
| 30 | + |
| 31 | +#include "config/config_result.h" |
| 32 | + |
| 33 | +namespace config |
| 34 | +{ |
| 35 | + |
| 36 | +using RemapConfig = YAML::Node; |
| 37 | + |
| 38 | +/** |
| 39 | + * Parser for remap.yaml / remap.config configuration files. |
| 40 | + * |
| 41 | + * The parser normalizes the legacy remap.config format into the YAML tree shape |
| 42 | + * consumed by the remap.yaml loader so both traffic_server and traffic_ctl can |
| 43 | + * share one conversion path. |
| 44 | + */ |
| 45 | +class RemapParser |
| 46 | +{ |
| 47 | +public: |
| 48 | + /** |
| 49 | + * Parse a remap configuration file. |
| 50 | + * |
| 51 | + * The format is auto-detected based on the filename extension and content. |
| 52 | + * |
| 53 | + * @param[in] filename Path to the configuration file. |
| 54 | + * @return ConfigResult containing the parsed configuration tree or errors. |
| 55 | + */ |
| 56 | + ConfigResult<RemapConfig> parse(std::string const &filename); |
| 57 | + |
| 58 | + /** |
| 59 | + * Parse remap configuration content from a string. |
| 60 | + * |
| 61 | + * The format is auto-detected from @a filename and @a content. |
| 62 | + * |
| 63 | + * @param[in] content The configuration content to parse. |
| 64 | + * @param[in] filename Synthetic filename used for format detection. |
| 65 | + * @return ConfigResult containing the parsed configuration tree or errors. |
| 66 | + */ |
| 67 | + ConfigResult<RemapConfig> parse_content(std::string_view content, std::string const &filename = "remap.yaml"); |
| 68 | + |
| 69 | +private: |
| 70 | + enum class Format { YAML, Legacy }; |
| 71 | + |
| 72 | + Format detect_format(std::string_view content, std::string const &filename) const; |
| 73 | + ConfigResult<RemapConfig> parse_yaml(std::string_view content) const; |
| 74 | + ConfigResult<RemapConfig> parse_legacy(std::string_view content) const; |
| 75 | +}; |
| 76 | + |
| 77 | +/** |
| 78 | + * Marshaller for remap configuration. |
| 79 | + */ |
| 80 | +class RemapMarshaller |
| 81 | +{ |
| 82 | +public: |
| 83 | + /** |
| 84 | + * Serialize configuration to YAML format. |
| 85 | + * |
| 86 | + * @param[in] config The configuration tree to serialize. |
| 87 | + * @return YAML string representation. |
| 88 | + */ |
| 89 | + std::string to_yaml(RemapConfig const &config); |
| 90 | +}; |
| 91 | + |
| 92 | +} // namespace config |
0 commit comments