...

Source file src/cuelang.org/go/cue/ast/astutil/file.go

Documentation: cuelang.org/go/cue/ast/astutil

     1  // Copyright 2020 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 astutil
    16  
    17  import (
    18  	"cuelang.org/go/cue/ast"
    19  	"cuelang.org/go/cue/token"
    20  )
    21  
    22  // ToFile converts an expression to a File. It will create an import section for
    23  // any of the identifiers in x that refer to an import and will unshadow
    24  // references as appropriate.
    25  func ToFile(x ast.Expr) (*ast.File, error) {
    26  	var f *ast.File
    27  	if st, ok := x.(*ast.StructLit); ok {
    28  		f = &ast.File{Decls: st.Elts}
    29  	} else {
    30  		ast.SetRelPos(x, token.NoSpace)
    31  		f = &ast.File{Decls: []ast.Decl{&ast.EmbedDecl{Expr: x}}}
    32  	}
    33  
    34  	if err := Sanitize(f); err != nil {
    35  		return nil, err
    36  	}
    37  	return f, nil
    38  }
    39  

View as plain text