internal: add ToStruct helper.
Change-Id: I2a26553db2ee2705a8a9d8ef5ae09ccca7e04867
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6564
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/internal/internal.go b/internal/internal.go
index 9ff02c8..ec94f5d 100644
--- a/internal/internal.go
+++ b/internal/internal.go
@@ -286,6 +286,24 @@
}
}
+// ToStruct gets the non-preamble declarations of a file and puts them in a
+// struct.
+func ToStruct(f *ast.File) *ast.StructLit {
+ start := 0
+ for i, d := range f.Decls {
+ switch d.(type) {
+ case *ast.Package, *ast.ImportDecl:
+ start = i + 1
+ case *ast.Attribute, *ast.CommentGroup:
+ default:
+ break
+ }
+ }
+ s := ast.NewStruct()
+ s.Elts = f.Decls[start:]
+ return s
+}
+
func IsBulkField(d ast.Decl) bool {
if f, ok := d.(*ast.Field); ok {
if _, ok := f.Label.(*ast.ListLit); ok {