github.com/octohelm/wagon@v0.0.0-20240308040401-88662650dc0b/pkg/engine/plan/task/clone_without_null.go (about)

     1  package task
     2  
     3  import (
     4  	"github.com/octohelm/wagon/pkg/engine/plan/task/core"
     5  	"golang.org/x/net/context"
     6  )
     7  
     8  func init() {
     9  	core.DefaultFactory.Register(&CloneWithoutNull{})
    10  }
    11  
    12  type CloneWithoutNull struct {
    13  	core.Task
    14  	Input  any `json:"input"`
    15  	Output any `json:"-" wagon:"generated,name=output"`
    16  }
    17  
    18  func (e *CloneWithoutNull) Do(ctx context.Context) error {
    19  	e.Output = cloneWithoutNull(e.Input)
    20  	return nil
    21  }
    22  
    23  func cloneWithoutNull(v any) any {
    24  	switch x := v.(type) {
    25  	case map[string]any:
    26  		vv := map[string]any{}
    27  		for k := range x {
    28  			if pv := cloneWithoutNull(x[k]); pv != nil {
    29  				vv[k] = pv
    30  			}
    31  		}
    32  		return vv
    33  	case []any:
    34  		s := make([]any, 0, len(x))
    35  		for i := range x {
    36  			if ev := cloneWithoutNull(x[i]); ev != nil {
    37  				s = append(s, ev)
    38  			}
    39  		}
    40  		return s
    41  	}
    42  	return v
    43  }