github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/gadget-service/api-helpers/apihelpers.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 apihelpers provides some helper functions for the API package; these were extracted into this package
    16  // to avoid having additional dependencies on the API package itself
    17  package apihelpers
    18  
    19  import (
    20  	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-service/api"
    21  	"github.com/inspektor-gadget/inspektor-gadget/pkg/params"
    22  )
    23  
    24  func ParamDescsToParams(descs params.ParamDescs) (res api.Params) {
    25  	if descs == nil {
    26  		return
    27  	}
    28  	for _, desc := range descs {
    29  		res = append(res, &api.Param{
    30  			Key:            desc.Key,
    31  			Description:    desc.Description,
    32  			DefaultValue:   desc.DefaultValue,
    33  			TypeHint:       string(desc.TypeHint),
    34  			Title:          desc.Title,
    35  			Alias:          desc.Alias,
    36  			Tags:           desc.Tags,
    37  			ValueHint:      string(desc.ValueHint),
    38  			PossibleValues: desc.PossibleValues,
    39  			IsMandatory:    desc.IsMandatory,
    40  		})
    41  	}
    42  	return
    43  }
    44  
    45  func ParamToParamDesc(p *api.Param) *params.ParamDesc {
    46  	return &params.ParamDesc{
    47  		Key:            p.Key,
    48  		Alias:          p.Alias,
    49  		Title:          p.Title,
    50  		DefaultValue:   p.DefaultValue,
    51  		Description:    p.Description,
    52  		IsMandatory:    p.IsMandatory,
    53  		Tags:           p.Tags,
    54  		Validator:      nil,
    55  		TypeHint:       params.TypeHint(p.TypeHint),
    56  		ValueHint:      params.ValueHint(p.ValueHint),
    57  		PossibleValues: p.PossibleValues,
    58  	}
    59  }
    60  
    61  func ToParamDescs(p api.Params) params.ParamDescs {
    62  	res := make(params.ParamDescs, 0, len(p))
    63  	for _, param := range p {
    64  		res = append(res, ParamToParamDesc(param))
    65  	}
    66  	return res
    67  }
    68  
    69  func Validate(p api.Params, v api.ParamValues) error {
    70  	for _, param := range p {
    71  		if v[param.Key] == "" {
    72  			continue
    73  		}
    74  		err := ParamToParamDesc(param).ToParam().Validate(v[param.Key])
    75  		if err != nil {
    76  			return err
    77  		}
    78  	}
    79  	return nil
    80  }