Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: payload serialization in sensor. Fixes #2272 #2273

Merged

Conversation

GoshaDo
Copy link
Contributor

@GoshaDo GoshaDo commented Oct 25, 2022

Fixes #2272
Signed-off-by: GoshaDo goshatoo@gmail.com

@GoshaDo GoshaDo force-pushed the fix-sensor-parameter-serlization-block branch from 12e051f to ae83c04 Compare October 25, 2022 17:41
USERS.md Outdated Show resolved Hide resolved
sensors/triggers/params_test.go Show resolved Hide resolved
USERS.md Outdated Show resolved Hide resolved
sensors/triggers/params.go Show resolved Hide resolved
sensors/triggers/params.go Outdated Show resolved Hide resolved
resultValue, err = getValueByKey(eventPayload, key)
tmp, typ, err := getValueByKey(eventPayload, key)
// For block injection support
fmt.Printf("failed to get value by key: %+v\n", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this?

How come we have so many fmt.Printf in the code.....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sensors/triggers/params.go Outdated Show resolved Hide resolved
@GoshaDo GoshaDo force-pushed the fix-sensor-parameter-serlization-block branch from 4e144c5 to 48a81a3 Compare November 3, 2022 04:11
dependabot bot and others added 25 commits November 3, 2022 06:11
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: goshado <goshatoo@gmail.com>
Signed-off-by: goshado <goshatoo@gmail.com>
Signed-off-by: goshado <goshatoo@gmail.com>
Signed-off-by: goshado <goshatoo@gmail.com>
Signed-off-by: goshado <goshatoo@gmail.com>
Signed-off-by: goshado <goshatoo@gmail.com>
Signed-off-by: goshado <goshatoo@gmail.com>
Signed-off-by: goshado <goshatoo@gmail.com>
Signed-off-by: goshado <goshatoo@gmail.com>
…goproj#2286)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: goshado <goshatoo@gmail.com>
…rgoproj#2285)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: goshado <goshatoo@gmail.com>
…rgoproj#2288)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: goshado <goshatoo@gmail.com>
…argoproj#2289)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: goshado <goshatoo@gmail.com>
…j#2283)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: goshado <goshatoo@gmail.com>
…rgoproj#2284)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: goshado <goshatoo@gmail.com>
Signed-off-by: Joep Keijsers <joep.keijsers@geodan.nl>
Signed-off-by: goshado <goshatoo@gmail.com>
Signed-off-by: Eddie Knight <iv.eddieknight@gmail.com>
Signed-off-by: goshado <goshatoo@gmail.com>
Signed-off-by: Elifarley <elifarley@gmail.com>
Signed-off-by: goshado <goshatoo@gmail.com>
Signed-off-by: Eddie Knight <knight@linux.com>
Signed-off-by: goshado <goshatoo@gmail.com>
Signed-off-by: goshado <goshatoo@gmail.com>
@GoshaDo GoshaDo force-pushed the fix-sensor-parameter-serlization-block branch from 48a81a3 to 3a8c609 Compare November 3, 2022 04:12
@GoshaDo GoshaDo requested a review from whynowy November 3, 2022 04:13
@@ -183,7 +205,7 @@ func ResolveParamValue(src *v1alpha1.TriggerParameterSource, events map[string]*
}

if err == nil {
return &resultValue, nil
return &resultValue, errorType, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right? If there's no error, return errorType?

The original logic looks very tricky. Can we make it simple like this:

if src.Value != nil {
  return *src.Value
} else {
  eventPayload, err = json.Marshal.....
  if err != nil {
    return err
  }
  return string(eventPayload)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have changed the error check just after json.Marshal
about the return, we should return a pointer to the casted eventPayload variable. I think best to write it in memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and the function returns (*string, string, error)
we can't return error only...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's crashing the unit-test... I have revert the changes

}
fmt.Printf("failed to get value by key: %+v\n", err)
}
// In case neither key nor template resolving was successful, fall back to the default value if exists
if src.Value != nil {
resultValue = *src.Value
return &resultValue, nil
return &resultValue, typ, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the typ returned here have a value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right... I will return errorType, because we fail the function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It falls back to the default value, does not fail the function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you suggest to return here?

default:
// The parameter doesn't have a default value and is referencing a dependency that is
// missing in the received events. This is not an error and may happen with || conditions.
return nil, nil
return nil, errorType, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original return seems to be incorrect, it should return an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkout the comment in lines 230-231

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's the case, can you return stringType as that's the original expectation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have changed it

sensors/triggers/params.go Outdated Show resolved Hide resolved
default:
// The parameter doesn't have a default value and is referencing a dependency that is
// missing in the received events. This is not an error and may happen with || conditions.
return nil, nil
return nil, errorType, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's the case, can you return stringType as that's the original expectation?

// Get the value corresponding to specified key or template within event payload
if eventPayload != nil {
if tmplt != "" {
resultValue, err = getValueWithTemplate(eventPayload, tmplt)
if err == nil {
return &resultValue, nil
return &resultValue, errorType, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why return errorType when err == nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because the we didn't resolver the param. what are you suggestions ?

}
fmt.Printf("failed to get value by key: %+v\n", err)
}
// In case neither key nor template resolving was successful, fall back to the default value if exists
if src.Value != nil {
resultValue = *src.Value
return &resultValue, nil
return &resultValue, errorType, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why return errorType?

@@ -179,12 +200,11 @@ func ResolveParamValue(src *v1alpha1.TriggerParameterSource, events map[string]*
} else {
// Default value doesn't exist so return the whole event payload
eventPayload, err = json.Marshal(&event)
if err == nil {
return &resultValue, errorType, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why return errorType?

// returns an error if the Path is invalid/not found and the default value is nil OR if the eventDependency event doesn't exist and default value is nil
func ResolveParamValue(src *v1alpha1.TriggerParameterSource, events map[string]*v1alpha1.Event) (*string, error) {
func ResolveParamValue(src *v1alpha1.TriggerParameterSource, events map[string]*v1alpha1.Event) (*string, string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even you added an errorType in the return, but you never checked it. To make it simple, the guideline should be:

  1. If there's no error, return (xxx, xx, nil)
  2. Or return (nil, "", err)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that what originally been there... reverting those changes..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// Otherwise, return the error
return nil, err
return nil, errorType, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to have an errorType, since you never checked it.

Simply return nil, "", err,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

// Get the value corresponding to specified key or template within event payload
if eventPayload != nil {
if tmplt != "" {
resultValue, err = getValueWithTemplate(eventPayload, tmplt)
if err == nil {
return &resultValue, nil
return &resultValue, "", nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason it returns "" as the type instead of stringType when err == nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
fmt.Printf("failed to get value by key: %+v\n", err)
}
// In case neither key nor template resolving was successful, fall back to the default value if exists
if src.Value != nil {
resultValue = *src.Value
return &resultValue, nil
return &resultValue, "", nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it returns "" as the type instead of stringType?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
return "", fmt.Errorf("key %s does not exist to in the event payload", key)
return "", errorType, fmt.Errorf("key %s does not exist to in the event payload", key)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to have an errorType, same reason as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

} else if res.Type.String() == jsonType {
return res.Raw, res.Type.String(), nil
}
return res.Raw, res.Type.String(), nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the default return, make it consistent as before:

return res.String(), stringType, nil

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -183,7 +203,7 @@ func ResolveParamValue(src *v1alpha1.TriggerParameterSource, events map[string]*
}

if err == nil {
return &resultValue, nil
return &resultValue, "", nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return &resultValue, stringType, nil

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

@whynowy whynowy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@whynowy whynowy changed the title fix of payload serialisation in sensor Fix #2272 fix: payload serialization in sensor. Fixes #2272 Nov 11, 2022
@whynowy whynowy merged commit 876baf6 into argoproj:master Nov 11, 2022
whynowy pushed a commit that referenced this pull request Dec 12, 2022
Signed-off-by: goshado <goshatoo@gmail.com>
bilalba pushed a commit to intuit-data-os/argo-events that referenced this pull request Jan 9, 2023
)

Signed-off-by: goshado <goshatoo@gmail.com>
Signed-off-by: Bilal Bakht Ahmad <tringingly@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Parameterization a block of k8s resource (set of key values)
7 participants