github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/plugins/shared/structs/util.go (about)

     1  package structs
     2  
     3  import (
     4  	"github.com/golang/protobuf/ptypes/wrappers"
     5  	"github.com/hashicorp/nomad/helper"
     6  	"github.com/hashicorp/nomad/plugins/shared/structs/proto"
     7  )
     8  
     9  func ConvertProtoAttribute(in *proto.Attribute) *Attribute {
    10  	out := &Attribute{
    11  		Unit: in.Unit,
    12  	}
    13  
    14  	switch in.Value.(type) {
    15  	case *proto.Attribute_BoolVal:
    16  		out.Bool = helper.BoolToPtr(in.GetBoolVal())
    17  	case *proto.Attribute_FloatVal:
    18  		out.Float = helper.Float64ToPtr(in.GetFloatVal())
    19  	case *proto.Attribute_IntVal:
    20  		out.Int = helper.Int64ToPtr(in.GetIntVal())
    21  	case *proto.Attribute_StringVal:
    22  		out.String = helper.StringToPtr(in.GetStringVal())
    23  	default:
    24  	}
    25  
    26  	return out
    27  }
    28  
    29  func ConvertProtoAttributeMap(in map[string]*proto.Attribute) map[string]*Attribute {
    30  	if in == nil {
    31  		return nil
    32  	}
    33  
    34  	out := make(map[string]*Attribute, len(in))
    35  	for k, a := range in {
    36  		out[k] = ConvertProtoAttribute(a)
    37  	}
    38  
    39  	return out
    40  }
    41  
    42  func ConvertStructsAttribute(in *Attribute) *proto.Attribute {
    43  	out := &proto.Attribute{
    44  		Unit: in.Unit,
    45  	}
    46  
    47  	if in.Int != nil {
    48  		out.Value = &proto.Attribute_IntVal{
    49  			IntVal: *in.Int,
    50  		}
    51  	} else if in.Float != nil {
    52  		out.Value = &proto.Attribute_FloatVal{
    53  			FloatVal: *in.Float,
    54  		}
    55  	} else if in.String != nil {
    56  		out.Value = &proto.Attribute_StringVal{
    57  			StringVal: *in.String,
    58  		}
    59  	} else if in.Bool != nil {
    60  		out.Value = &proto.Attribute_BoolVal{
    61  			BoolVal: *in.Bool,
    62  		}
    63  	}
    64  
    65  	return out
    66  }
    67  
    68  func ConvertStructAttributeMap(in map[string]*Attribute) map[string]*proto.Attribute {
    69  	if in == nil {
    70  		return nil
    71  	}
    72  
    73  	out := make(map[string]*proto.Attribute, len(in))
    74  	for k, a := range in {
    75  		out[k] = ConvertStructsAttribute(a)
    76  	}
    77  
    78  	return out
    79  }
    80  
    81  func Pow(a, b int64) int64 {
    82  	var p int64 = 1
    83  	for b > 0 {
    84  		if b&1 != 0 {
    85  			p *= a
    86  		}
    87  		b >>= 1
    88  		a *= a
    89  	}
    90  	return p
    91  }
    92  
    93  // CopyMapStringAttribute copies a map of string to Attribute
    94  func CopyMapStringAttribute(in map[string]*Attribute) map[string]*Attribute {
    95  	if in == nil {
    96  		return nil
    97  	}
    98  
    99  	out := make(map[string]*Attribute, len(in))
   100  	for k, v := range in {
   101  		out[k] = v.Copy()
   102  	}
   103  	return out
   104  }
   105  
   106  // ConvertProtoStatObject converts between a proto and struct StatObject
   107  func ConvertProtoStatObject(in *proto.StatObject) *StatObject {
   108  	if in == nil {
   109  		return nil
   110  	}
   111  
   112  	out := &StatObject{
   113  		Nested:     make(map[string]*StatObject, len(in.Nested)),
   114  		Attributes: make(map[string]*StatValue, len(in.Attributes)),
   115  	}
   116  
   117  	for k, v := range in.Nested {
   118  		out.Nested[k] = ConvertProtoStatObject(v)
   119  	}
   120  
   121  	for k, v := range in.Attributes {
   122  		out.Attributes[k] = ConvertProtoStatValue(v)
   123  	}
   124  
   125  	return out
   126  }
   127  
   128  // ConvertProtoStatValue converts between a proto and struct StatValue
   129  func ConvertProtoStatValue(in *proto.StatValue) *StatValue {
   130  	if in == nil {
   131  		return nil
   132  	}
   133  
   134  	return &StatValue{
   135  		FloatNumeratorVal:   unwrapDouble(in.FloatNumeratorVal),
   136  		FloatDenominatorVal: unwrapDouble(in.FloatDenominatorVal),
   137  		IntNumeratorVal:     unwrapInt64(in.IntNumeratorVal),
   138  		IntDenominatorVal:   unwrapInt64(in.IntDenominatorVal),
   139  		StringVal:           unwrapString(in.StringVal),
   140  		BoolVal:             unwrapBool(in.BoolVal),
   141  		Unit:                in.Unit,
   142  		Desc:                in.Desc,
   143  	}
   144  }
   145  
   146  // ConvertStructStatObject converts between a struct and proto StatObject
   147  func ConvertStructStatObject(in *StatObject) *proto.StatObject {
   148  	if in == nil {
   149  		return nil
   150  	}
   151  
   152  	out := &proto.StatObject{
   153  		Nested:     make(map[string]*proto.StatObject, len(in.Nested)),
   154  		Attributes: make(map[string]*proto.StatValue, len(in.Attributes)),
   155  	}
   156  
   157  	for k, v := range in.Nested {
   158  		out.Nested[k] = ConvertStructStatObject(v)
   159  	}
   160  
   161  	for k, v := range in.Attributes {
   162  		out.Attributes[k] = ConvertStructStatValue(v)
   163  	}
   164  
   165  	return out
   166  }
   167  
   168  // ConvertStructStatValue converts between a struct and proto StatValue
   169  func ConvertStructStatValue(in *StatValue) *proto.StatValue {
   170  	if in == nil {
   171  		return nil
   172  	}
   173  
   174  	return &proto.StatValue{
   175  		FloatNumeratorVal:   wrapDouble(in.FloatNumeratorVal),
   176  		FloatDenominatorVal: wrapDouble(in.FloatDenominatorVal),
   177  		IntNumeratorVal:     wrapInt64(in.IntNumeratorVal),
   178  		IntDenominatorVal:   wrapInt64(in.IntDenominatorVal),
   179  		StringVal:           wrapString(in.StringVal),
   180  		BoolVal:             wrapBool(in.BoolVal),
   181  		Unit:                in.Unit,
   182  		Desc:                in.Desc,
   183  	}
   184  }
   185  
   186  // Helper functions for proto wrapping
   187  
   188  func unwrapDouble(w *wrappers.DoubleValue) *float64 {
   189  	if w == nil {
   190  		return nil
   191  	}
   192  
   193  	v := w.Value
   194  	return &v
   195  }
   196  
   197  func wrapDouble(v *float64) *wrappers.DoubleValue {
   198  	if v == nil {
   199  		return nil
   200  	}
   201  
   202  	return &wrappers.DoubleValue{Value: *v}
   203  }
   204  
   205  func unwrapInt64(w *wrappers.Int64Value) *int64 {
   206  	if w == nil {
   207  		return nil
   208  	}
   209  
   210  	v := w.Value
   211  	return &v
   212  }
   213  
   214  func wrapInt64(v *int64) *wrappers.Int64Value {
   215  	if v == nil {
   216  		return nil
   217  	}
   218  
   219  	return &wrappers.Int64Value{Value: *v}
   220  }
   221  
   222  func unwrapString(w *wrappers.StringValue) *string {
   223  	if w == nil {
   224  		return nil
   225  	}
   226  
   227  	v := w.Value
   228  	return &v
   229  }
   230  
   231  func wrapString(v *string) *wrappers.StringValue {
   232  	if v == nil {
   233  		return nil
   234  	}
   235  
   236  	return &wrappers.StringValue{Value: *v}
   237  }
   238  
   239  func unwrapBool(w *wrappers.BoolValue) *bool {
   240  	if w == nil {
   241  		return nil
   242  	}
   243  
   244  	v := w.Value
   245  	return &v
   246  }
   247  
   248  func wrapBool(v *bool) *wrappers.BoolValue {
   249  	if v == nil {
   250  		return nil
   251  	}
   252  
   253  	return &wrappers.BoolValue{Value: *v}
   254  }