mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-14 18:47:01 +02:00
* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License. Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUS-1.1 * Fix test that expected exact offset on hcl file --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Sarah Thompson <sthompson@hashicorp.com> Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
92 lines
1.9 KiB
Go
92 lines
1.9 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package random
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
)
|
|
|
|
// serializableRules is a slice of rules that can be marshalled to JSON in an HCL format
|
|
type serializableRules []Rule
|
|
|
|
// MarshalJSON in an HCL-friendly way
|
|
func (r serializableRules) MarshalJSON() (b []byte, err error) {
|
|
// Example:
|
|
// [
|
|
// {
|
|
// "testrule": [
|
|
// {
|
|
// "string": "teststring",
|
|
// "int": 123
|
|
// }
|
|
// ]
|
|
// },
|
|
// {
|
|
// "charset": [
|
|
// {
|
|
// "charset": "abcde",
|
|
// "min-chars": 2
|
|
// }
|
|
// ]
|
|
// }
|
|
// ]
|
|
data := []map[string][]map[string]interface{}{} // Totally not confusing at all
|
|
for _, rule := range r {
|
|
ruleData := map[string]interface{}{}
|
|
err = mapstructure.Decode(rule, &ruleData)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to decode rule: %w", err)
|
|
}
|
|
|
|
ruleMap := map[string][]map[string]interface{}{
|
|
rule.Type(): {
|
|
ruleData,
|
|
},
|
|
}
|
|
data = append(data, ruleMap)
|
|
}
|
|
|
|
b, err = json.Marshal(data)
|
|
return b, err
|
|
}
|
|
|
|
func (r *serializableRules) UnmarshalJSON(data []byte) (err error) {
|
|
mapData := []map[string]interface{}{}
|
|
err = json.Unmarshal(data, &mapData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rules, err := parseRules(defaultRegistry, mapData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*r = rules
|
|
return nil
|
|
}
|
|
|
|
type runes []rune
|
|
|
|
func (r runes) Len() int { return len(r) }
|
|
func (r runes) Less(i, j int) bool { return r[i] < r[j] }
|
|
func (r runes) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
|
|
|
// MarshalJSON converts the runes to a string for smaller JSON and easier readability
|
|
func (r runes) MarshalJSON() (b []byte, err error) {
|
|
return json.Marshal(string(r))
|
|
}
|
|
|
|
// UnmarshalJSON converts a string to []rune
|
|
func (r *runes) UnmarshalJSON(data []byte) (err error) {
|
|
var str string
|
|
err = json.Unmarshal(data, &str)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*r = []rune(str)
|
|
return nil
|
|
}
|