github.com/zntrio/harp/v2@v2.0.9/pkg/template/engine/internal/codec/decoder.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package codec 19 20 import ( 21 "encoding/json" 22 23 "sigs.k8s.io/yaml" 24 ) 25 26 // FromYAML converts a YAML document into a map[string]interface{}. 27 // 28 // This is not a general-purpose YAML parser, and will not parse all valid 29 // YAML documents. Additionally, because its intended use is within templates 30 // it tolerates errors. It will insert the returned error message string into 31 // m["Error"] in the returned map. 32 func FromYAML(str string) map[string]interface{} { 33 m := map[string]interface{}{} 34 35 if err := yaml.Unmarshal([]byte(str), &m); err != nil { 36 m["Error"] = err.Error() 37 } 38 return m 39 } 40 41 // FromYAMLArray converts a YAML array into a []interface{}. 42 // 43 // This is not a general-purpose YAML parser, and will not parse all valid 44 // YAML documents. Additionally, because its intended use is within templates 45 // it tolerates errors. It will insert the returned error message string as 46 // the first and only item in the returned array. 47 func FromYAMLArray(str string) []interface{} { 48 a := []interface{}{} 49 50 if err := yaml.Unmarshal([]byte(str), &a); err != nil { 51 a = []interface{}{err.Error()} 52 } 53 return a 54 } 55 56 // FromJSON converts a JSON document into a map[string]interface{}. 57 // 58 // This is not a general-purpose JSON parser, and will not parse all valid 59 // JSON documents. Additionally, because its intended use is within templates 60 // it tolerates errors. It will insert the returned error message string into 61 // m["Error"] in the returned map. 62 func FromJSON(str string) map[string]interface{} { 63 m := make(map[string]interface{}) 64 65 if err := json.Unmarshal([]byte(str), &m); err != nil { 66 m["Error"] = err.Error() 67 } 68 return m 69 } 70 71 // FromJSONArray converts a JSON array into a []interface{}. 72 // 73 // This is not a general-purpose JSON parser, and will not parse all valid 74 // JSON documents. Additionally, because its intended use is within templates 75 // it tolerates errors. It will insert the returned error message string as 76 // the first and only item in the returned array. 77 func FromJSONArray(str string) []interface{} { 78 a := []interface{}{} 79 80 if err := json.Unmarshal([]byte(str), &a); err != nil { 81 a = []interface{}{err.Error()} 82 } 83 return a 84 }