Skip to content

Commit 8a0ac3e

Browse files
committed
internal: add ToStruct helper.
Change-Id: I2a26553db2ee2705a8a9d8ef5ae09ccca7e04867 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6564 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 0ccdee2 commit 8a0ac3e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

internal/internal.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,24 @@ func ToFile(n ast.Node) *ast.File {
286286
}
287287
}
288288

289+
// ToStruct gets the non-preamble declarations of a file and puts them in a
290+
// struct.
291+
func ToStruct(f *ast.File) *ast.StructLit {
292+
start := 0
293+
for i, d := range f.Decls {
294+
switch d.(type) {
295+
case *ast.Package, *ast.ImportDecl:
296+
start = i + 1
297+
case *ast.Attribute, *ast.CommentGroup:
298+
default:
299+
break
300+
}
301+
}
302+
s := ast.NewStruct()
303+
s.Elts = f.Decls[start:]
304+
return s
305+
}
306+
289307
func IsBulkField(d ast.Decl) bool {
290308
if f, ok := d.(*ast.Field); ok {
291309
if _, ok := f.Label.(*ast.ListLit); ok {

0 commit comments

Comments
 (0)