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

Add GetString function #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

goodspark
Copy link

Resolves #53

Convenience function so users don't have to do:

s := string(v.GetStringBytes("key", "a", "b"))

Resolves valyala#53

Convenience function so users don't have to do:

```go
s := string(v.GetStringBytes("key", "a", "b"))
```
@codecov
Copy link

codecov bot commented Mar 2, 2020

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.01%. Comparing base (1649ca5) to head (da55489).
Report is 13 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #54      +/-   ##
==========================================
+ Coverage   97.00%   97.01%   +0.01%     
==========================================
  Files           9        9              
  Lines        1067     1072       +5     
==========================================
+ Hits         1035     1040       +5     
  Misses         19       19              
  Partials       13       13              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

// Array indexes may be represented as decimal numbers in keys.
//
// "" is returned for non-existing keys path or for invalid value type.
func (v *Value) GetString(keys ...string) string {
Copy link
Author

Choose a reason for hiding this comment

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

I didn't add an equivalent String function because that would collide with the existing one that returns a string representation of the value.

@valyala
Copy link
Owner

valyala commented Mar 3, 2020

Unfortunately this won't work as expected, since the returned string will contain garbage after the next call to Parser.Parse, since v.s is backed by byte slice, which is modified on each Parser.Parse call.

@goodspark
Copy link
Author

What do you suggest?

@valyala
Copy link
Owner

valyala commented Mar 3, 2020

Either stick to string(v.GetStringBytes()) or use something unsafe like toUnsafeString(v.GetStringBytes()) if you know possible consequences of improper usage of the returned unsafe string.

@goodspark
Copy link
Author

What if GetString returned string(v.GetStringBytes())?

@phuslu
Copy link

phuslu commented Oct 23, 2020

string(v.GetStringBytes()) will do a memory copy.
I think add "GetString" is not a good idea, we shall keep GetStringBytes and let users choose use b2s() or string() in their usecases.

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.

Provide a GetString to get string type instead of byte slice
3 participants