I'm trying to test if a value coming from the gjson library is a string in the quickest and simplest way possible. I don't want to use a switch type assertion.
if reflect.TypeOf(gjson.Get(input, "name").Value()) != "string" {
return "Not a string!"
}
What's wrong with my code?
gjson.Get returns a Result, so you can simply check its Type field:
if gjson.Get(input, "name").Type != gjson.String {
return "Not a string!"
}
Related
I can return a kj::Exception where a kj::Promise is expected, like so:
kj::Promise<void> someFunc() {
return KJ_EXCEPTION(FAILED, "some description");
}
But what if I end up having a kj::Promise<kj::Exception> in a place where I don't have a waitScope? For instance:
kj::Promise<void> someFunc(kj::Promise<void> somePromise) {
return somePromise.then([]() {
return KJ_EXCEPTION(FAILED, "some description");
});
}
There my compiler complains that there is no valid conversion between kj::Promise<kj::Exception> and kj::Promise<void>.
Is there a way around that?
I'm a little surprised that I didn't prohibit Promise<Exception> in the first place; using such a construction likely leads to a lot of problems.
You can avoid it in your code by explicitly declaring the return type of the lambda to be Promise<void>, i.e.:
return somePromise.then([]() -> kj::Promise<void> {
I currently have a form post that is coming in like
{
"stuff":"cool text",
"otherthing":"neat thing",
"captions":[
{"first":"the list",
"second":"how are you"},
{"first":"wow",
etc....
]
}
Now I don't know how many captions there will be. It might be one in the array, it might be twenty.
I have set up two structures as well
type ThingContext struct {
Stuff string `json:"stuff"`
OtherThing string `json:"otherthing"`
Captions []ArrayText `json:"captions"`
}
type ArrayText struct {
First string `json:"first"`
Second string `json:"second"`
}
And in my golang function I have something like this
func (c *ThingContext) SetThingContext(rw web.ResponseWriter, req *web.Request, next web.NextMiddlewareFunc) {
if err := req.ParseForm(); err != nil {
}
c.Stuff = req.FormValue("stuff")
c.OtherThing = req.FormValue("otherthing")
}
This works fine till I try and parse the array.
When I do something along the lines of c.Captions = req.ParseForm("captions")
I get the error
.cannot use req.Request.ParseForm("captions") (type error) as type []ArrayText in assignment
You're doing it right, except for the assignment. When you run req.Request.ParseForm(), rather than returning a value, or passing in a reference to a buffer, it populates the req.Request.Form and req.Request.PostForm structures.
The related GoDoc explaining said function
So rather than
c.Captions = req.Request.ParseForm()
It would look more like
err := req.Request.ParseForm()
//check for errors as usual here
c.Captions = req.Request.Form
//or
c.Captions = req.Request.PostForm
Approaching it from this direction should put you on the right track.
Cheers!
Taylor
mock.on("FunctionName", "someStringArgument").Return(...)
Suppose if someStringArgument is "hello" then I want to return "1". But if someStringArgument is any other string I want to return "2".
How is this achieve able with GoMock?
What you want to do is write a custom function which will return your desired output.
Here is a simple example of what I do.
Define a custom response function
func FunctionNameResponse(arg String) string{
if arg == "hellp" {
// I used quotes because you mentioned "1" and not 1
return "1"
}
return "2"
}
Call custom function anywhere needed.
mock.on("FunctionName", mock.Anything).Return(FunctionNameResponse("someStringArgument"))
I need to check if a var, f.e. passed by a func, is a valid enum value. Not per se passed but just as an example here.
enum CollectionDict : String { // Mapping to String Model = "Model" or should I ...
case Model
case Type
case Element
case ....
}
....
guard InColectionDict != CollectionDict else { return false }
....
Obviously my sample guards line is wrong. What should I use or do to get the guard right or at least just compare/validate the InColectionDict variable with the enum CollectionDict in a one-liner?
I did hope to get away with..
func makeItem ( _ item: String , with key : String , inCollection : CollectionDict ) -> Bool {
guard let res = CollectionDict.inCollection else { return false }
But it give me an error.
Of course thank you in advance.
Swift is strongly typed. If your function has an non-optional Enum parameter, then at run-time it's guaranteed to be a valid enum value.
Im using the following struct
type Str struct {
Info string
Command string
}
And to fill data inside of it Im doing the following which works.
return []Str{
{"info from source",
"install && run"},
}
Now I need to change the command to array
type Str struct {
Info string
Command []string
}
And provide each of the commands("install" and "run") in new entry in the array, how can I do that
when I try with
return []Str{
{"info from source",string[]{
{"install}, {"run"}},
}
I got erorr of missing type literal, any idea what Im doing wrong
The correct syntax is the following:
return []Str{
{"info from source", []string{"install", "run"}},
}