src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

fields.go (766B)


      1 // Copyright 2024 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 package astutil
      6 
      7 import (
      8 	"go/ast"
      9 	"iter"
     10 )
     11 
     12 // FlatFields 'flattens' an ast.FieldList, returning an iterator over each
     13 // (name, field) combination in the list. For unnamed fields, the identifier is
     14 // nil.
     15 func FlatFields(list *ast.FieldList) iter.Seq2[*ast.Ident, *ast.Field] {
     16 	return func(yield func(*ast.Ident, *ast.Field) bool) {
     17 		if list == nil {
     18 			return
     19 		}
     20 
     21 		for _, field := range list.List {
     22 			if len(field.Names) == 0 {
     23 				if !yield(nil, field) {
     24 					return
     25 				}
     26 			} else {
     27 				for _, name := range field.Names {
     28 					if !yield(name, field) {
     29 						return
     30 					}
     31 				}
     32 			}
     33 		}
     34 	}
     35 }