本文整理匯總了Golang中github.com/ghodss/yaml.YAMLToJSON函數的典型用法代碼示例。如果您正苦於以下問題:Golang YAMLToJSON函數的具體用法?Golang YAMLToJSON怎麽用?Golang YAMLToJSON使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了YAMLToJSON函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: argsToEnv
// argsToEnv uses reflection to convert a map[string]interface to a list
// of environment variables.
func argsToEnv(from map[string]interface{}, to map[string]string) error {
for k, v := range from {
if v == nil {
to[k] = ""
continue
}
t := reflect.TypeOf(v)
vv := reflect.ValueOf(v)
k = "PLUGIN_" + strings.ToUpper(k)
switch t.Kind() {
case reflect.Bool:
to[k] = strconv.FormatBool(vv.Bool())
case reflect.String:
to[k] = vv.String()
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8:
to[k] = fmt.Sprintf("%v", vv.Int())
case reflect.Float32, reflect.Float64:
to[k] = fmt.Sprintf("%v", vv.Float())
case reflect.Map:
yml, _ := yaml.Marshal(vv.Interface())
out, _ := json.YAMLToJSON(yml)
to[k] = string(out)
case reflect.Slice:
out, err := yaml.Marshal(vv.Interface())
if err != nil {
return err
}
in := []string{}
err = yaml.Unmarshal(out, &in)
if err == nil {
to[k] = strings.Join(in, ",")
} else {
out, err = json.YAMLToJSON(out)
if err != nil {
return err
}
to[k] = string(out)
}
}
}
return nil
}
示例2: loadUnversionedIndex
// loadUnversionedIndex loads a pre-Alpha.5 index.yaml file.
//
// This format is deprecated. This function will be removed prior to v2.0.0.
func loadUnversionedIndex(data []byte) (*IndexFile, error) {
fmt.Fprintln(os.Stderr, "WARNING: Deprecated index file format. Try 'helm repo update'")
i := map[string]unversionedEntry{}
// This gets around an error in the YAML parser. Instead of parsing as YAML,
// we convert to JSON, and then decode again.
var err error
data, err = yaml.YAMLToJSON(data)
if err != nil {
return nil, err
}
if err := json.Unmarshal(data, &i); err != nil {
return nil, err
}
if len(i) == 0 {
return nil, ErrNoAPIVersion
}
ni := NewIndexFile()
for n, item := range i {
if item.Chartfile == nil || item.Chartfile.Name == "" {
parts := strings.Split(n, "-")
ver := ""
if len(parts) > 1 {
ver = strings.TrimSuffix(parts[1], ".tgz")
}
item.Chartfile = &chart.Metadata{Name: parts[0], Version: ver}
}
ni.Add(item.Chartfile, item.URL, "", item.Checksum)
}
return ni, nil
}
示例3: ProcessFile
func ProcessFile(file string) ([]byte, error) {
ext := filepath.Ext(file)
isYaml := ext == ".yaml" || ext == ".yml"
isJson := ext == ".json"
if !isYaml && !isJson {
return nil, nil
}
bytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, kerr.Wrap("NMWROTKPLJ", err)
}
if isYaml {
j, err := yaml.YAMLToJSON(bytes)
if err != nil {
return nil, kerr.Wrap("FAFJCYESRH", err)
}
bytes = j
}
return bytes, nil
}
示例4: YamlToJson
func YamlToJson(inputBytes []byte, minify bool) []byte {
buffer, err := yaml.YAMLToJSON(inputBytes)
if err != nil {
fmt.Println("YAML -> JSON convert error.")
fmt.Printf("err: %v\n", err)
os.Exit(1)
}
if minify {
return buffer
} else {
jsonData, err := simplejson.NewJson(buffer)
if err != nil {
fmt.Println("JSON parse error.")
fmt.Printf("err: %v\n", err)
os.Exit(1)
}
prettyBytes, err := jsonData.EncodePretty()
if err != nil {
fmt.Println("JSON encode error.")
fmt.Printf("err: %v\n", err)
os.Exit(1)
}
return prettyBytes
}
}
示例5: handleMeta
// handleMeta extracts data from api:meta markup
// there should only be one api:meta markup tag per project
// if there is more than one, the first tag will be used
func handleMeta(swagDoc *specs.SwagDoc, docs []string) error {
swagDoc.Swagger = "2.0"
y := []byte(docs[0])
j, err := yaml.YAMLToJSON(y)
err = json.Unmarshal(j, swagDoc)
return err
}
示例6: Read
// Read an input configuration file, parsing it (as YAML or JSON)
// into the input 'interface{}', v
func Read(path string, v interface{}, schema string) []serror.SnapError {
// read bytes from file
b, err := cfgReader.ReadFile(path)
if err != nil {
return []serror.SnapError{serror.New(err)}
}
// convert from YAML to JSON (remember, JSON is actually valid YAML)
jb, err := yaml.YAMLToJSON(b)
if err != nil {
return []serror.SnapError{serror.New(fmt.Errorf("error converting YAML to JSON: %v", err))}
}
// validate the resulting JSON against the input the schema
if errors := cfgValidator.validateSchema(schema, string(jb)); errors != nil {
// if invalid, construct (and return?) a SnapError from the errors identified
// during schema validation
return errors
}
// if valid, parse the JSON byte-stream (above)
if parseErr := json.Unmarshal(jb, v); parseErr != nil {
// remove any YAML-specific prefix that might have been added by then
// yaml.Unmarshal() method or JSON-specific prefix that might have been
// added if the resulting JSON string could not be marshalled into our
// input interface correctly (note, if there is no match to either of
// these prefixes then the error message will be passed through unchanged)
tmpErr := strings.TrimPrefix(parseErr.Error(), "error converting YAML to JSON: yaml: ")
errRet := strings.TrimPrefix(tmpErr, "error unmarshaling JSON: json: ")
return []serror.SnapError{serror.New(fmt.Errorf("Error while parsing configuration file: %v", errRet))}
}
return nil
}
示例7: main
func main() {
args := os.Args[1:]
if len(args) == 0 {
fmt.Fprintln(os.Stderr, "missing input YAML file")
return
}
// Load YAML file:
ymlbytes, err := ioutil.ReadFile(args[0])
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
jsonbytes, err := yaml.YAMLToJSON(ymlbytes)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
outName := FileName(args[0]) + ".json"
err = ioutil.WriteFile(outName, jsonbytes, 0644)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
}
示例8: Parsefile
func Parsefile(y []byte) ([]byte, error) {
file, err := yaml.YAMLToJSON(y)
if err != nil {
fmt.Printf("err: %v\n", err)
return nil, err
}
return file, nil
}
示例9: StateMapFromYaml
/*
Load a StateMap from a YAML byte array
*/
func StateMapFromYaml(data []byte) (*StateMap, error) {
j, err := yaml.YAMLToJSON(data)
if err != nil {
return nil, err
}
return StateMapFromJson(j)
}
示例10: yamlToJSON
func yamlToJSON(y []byte) ([]byte, error) {
j, err := yaml.YAMLToJSON(y)
if err != nil {
return nil, fmt.Errorf("yaml to json failed: %v\n%v\n", err, y)
}
return j, nil
}
示例11: unmarshalYAML
// unmarshalYAML re-implements yaml.Unmarshal so that the JSON decoder can have
// UseNumber set.
func unmarshalYAML(y []byte, o interface{}) error {
bs, err := yaml.YAMLToJSON(y)
if err != nil {
return fmt.Errorf("error converting YAML to JSON: %v", err)
}
buf := bytes.NewBuffer(bs)
decoder := util.NewJSONDecoder(buf)
return decoder.Decode(o)
}
示例12: LoadFile
func LoadFile(path string) (*gabs.Container, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.New(color.RedString("Manifest file `%s` not found: %v", path, err))
}
if jsonData, err := yaml.YAMLToJSON(data); err != nil {
return nil, errors.New(color.RedString("Error on parse manifest %s: %v!", path, err))
} else {
return gabs.ParseJSON(jsonData)
}
}
示例13: ToJSON
// ToJSON converts a single YAML document into a JSON document
// or returns an error. If the document appears to be JSON the
// YAML decoding path is not used (so that error messages are)
// JSON specific.
func ToJSON(data []byte, checkValid bool) ([]byte, error) {
if hasJSONPrefix(data) {
if checkValid {
var obj interface{}
if err := json.Unmarshal(data, &obj); err != nil {
return nil, err
}
}
return data, nil
}
return yaml.YAMLToJSON(data)
}
示例14: swagDocToJson
// swagDocToJson converts a swagDoc struct to json and returns a pointer
func swagDocToJson(swagDoc *specs.SwagDoc) (*[]byte, error) {
y, err := yaml.Marshal(swagDoc)
if err != nil {
return nil, err
}
j, err := yaml.YAMLToJSON(y)
if err != nil {
return nil, err
}
return &j, err
}
示例15: str2yaml
func str2yaml() {
y := []byte(`name: jeno
age: 33
food:
- pacal
- rum
`)
j2, err := yaml.YAMLToJSON(y)
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
fmt.Println(string(j2))
}