github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/datasource/field.go (about)

     1  // Copyright 2024 The Inspektor Gadget 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 datasource
    16  
    17  import (
    18  	"maps"
    19  
    20  	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-service/api"
    21  )
    22  
    23  type FieldFlag uint32
    24  
    25  const (
    26  	// FieldFlagEmpty means the field cannot have a value
    27  	FieldFlagEmpty FieldFlag = 1 << iota
    28  
    29  	// FieldFlagContainer means that the field is statically sized and can have multiple statically sized members;
    30  	// AddStaticFields() will return the container for all given fields, and it is assumed, that the creator will
    31  	// always assign the full container using Set()
    32  	FieldFlagContainer
    33  
    34  	// FieldFlagHidden sets a field to invisible
    35  	FieldFlagHidden
    36  
    37  	// FieldFlagHasParent means that the field is not directly attached to the root of DataSource, but instead to
    38  	// another field that is referenced to in the Parent field
    39  	FieldFlagHasParent
    40  
    41  	// FieldFlagStaticMember means that the field is part of a container and is statically sized
    42  	FieldFlagStaticMember
    43  
    44  	// FieldFlagUnreferenced means that a field is no longer referenced by its name in the DataSource
    45  	FieldFlagUnreferenced
    46  )
    47  
    48  func (f FieldFlag) Uint32() uint32 {
    49  	return uint32(f)
    50  }
    51  
    52  func (f FieldFlag) In(of uint32) bool {
    53  	return of&uint32(f) != 0
    54  }
    55  
    56  func (f FieldFlag) AddTo(of *uint32) {
    57  	*of |= uint32(f)
    58  }
    59  
    60  func (f FieldFlag) RemoveFrom(of *uint32) {
    61  	*of &^= uint32(f)
    62  }
    63  
    64  type Field interface {
    65  	FieldName() string
    66  }
    67  
    68  type StaticField interface {
    69  	Field
    70  	FieldSize() uint32
    71  	FieldOffset() uint32
    72  }
    73  
    74  type AnnotatedField interface {
    75  	FieldAnnotations() map[string]string
    76  }
    77  
    78  type TaggedField interface {
    79  	FieldTags() []string
    80  }
    81  
    82  type TypedField interface {
    83  	FieldType() api.Kind
    84  }
    85  
    86  type FlaggedField interface {
    87  	FieldFlags() FieldFlag
    88  }
    89  
    90  type ParentedField interface {
    91  	// FieldParent should return an index to the parent of the field, -1 for no parent
    92  	FieldParent() int
    93  }
    94  
    95  type FieldOption func(*field)
    96  
    97  func WithKind(kind api.Kind) FieldOption {
    98  	return func(f *field) {
    99  		f.Kind = kind
   100  	}
   101  }
   102  
   103  func WithTags(tags ...string) FieldOption {
   104  	return func(f *field) {
   105  		f.Tags = append(f.Tags, tags...)
   106  	}
   107  }
   108  
   109  func WithFlags(flags FieldFlag) FieldOption {
   110  	return func(f *field) {
   111  		f.Flags |= uint32(flags)
   112  	}
   113  }
   114  
   115  func WithAnnotations(annotations map[string]string) FieldOption {
   116  	return func(f *field) {
   117  		f.Annotations = maps.Clone(annotations)
   118  	}
   119  }
   120  
   121  func WithOrder(order int32) FieldOption {
   122  	return func(f *field) {
   123  		f.Order = order
   124  	}
   125  }