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

Allowing for encoding of values that implement compatible interfaces #1

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

Conversation

mariusor
Copy link

When it's type implements a known compatible interface

Currently: fmt.Stringer and encoding.TextMarshaller

…nterface

Currently: fmt.Stringer and encoding.TextMarshaller
@mariusor
Copy link
Author

Hi, I don't know if this repository is still maintained, but if it is, I made a small improvement to it and I would appreciate if someone can review it.

…tion too

Also refactored the logic into a separate function
@mariusor
Copy link
Author

mariusor commented Sep 19, 2018

I realized I haven't provided a use case where this would be needed.

In my case I have a type which is an alias for a byte slice. However the slice is pretty long, 65 characters, of which only the first 7-8 are needed, basically a SHA hash.

So for me it would be useful if the value of such a type would be restricted to just 8 characters, which is already being done for when marshaling it for Json with the help of a MarshalText() method.

type Hash []byte

func (h Hash) MarshalText () ([]byte, error) {
    return []byte(h[0:8])
} 

var hash = Hash("42ea17b556f4f77c8d9fcdfde313e7847f2115f0cc1a2ae04347a568960396ae")

hashes := make(map[string][]Hash)

hashes["hashes"] = []Hash{ hash, }

s, _ := qstring.MarshalString(&hashes)

fmt.Println(s)
// prints hashes=42ea17b5

@mariusor mariusor changed the title Adding a value's encoding Allowing for encoding of values that implement compatible interfaces Sep 19, 2018
@@ -116,7 +126,21 @@ func marshalSlice(field reflect.Value) []string {
return out
}

func compatibleInterfaceValue(field reflect.Value) (string, bool) {
Copy link
Author

Choose a reason for hiding this comment

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

In the end I think relying on encoding.TextMarshaler and fmt.Stringer interfaces for compatible types is a mistake, as using their methods implicitly might be a problem for structs that have a different logic for them than what qstring would expect.

I think a better idea would be to create a qstring.Marshaler interface which types would be required to implement explicitly.

A concrete example would be a named type for a slice. A qstring specific implementation would have to prepend the property name (or qstring tag) for every value, but a plain Stringer/TextMarshaler method does not have the complete information for it, and as such would return a broken value.

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.

1 participant