Skip to content

Commit

Permalink
enhanced value style & bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFireMike committed Jan 18, 2021
1 parent 9730c17 commit a327831
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 35 deletions.
2 changes: 1 addition & 1 deletion core/communicator/device_class_communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ func (o *deviceClassCommunicator) getValuesBySNMPWalk(ctx context.Context, oids
return nil, errors.Wrap(err, "couldn't get value from response response")
}
if res != "" {
resNormalized, err := oid.operators.apply(ctx, value.Value(res))
resNormalized, err := oid.operators.apply(ctx, value.New(res))
if err != nil {
log.Ctx(ctx).Trace().Err(err).Msg("response couldn't be normalized")
return nil, errors.Wrap(err, "response couldn't be normalized")
Expand Down
28 changes: 14 additions & 14 deletions core/communicator/device_class_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type filterOperatorAdapter struct {
func (o *filterOperatorAdapter) operate(ctx context.Context, v value.Value) (value.Value, error) {
err := o.operator.filter(ctx, v)
if err != nil {
return "", err
return value.Empty(), err
}
return v, err
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func (o *propertyOperators) apply(ctx context.Context, v value.Value) (value.Val
if operator.returnOnError() {
return v, nil
}
return "", errors.Wrap(err, "operator failed")
return value.Empty(), errors.Wrap(err, "operator failed")
}
v = x
}
Expand All @@ -106,7 +106,7 @@ type multiplyNumberModifier struct {
func (m *multiplyNumberModifier) modify(_ context.Context, v value.Value) (value.Value, error) {
a, err := decimal.NewFromString(v.String())
if err != nil {
return "", errors.New("cannot covert current value to decimal number")
return value.Empty(), errors.New("cannot covert current value to decimal number")
}
b := decimal.NewFromFloat(m.value)
result := a.Mul(b)
Expand Down Expand Up @@ -159,15 +159,15 @@ type addSuffixModifier struct {
}

func (a *addSuffixModifier) modify(_ context.Context, v value.Value) (value.Value, error) {
return v + value.New(a.suffix), nil
return value.New(v.String() + a.suffix), nil
}

type addPrefixModifier struct {
prefix string
}

func (a *addPrefixModifier) modify(_ context.Context, v value.Value) (value.Value, error) {
return value.New(a.prefix) + v, nil
return value.New(a.prefix + v.String()), nil
}

type regexSubmatchModifier struct {
Expand All @@ -189,7 +189,7 @@ func newRegexSubmatchModifier(regex string, format string) (*regexSubmatchModifi
func (o *regexSubmatchModifier) modify(_ context.Context, v value.Value) (value.Value, error) {
subMatches := o.regex.FindStringSubmatch(v.String())
if subMatches == nil {
return "", errors.New("regex does not match")
return value.Empty(), errors.New("regex does not match")
}
return value.New(o.regex.ReplaceAllString(subMatches[0], o.format)), nil
}
Expand All @@ -211,7 +211,7 @@ func newRegexReplaceModifier(regex, replace string) (*regexReplaceModifier, erro
}

func (r *regexReplaceModifier) modify(_ context.Context, v value.Value) (value.Value, error) {
return value.New(r.regex.ReplaceAllString(string(v), r.replace)), nil
return value.New(r.regex.ReplaceAllString(v.String(), r.replace)), nil
}

type insertReadValueModifier struct {
Expand All @@ -222,7 +222,7 @@ type insertReadValueModifier struct {
func (r *insertReadValueModifier) modify(ctx context.Context, v value.Value) (value.Value, error) {
readValue, err := r.readValueReader.getProperty(ctx)
if err != nil {
return "", errors.Wrap(err, "failed to read out value")
return value.Empty(), errors.Wrap(err, "failed to read out value")
}
str := strings.ReplaceAll(r.format, "$property$", v.String())
str = strings.ReplaceAll(str, "$read_value$", fmt.Sprint(readValue))
Expand All @@ -237,7 +237,7 @@ func (r *mapModifier) modify(_ context.Context, v value.Value) (value.Value, err
if val, ok := r.mappings[v.String()]; ok {
return value.New(val), nil
}
return "", tholaerr.NewNotFoundError("string not found in mapping")
return value.Empty(), tholaerr.NewNotFoundError("string not found in mapping")
}

type genericStringSwitch struct {
Expand All @@ -254,9 +254,9 @@ type stringSwitchCase struct {
func (w *genericStringSwitch) switchOperate(ctx context.Context, s value.Value) (value.Value, error) {
switchValue, err := w.switchValueGetter.getSwitchValue(ctx, s)
if err != nil {
return "", errors.Wrap(err, "failed to get switch string")
return value.Empty(), errors.Wrap(err, "failed to get switch string")
}
switchString := string(switchValue)
switchString := switchValue.String()
for _, c := range w.cases {
b, err := matchStrings(ctx, switchString, w.switchMode, c.caseString)
if err != nil {
Expand All @@ -269,7 +269,7 @@ func (w *genericStringSwitch) switchOperate(ctx context.Context, s value.Value)
}
x, err := c.operators.apply(ctx, s)
if err != nil {
return "", errors.Wrapf(err, "failed to apply operations inside switch case '%s'", c.caseString)
return value.Empty(), errors.Wrapf(err, "failed to apply operations inside switch case '%s'", c.caseString)
}
return value.New(x), nil
}
Expand All @@ -296,13 +296,13 @@ type snmpwalkCountStringSwitchValueGetter struct {
func (w *snmpwalkCountStringSwitchValueGetter) getSwitchValue(ctx context.Context, _ value.Value) (value.Value, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return "", errors.New("no snmp connection available, snmpwalk not possible")
return value.Empty(), errors.New("no snmp connection available, snmpwalk not possible")
}
var i int

res, err := con.SNMP.SnmpClient.SNMPWalk(ctx, w.oid)
if err != nil {
return "", errors.Wrap(err, "snmpwalk failed")
return value.Empty(), errors.Wrap(err, "snmpwalk failed")
}
for _, r := range res {
var str string
Expand Down
36 changes: 18 additions & 18 deletions core/communicator/device_class_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (p *propertyReaderSet) getProperty(ctx context.Context) (value.Value, error
return property, nil
}
}
return "", tholaerr.NewNotFoundError("failed to read out property")
return value.Empty(), tholaerr.NewNotFoundError("failed to read out property")
}

type basePropertyReader struct {
Expand All @@ -38,21 +38,21 @@ func (b *basePropertyReader) getProperty(ctx context.Context) (value.Value, erro
conditionsMatched, err := b.preCondition.check(ctx)
if err != nil {
log.Ctx(ctx).Trace().Err(err).Msg("error during pre condition check")
return "", errors.Wrap(err, "an error occurred while checking preconditions")
return value.Empty(), errors.Wrap(err, "an error occurred while checking preconditions")
}
if !conditionsMatched {
log.Ctx(ctx).Trace().Err(err).Msg("pre condition not fulfilled")
return "", errors.New("pre condition failed")
return value.Empty(), errors.New("pre condition failed")
}
}
v, err := b.propertyReader.getProperty(ctx)
if err != nil {
return "", err
return value.Empty(), err
}
v, err = b.applyOperators(ctx, v)
if err != nil {
log.Ctx(ctx).Trace().Err(err).Msg("error while applying operators")
return "", errors.Wrap(err, "error while applying operators")
return value.Empty(), errors.Wrap(err, "error while applying operators")
}
log.Ctx(ctx).Trace().Msgf("property determined (%v)", v)
return v, nil
Expand All @@ -77,13 +77,13 @@ type sysObjectIDPropertyReader struct {
func (c *sysObjectIDPropertyReader) getProperty(ctx context.Context) (value.Value, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return "", errors.New("snmp data is missing, SysObjectID property cannot be read")
return value.Empty(), errors.New("snmp data is missing, SysObjectID property cannot be read")
}

sysObjectID, err := con.SNMP.GetSysObjectID(ctx)
if err != nil {
log.Ctx(ctx).Trace().Str("property_reader", "SysObjectID").Msg("failed to get sys object id")
return "", errors.New("failed to get sys object id")
return value.Empty(), errors.New("failed to get sys object id")
}
log.Ctx(ctx).Trace().Str("property_reader", "SysObjectID").Msg("received SysObjectID successfully")

Expand All @@ -96,13 +96,13 @@ type sysDescriptionPropertyReader struct {
func (c *sysDescriptionPropertyReader) getProperty(ctx context.Context) (value.Value, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil {
return "", errors.New("snmp data is missing, SysDescription property cannot be read")
return value.Empty(), errors.New("snmp data is missing, SysDescription property cannot be read")
}

sysDescription, err := con.SNMP.GetSysDescription(ctx)
if err != nil {
log.Ctx(ctx).Trace().Str("property_reader", "SysDescription").Msg("failed to get sys description")
return "", errors.New("failed to get sys description")
return value.Empty(), errors.New("failed to get sys description")
}
log.Ctx(ctx).Trace().Str("property_reader", "SysDescription").Msg("received SysDescription successfully")

Expand All @@ -116,13 +116,13 @@ type snmpGetPropertyReader struct {
func (s *snmpGetPropertyReader) getProperty(ctx context.Context) (value.Value, error) {
con, ok := network.DeviceConnectionFromContext(ctx)
if !ok || con.SNMP == nil || con.SNMP.SnmpClient == nil {
return "", errors.New("No SNMP Data available!")
return value.Empty(), errors.New("No SNMP Data available!")
}
oid := string(s.OID)
result, err := con.SNMP.SnmpClient.SNMPGet(ctx, oid)
if err != nil {
log.Ctx(ctx).Trace().Err(err).Str("property_reader", "snmpget").Msg("snmpget on oid " + oid + " failed")
return "", errors.Wrap(err, "snmpget failed")
return value.Empty(), errors.Wrap(err, "snmpget failed")
}

var val interface{}
Expand All @@ -133,7 +133,7 @@ func (s *snmpGetPropertyReader) getProperty(ctx context.Context) (value.Value, e
}
if err != nil {
log.Ctx(ctx).Trace().Err(err).Str("property_reader", "snmpget").Msg("snmpget failed")
return "", err
return value.Empty(), err
}
log.Ctx(ctx).Trace().Str("property_reader", "snmpget").Msg("snmpget successful")
return value.New(val), nil
Expand All @@ -144,12 +144,12 @@ type vendorPropertyReader struct{}
func (v *vendorPropertyReader) getProperty(ctx context.Context) (value.Value, error) {
properties, ok := device.DevicePropertiesFromContext(ctx)
if !ok {
return "", errors.New("no properties found in context")
return value.Empty(), errors.New("no properties found in context")
}

if properties.Properties.Vendor == nil {
log.Ctx(ctx).Trace().Str("property_reader", "vendor").Msg("vendor has not yet been determined")
return "", tholaerr.NewPreConditionError("vendor has not yet been determined")
return value.Empty(), tholaerr.NewPreConditionError("vendor has not yet been determined")
}
return value.New(*properties.Properties.Vendor), nil
}
Expand All @@ -159,12 +159,12 @@ type modelPropertyReader struct{}
func (m *modelPropertyReader) getProperty(ctx context.Context) (value.Value, error) {
properties, ok := device.DevicePropertiesFromContext(ctx)
if !ok {
return "", errors.New("no properties found in context")
return value.Empty(), errors.New("no properties found in context")
}

if properties.Properties.Model == nil {
log.Ctx(ctx).Trace().Str("property_reader", "model").Msg("model has not yet been determined")
return "", tholaerr.NewPreConditionError("model has not yet been determined")
return value.Empty(), tholaerr.NewPreConditionError("model has not yet been determined")
}
return value.New(*properties.Properties.Model), nil
}
Expand All @@ -174,12 +174,12 @@ type modelSeriesPropertyReader struct{}
func (m *modelSeriesPropertyReader) getProperty(ctx context.Context) (value.Value, error) {
properties, ok := device.DevicePropertiesFromContext(ctx)
if !ok {
return "", errors.New("no properties found in context")
return value.Empty(), errors.New("no properties found in context")
}

if properties.Properties.ModelSeries == nil {
log.Ctx(ctx).Trace().Str("property_reader", "model_series").Msg("model series has not yet been determined")
return "", tholaerr.NewPreConditionError("model series has not yet been determined")
return value.Empty(), tholaerr.NewPreConditionError("model series has not yet been determined")
}
return value.New(*properties.Properties.ModelSeries), nil
}
3 changes: 1 addition & 2 deletions core/communicator/network_device_communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ func (c *networkDeviceCommunicator) GetIdentifyProperties(ctx context.Context) (
}
} else {
dev.Properties.OSVersion = &osVersion
ctx = device.NewContextWithDeviceProperties(ctx, dev)
}

return dev.Properties, nil
Expand Down Expand Up @@ -407,7 +406,7 @@ func (c *networkDeviceCommunicator) GetSBCComponent(ctx context.Context) (device
}

if empty {
return device.SBCComponent{}, tholaerr.NewNotFoundError("no cpu data available")
return device.SBCComponent{}, tholaerr.NewNotFoundError("no sbc data available")
}

return sbc, nil
Expand Down
5 changes: 5 additions & 0 deletions core/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func New(i interface{}) Value {
return v
}

// Empty returns the an empty value.
func Empty() Value {
return ""
}

// String returns the value as a string
func (v *Value) String() string {
return string(*v)
Expand Down

0 comments on commit a327831

Please sign in to comment.