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

Added json marshal/unmarshal methods and structs #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

krishnaduttPanchagnula
Copy link

Closes #4

@krishnaduttPanchagnula
Copy link
Author

@Semior001 can you have a look at it.

range.go Outdated
@@ -51,6 +52,11 @@ type Range struct {
dur time.Duration
}

type MarshalTime struct {
Copy link
Member

Choose a reason for hiding this comment

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

I actually think that the type could be private, as we want to hide the logic of marshalling and unmarshalling behind the MarshalJSON and UnmarshalJSON methods of Range type

range.go Outdated
fmt.Println("JSON Data:", string(jsonData))
}

func (r *Range) UnmarshalStartEndTime(jsonData []byte) error {
Copy link
Member

Choose a reason for hiding this comment

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

Method names should be UnmarshalJSON and MarshalJSON

var data MarshalTime

if err := json.Unmarshal(jsonData, &data); err != nil {
return err
Copy link
Member

Choose a reason for hiding this comment

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

This error could be wrapped to add additional context to the error itself, for better traceability

Copy link
Member

Choose a reason for hiding this comment

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

I think this method lacks some tests, it would be better if it would be covered.

@Semior001
Copy link
Member

Thank you for your contribution!
However, the whole idea of such a change request was to provide the consumer a way to marshal this type as a field in their custom types and transfer it over some protocols, not just to print it to stdout. The type must be marshallable with json.Marshal and unmarshallable with json.Unmarshal, for which it should implement MarshalJSON() ([]byte, error) and UnmarshalJSON([]byte) error methods.

range.go Outdated
@@ -182,6 +188,34 @@ func (r Range) Flip(ranges []Range) []Range {
return r.flipValidRanges(rngs)
}

func (r Range) MarshalStartEndTime() {
Copy link

Choose a reason for hiding this comment

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

could you please provide more context with use cases where this is required.


jsonData, err := json.Marshal(data)
if err != nil {
fmt.Println("Error marshaling JSON:", err)
Copy link

Choose a reason for hiding this comment

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

why do force print needed here. Could you provide more context please where do you need use stdout f.e. in standard libraries

Copy link
Member

Choose a reason for hiding this comment

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

I agree with that point. An average consumer of the library don't want an arbitrary error to appear in stdout, but rather to print the error, that has been returned by the function on his own. Also, I would recommend you to wrap the error with the message, like "unmarshal json data", e.g.:

return nil, fmt.Errorf("unmarshal start and date: %w", err)


jsonData, err := json.Marshal(data)
if err != nil {
fmt.Println("Error marshaling JSON:", err)
Copy link
Member

Choose a reason for hiding this comment

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

I agree with that point. An average consumer of the library don't want an arbitrary error to appear in stdout, but rather to print the error, that has been returned by the function on his own. Also, I would recommend you to wrap the error with the message, like "unmarshal json data", e.g.:

return nil, fmt.Errorf("unmarshal start and date: %w", err)


// Marshal the Range to JSON
jsonData, err := r.MarshalJSON()
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to use the assert library for such assertions, as it is used in the other tests

@@ -51,6 +52,11 @@ type Range struct {
dur time.Duration
}

type marshalTime struct {
StartTime time.Time `json:"StartTIme"`
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to use snake case keys for json values, e.g. "start_time" and "end_time". Also, I'm not sure that the tests will pass, as this key has "I" letter in the upper case, when in the test it is in the lower case

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.

json marshal/unmarshal
3 participants