Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
internal/encoding: support cue file type
Browse files Browse the repository at this point in the history
Change-Id: Ic8a0bb00c5db92079f8738a9b7179bd86f7c49f5
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5093
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Mar 7, 2020
1 parent 88b9bd5 commit 6e8fdd4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
4 changes: 4 additions & 0 deletions cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func parseArgs(cmd *Command, args []string, cfg *load.Config) (p *buildPlan, err
"builds contain two file packages")
}
p.orphanInstance = b
p.encConfig.Stream = true
}

for _, f := range b.OrphanedFiles {
Expand All @@ -205,6 +206,9 @@ func parseArgs(cmd *Command, args []string, cfg *load.Config) (p *buildPlan, err
}
}

if len(p.expressions) > 1 {
p.encConfig.Stream = true
}
return p, nil
}

Expand Down
74 changes: 65 additions & 9 deletions internal/encoding/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ import (
"fmt"
"io"
"os"
"path/filepath"

"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal/filetypes"
"cuelang.org/go/pkg/encoding/yaml"
)

// file.Format
// file.Info

// An Encoder converts CUE to various file formats, including CUE itself.
// An Encoder allows
type Encoder struct {
cfg *Config
closer io.Closer
interpret func(cue.Value) (*ast.File, error)
encFile func(*ast.File) error
encValue func(cue.Value) error
encInst func(*cue.Instance) error
cfg *Config
closer io.Closer
interpret func(cue.Value) (*ast.File, error)
encFile func(*ast.File) error
encValue func(cue.Value) error
autoSimplify bool
}

func (e Encoder) Close() error {
Expand Down Expand Up @@ -79,6 +79,60 @@ func NewEncoder(f *build.File, cfg *Config) (*Encoder, error) {
}

switch f.Encoding {
case build.CUE:
fi, err := filetypes.FromFile(f, cfg.Mode)
if err != nil {
return nil, err
}

synOpts := []cue.Option{}
if !fi.KeepDefaults || !fi.Incomplete {
synOpts = append(synOpts, cue.Final())
}

synOpts = append(synOpts,
cue.Docs(fi.Docs),
cue.Attributes(fi.Attributes),
cue.Optional(fi.Optional),
cue.Concrete(!fi.Incomplete),
cue.Definitions(fi.Definitions),
cue.ResolveReferences(!fi.References),
cue.DisallowCycles(!fi.Cycles),
)

opts := []format.Option{
format.UseSpaces(4),
format.TabIndent(false),
}
opts = append(opts, cfg.Format...)

useSep := false
format := func(name string, n ast.Node) error {
if name != "" && cfg.Stream {
// TODO: make this relative to DIR
fmt.Fprintf(w, "--- %s\n", filepath.Base(name))
} else if useSep {
fmt.Println("---")
}
useSep = true

opts := opts
if e.autoSimplify {
opts = append(opts, format.Simplify())
}

b, err := format.Node(n, opts...)
if err != nil {
return err
}
_, err = w.Write(b)
return err
}
e.encValue = func(v cue.Value) error {
return format("", v.Syntax(synOpts...))
}
e.encFile = func(f *ast.File) error { return format(f.Filename, f) }

case build.JSON, build.JSONL:
// SetEscapeHTML
d := json.NewEncoder(w)
Expand Down Expand Up @@ -126,6 +180,7 @@ func NewEncoder(f *build.File, cfg *Config) (*Encoder, error) {
}

func (e *Encoder) EncodeFile(f *ast.File) error {
e.autoSimplify = false
return e.encodeFile(f, e.interpret)
}

Expand All @@ -134,6 +189,7 @@ func (e *Encoder) EncodeExpr(x ast.Expr) error {
}

func (e *Encoder) Encode(v cue.Value) error {
e.autoSimplify = true
if e.interpret != nil {
f, err := e.interpret(v)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion internal/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/parser"
"cuelang.org/go/encoding/json"
"cuelang.org/go/encoding/protobuf"
"cuelang.org/go/internal/filetypes"
Expand Down Expand Up @@ -101,10 +103,12 @@ type Config struct {
Stdin io.Reader
Stdout io.Writer

Force bool // overwrite existing files.
Force bool // overwrite existing files.
Stream bool // will potentially write more than one document per file

EscapeHTML bool
ProtoPath []string
Format []format.Option
}

// NewDecoder returns a stream of non-rooted data expressions. The encoding
Expand All @@ -129,6 +133,9 @@ func NewDecoder(f *build.File, cfg *Config) *Decoder {

path := f.Filename
switch f.Encoding {
case build.CUE:
i.file, i.err = parser.ParseFile(path, r, parser.ParseComments)
// TODO: verify input format
case build.JSON, build.JSONL:
i.next = json.NewDecoder(nil, path, r).Extract
i.Next()
Expand Down

0 comments on commit 6e8fdd4

Please sign in to comment.