chewbranca commented on code in PR #5602: URL: https://github.com/apache/couchdb/pull/5602#discussion_r2221281060
########## src/couch_stats/src/csrt_entry.erl: ########## @@ -0,0 +1,244 @@ +% Licensed under the Apache License, Version 2.0 (the "License"); you may not +% use this file except in compliance with the License. You may obtain a copy of +% the License at +% +% http://www.apache.org/licenses/LICENSE-2.0 +% +% Unless required by applicable law or agreed to in writing, software +% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +% License for the specific language governing permissions and limitations under +% the License. + +-module(csrt_entry). + +-include_lib("stdlib/include/ms_transform.hrl"). +-include_lib("csrt.hrl"). + +-export([ + value/2, + key/1, + from_map/1, + record_info/0 +]). + +%% JSON Conversion API +-export([ + convert_type/1, + convert_pidref/1, + convert_pid/1, + convert_ref/1, + convert_string/1, + to_json/1 +]). + +-spec value(rctx_field(), #rctx{}) -> any(). + +value(pid_ref, #rctx{pid_ref = Val}) -> Val; +value(nonce, #rctx{nonce = Val}) -> Val; +value(type, #rctx{type = Val}) -> convert_type(Val); +value(dbname, #rctx{dbname = Val}) -> Val; +value(username, #rctx{username = Val}) -> Val; +value(db_open, #rctx{db_open = Val}) -> Val; +value(docs_read, #rctx{docs_read = Val}) -> Val; +value(docs_written, #rctx{docs_written = Val}) -> Val; +value(rows_read, #rctx{rows_read = Val}) -> Val; +value(changes_returned, #rctx{changes_returned = Val}) -> Val; +value(ioq_calls, #rctx{ioq_calls = Val}) -> Val; +value(js_filter, #rctx{js_filter = Val}) -> Val; +value(js_filtered_docs, #rctx{js_filtered_docs = Val}) -> Val; +value(get_kv_node, #rctx{get_kv_node = Val}) -> Val; +value(get_kp_node, #rctx{get_kp_node = Val}) -> Val; +value(started_at, #rctx{started_at = Val}) -> Val; +value(updated_at, #rctx{updated_at = Val}) -> Val. + +-spec key(BinKey :: binary() | string() | atom()) -> + Key :: + rctx_field() + | {error, Reason :: any()}. + +key(Key) when is_atom(Key) -> + key_from_atom(Key); +key(Key) when is_binary(Key) -> + key_from_binary(Key); +key(Key) when is_list(Key) -> + case key_from_binary(list_to_binary(Key)) of + {error, {invalid_key, _Key}} -> + {error, {invalid_key, Key}}; + Res -> + Res + end; +key(Other) -> + key_error(Other). + +key_from_atom(pid_ref) -> pid_ref; +key_from_atom(nonce) -> nonce; +key_from_atom(type) -> type; +key_from_atom(dbname) -> dbname; +key_from_atom(username) -> username; +key_from_atom(db_open) -> db_open; +key_from_atom(docs_read) -> docs_read; +key_from_atom(rows_read) -> rows_read; +key_from_atom(changes_returned) -> changes_returned; +key_from_atom(ioq_calls) -> ioq_calls; +key_from_atom(js_filter) -> js_filter; +key_from_atom(js_filtered_docs) -> js_filtered_docs; +key_from_atom(get_kv_node) -> get_kv_node; +key_from_atom(get_kp_node) -> get_kp_node; +key_from_atom(Other) -> key_error(Other). + +key_from_binary(<<"pid_ref">>) -> pid_ref; +key_from_binary(<<"nonce">>) -> nonce; +key_from_binary(<<"type">>) -> type; +key_from_binary(<<"dbname">>) -> dbname; +key_from_binary(<<"username">>) -> username; +key_from_binary(<<"db_open">>) -> db_open; +key_from_binary(<<"docs_read">>) -> docs_read; +key_from_binary(<<"rows_read">>) -> rows_read; +key_from_binary(<<"changes_returned">>) -> changes_returned; +key_from_binary(<<"ioq_calls">>) -> ioq_calls; +key_from_binary(<<"js_filter">>) -> js_filter; +key_from_binary(<<"js_filtered_docs">>) -> js_filtered_docs; +key_from_binary(<<"get_kv_node">>) -> get_kv_node; +key_from_binary(<<"get_kp_node">>) -> get_kp_node; +key_from_binary(Other) -> key_error(Other). Review Comment: Same thing as above with `key_from_atom`, yes, this is verbosity, but it's well encapsulated into specific functions to hide that verbosity, and done in a clean way that avoids having to do dynamic runtime conversions. Again, I'd prefer to see parse transforms on top of the `#rctx{}` record to generate all of these functions, but for a first pass we've got hardcoded conversion functions to efficiently handle the logic, as well as various use of static analysis to ensure that the `rctx_field()`'s are picked up as expected. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
