@@ -42,33 +42,34 @@ class RequestGenerator {
4242 genTypes ( ) {
4343 this . begin ( ) ;
4444
45+ this . emit ( ) ;
46+ this . emit ( `auto LSPRequest::id() const -> std::optional<std::variant<long, std::string>> {` ) ;
47+ this . emit ( ` if (!repr_->contains("id")) return std::nullopt;` ) ;
48+ this . emit ( ` const auto& id = repr_->at("id");` ) ;
49+ this . emit ( ` if (id.is_string()) return id.get<std::string>();` ) ;
50+ this . emit ( ` return id.get<long>();` ) ;
51+ this . emit ( `}` ) ;
52+ this . emit ( ) ;
53+ this . emit ( `auto LSPRequest::method() const -> std::string {` ) ;
54+ this . emit ( ` return repr_->at("method");` ) ;
55+ this . emit ( `}` ) ;
56+
4557 const requestsAndNotifications = [ ...this . model . requests , ...this . model . notifications ] ;
4658
4759 requestsAndNotifications . forEach ( ( request ) => {
4860 const { typeName } = request ;
4961 this . emit ( ) ;
50- this . emit ( `auto ${ typeName } ::method() const -> std::string {` ) ;
51- this . emit ( ` return repr_->at("method");` ) ;
52- this . emit ( `}` ) ;
53- this . emit ( ) ;
5462 this . emit ( `auto ${ typeName } ::method(std::string method) -> ${ typeName } & {` ) ;
5563 this . emit ( ` (*repr_)["method"] = std::move(method);` ) ;
5664 this . emit ( ` return *this;` ) ;
5765 this . emit ( `}` ) ;
5866 this . emit ( ) ;
59- this . emit ( `auto ${ typeName } ::id() const -> std::variant<long, std::string> {` ) ;
60- this . emit ( ` const auto& id = repr_->at("id");` ) ;
61- this . emit ( ` if (id.is_string()) return id.get<std::string>();` ) ;
62- this . emit ( ` return id.get<long>();` ) ;
63- this . emit ( `}` ) ;
64- this . emit ( ) ;
65- this . emit ( `auto ${ typeName } ::id(long id) -> ${ typeName } & {` ) ;
66- this . emit ( ` (*repr_)["id"] = id;` ) ;
67- this . emit ( ` return *this;` ) ;
68- this . emit ( `}` ) ;
69- this . emit ( ) ;
70- this . emit ( `auto ${ typeName } ::id(std::string id) -> ${ typeName } & {` ) ;
71- this . emit ( ` (*repr_)["id"] = std::move(id);` ) ;
67+ this . emit ( `auto ${ typeName } ::id(std::variant<long, std::string> id) -> ${ typeName } & {` ) ;
68+ this . emit ( ` if (std::holds_alternative<long>(id)) {` ) ;
69+ this . emit ( ` (*repr_)["id"] = std::get<long>(id);` ) ;
70+ this . emit ( ` } else {` ) ;
71+ this . emit ( ` (*repr_)["id"] = std::get<std::string>(id);` ) ;
72+ this . emit ( ` }` ) ;
7273 this . emit ( ` return *this;` ) ;
7374 this . emit ( `}` ) ;
7475
@@ -91,12 +92,6 @@ class RequestGenerator {
9192 const responseTypeName = typeName . replace ( / R e q u e s t $ / , "Response" ) ;
9293 const resultTypeName = toCppType ( request . result ) ;
9394
94- this . emit ( ) ;
95- this . emit ( `auto ${ responseTypeName } ::id() const -> std::variant<long, std::string> {` ) ;
96- this . emit ( ` const auto& id = repr_->at("id");` ) ;
97- this . emit ( ` if (id.is_string()) return id.get<std::string>();` ) ;
98- this . emit ( ` return id.get<long>();` ) ;
99- this . emit ( `}` ) ;
10095 this . emit ( ) ;
10196 this . emit ( `auto ${ responseTypeName } ::id(long id) -> ${ responseTypeName } & {` ) ;
10297 this . emit ( ` (*repr_)["id"] = id;` ) ;
0 commit comments