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

Expose previous and current resource properties #185

Closed
wants to merge 3 commits into from

Conversation

parsnips
Copy link

@parsnips parsnips commented Dec 11, 2020

Issue #, if available:

#184

Description of changes:

Allows users to use their own Unmarshal strategy by exposing the making the raw resource bytes through a get method.

e.g.

       // Populate the previous model
	prevModel := &resource.Model{}
	if err := json.Unmarshal(req.GetPreviousResourcePropertiesBody(), prevModel); err != nil {
		log.Printf("Error unmarshaling prev model: %v", err)
		return handler.NewFailedEvent(err)
	}

	// Populate the current model
	currentModel := &resource.Model{}
	if err := json.Unmarshal(req.GetResourcePropertiesBody(), currentModel); err != nil {
		log.Printf("Error unmarshaling model: %v", err)
		return handler.NewFailedEvent(err)
	}

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

This will allow users to do their own marshaling/unmarshaling.

aws-cloudformation#184
@@ -38,6 +38,14 @@ type Request struct {
resourcePropertiesBody []byte
}

func (r *Request) GetPreviousResourcePropertiesBody() []byte {
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason for using the GetPreviousResourcePropertiesBody instead of just going with PreviousResourcePropertiesBody?

Copy link
Author

Choose a reason for hiding this comment

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

The JVM has affected me more than I realized ;). I'll push up the shorter version.

@kddejong
Copy link
Contributor

I like this idea. A lot of times when working with other packages they already have the models I need defined. They already have the logic for Marshaling and Unmarshaling as well.

The only thing that I got tripped up is when I have to send the response the stringify would fail a lot with non standard types

@parsnips
Copy link
Author

parsnips commented Dec 17, 2020

The only thing that I got tripped up is when I have to send the response the stringify would fail a lot with non standard types

Yes I noticed that bool is sent in as a string in my case and now understand why the stringify and unstringify methods exist... In my case I just encapsulated that logic into a type... and used it in place of bools in my model.

eg:

type StringyBool bool

func (s *StringyBool) UnmarshalJSON(data []byte) error {
	asStr := string(data)
	if asStr == "true" {
		*s = true
	} else { // else if asStr == "false"
		*s = false
	}

	return nil
}

It's a matter of different approaches I guess.. but this one gives the end user a bit more flexibility.

@parsnips parsnips closed this Jun 6, 2022
@parsnips parsnips deleted the request-getters branch June 6, 2022 22:35
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.

2 participants