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

Provide a GetString to get string type instead of byte slice #53

Open
goodspark opened this issue Feb 29, 2020 · 1 comment · May be fixed by #54
Open

Provide a GetString to get string type instead of byte slice #53

goodspark opened this issue Feb 29, 2020 · 1 comment · May be fixed by #54
Labels
enhancement New feature or request

Comments

@goodspark
Copy link

Could we get a function to just return the string type instead of a byte slice?

I often just need the string and noticed GetStringByte is actually converting the underlying string to a byte.

func (v *Value) GetString(keys ...string) string {
  v = v.Get(keys...)
  if v == nil || v.Type() != TypeString {
    return ""
  }
  return v.s
}
goodspark pushed a commit to goodspark/fastjson that referenced this issue Mar 2, 2020
Resolves valyala#53

Convenience function so users don't have to do:

```go
s := string(v.GetStringBytes("key", "a", "b"))
```
@goodspark goodspark linked a pull request Mar 2, 2020 that will close this issue
@valyala valyala added the enhancement New feature or request label Mar 3, 2020
@valyala
Copy link
Owner

valyala commented Mar 3, 2020

This won't work, since v.s is backed by byte slice, which changes on the next Parser.Parse call. This means that the returned string contents will become garbage on the next Parser.Parse call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants