cuelang.org/go@v0.13.0/pkg/encoding/yaml/manual.go (about) 1 // Copyright 2018 The CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package yaml 16 17 import ( 18 "bytes" 19 "io" 20 21 "cuelang.org/go/cue" 22 "cuelang.org/go/cue/ast" 23 "cuelang.org/go/internal/core/adt" 24 cueyaml "cuelang.org/go/internal/encoding/yaml" 25 "cuelang.org/go/internal/pkg" 26 "cuelang.org/go/internal/value" 27 ) 28 29 // Marshal returns the YAML encoding of v. 30 func Marshal(v cue.Value) (string, error) { 31 if err := v.Validate(cue.Concrete(true)); err != nil { 32 return "", err 33 } 34 n := v.Syntax(cue.Final(), cue.Concrete(true)) 35 b, err := cueyaml.Encode(n) 36 return string(b), err 37 } 38 39 // MarshalStream returns the YAML encoding of v. 40 func MarshalStream(v cue.Value) (string, error) { 41 // TODO: return an io.Reader and allow asynchronous processing. 42 iter, err := v.List() 43 if err != nil { 44 return "", err 45 } 46 buf := &bytes.Buffer{} 47 for i := 0; iter.Next(); i++ { 48 if i > 0 { 49 buf.WriteString("---\n") 50 } 51 v := iter.Value() 52 if err := v.Validate(cue.Concrete(true)); err != nil { 53 return "", err 54 } 55 n := v.Syntax(cue.Final(), cue.Concrete(true)) 56 b, err := cueyaml.Encode(n) 57 if err != nil { 58 return "", err 59 } 60 buf.Write(b) 61 } 62 return buf.String(), nil 63 } 64 65 // Unmarshal parses the YAML to a CUE expression. 66 func Unmarshal(data []byte) (ast.Expr, error) { 67 return cueyaml.Unmarshal("", data) 68 } 69 70 // UnmarshalStream parses the YAML to a CUE list expression on success. 71 func UnmarshalStream(data []byte) (ast.Expr, error) { 72 d := cueyaml.NewDecoder("", data) 73 a := []ast.Expr{} 74 for { 75 x, err := d.Decode() 76 if err == io.EOF { 77 break 78 } 79 if err != nil { 80 return nil, err 81 } 82 a = append(a, x) 83 } 84 85 return ast.NewList(a...), nil 86 } 87 88 // Validate validates YAML and confirms it is an instance of schema. 89 // If the YAML source is a stream, every object must match v. 90 func Validate(b []byte, v pkg.Schema) (bool, error) { 91 // This function is left for Go documentation. The package entry calls 92 // cueyaml.Validate directly, passing it the call context. 93 94 ctx := value.OpContext(v) 95 return cueyaml.Validate(ctx, b, v) 96 } 97 98 // validate is the actual implementation of Validate. 99 func validate(c *adt.OpContext, b []byte, v pkg.Schema) (bool, error) { 100 return cueyaml.Validate(c, b, v) 101 } 102 103 // ValidatePartial validates YAML and confirms it matches the constraints 104 // specified by v using unification. This means that b must be consistent with, 105 // but does not have to be an instance of v. If the YAML source is a stream, 106 // every object must match v. 107 func ValidatePartial(b []byte, v pkg.Schema) (bool, error) { 108 // This function is left for Go documentation. The package entry calls 109 // cueyaml.ValidatePartial directly, passing it the call context. 110 111 ctx := value.OpContext(v) 112 return cueyaml.ValidatePartial(ctx, b, v) 113 } 114 115 // validatePartial is the actual implementation of ValidatePartial. 116 func validatePartial(c *adt.OpContext, b []byte, v pkg.Schema) (bool, error) { 117 return cueyaml.ValidatePartial(c, b, v) 118 }