File: variable.go

package info (click to toggle)
golang-github-mozilla-scribe 0.0~git20220110.3fd4271-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 320 kB
  • sloc: makefile: 33
file content (30 lines) | stat: -rw-r--r-- 801 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor:
// - Aaron Meihm ameihm@mozilla.com

package scribe

import (
	"regexp"
)

// Variable defines variables that can be included in the policy document.
// Variables are expanded in objects at runtime.
type Variable struct {
	Key   string `json:"key" yaml:"key"`
	Value string `json:"value" yaml:"value"`
}

func variableExpansion(v []Variable, in string) string {
	res := in
	for _, x := range v {
		s := "\\$\\{" + x.Key + "\\}"
		re := regexp.MustCompile(s)
		res = re.ReplaceAllLiteralString(res, x.Value)
	}
	debugPrint("variableExpansion(): %v -> %v\n", in, res)
	return res
}