Coder Social home page Coder Social logo

optional parameters in request? about silicon HOT 6 CLOSED

matt-42 avatar matt-42 commented on August 10, 2024
optional parameters in request?

from silicon.

Comments (6)

matt-42 avatar matt-42 commented on August 10, 2024

Just mark your argument with the _optional symbol:

_argX(_optional) = int()

from silicon.

byzhang avatar byzhang commented on August 10, 2024

Thanks, it also works. A related question is how to assign the default value? I tried

_argX(_optional) = std::string("some_default_value")

And it doesn't work.

from silicon.

byzhang avatar byzhang commented on August 10, 2024

Another relevant question, does Iod support enum (class)?
I tried

enum class Gender {
MALE,
FEMALE
};
_argX = Gender()

it seems not compilable.

from silicon.

byzhang avatar byzhang commented on August 10, 2024

Back to the optional, how to check whether an optional arg is set or not?

_my_scope = D(_test2(_name=D(_first=std::string(), _second(_optional)=std::string()), _gender=int()) = [] (auto p) {
  return D(_message = "hello " + p.name.first + " " + (p.name.has(_second)? p.name.second: "default"));
})

Here is the result:

curl "http://127.0.0.1:23456/my_scope/test2" -H "Content-Type: application/json" --data '{"name":{"first": "a"}, "gender":1}'
{"message":"hello a "}%

It seems the p.name.has(_second) is true, but p.name.second has the default value "".

from silicon.

matt-42 avatar matt-42 commented on August 10, 2024

As of today, there is no way to check whether the optional arg has been set. I am thinking about how to implement it. The best solution to me would be to use something like boost::optional to store the optional fields...

from silicon.

matt-42 avatar matt-42 commented on August 10, 2024

It is now possible, here is an example:
https://github.com/matt-42/silicon/blob/master/tests/optional.cc

from silicon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.