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

seastar-json2code: reformat the rendered code and cleanups #2090

Merged
merged 5 commits into from
Feb 18, 2024

Conversation

tchaikov
Copy link
Contributor

@tchaikov tchaikov commented Feb 10, 2024

this change refactors seastar-json2code.py by splitting create_h_file() into smaller pieces, and also reformats the rendered code with better formatting.

Refs #2082

instead of have a giant function for generating the .hh and .cc
files from .json, let's extract the loop body out. for better
readability. in following-up changes, we will extract some
more helper functions out.

Signed-off-by: Kefu Chai <[email protected]>
instead of concatenating strings, use Template to generate code for
enum types. for better readability.

before this change the generated code looks like:
```c++
query_enum str2query_enum(const sstring& str) {
  static const sstring arr[] = {"VAL1","VAL2","VAL3"};
  int i;
  for (i=0; i < 3; i++) {
    if (arr[i] == str) {return (query_enum)i;}
}
  return (query_enum)i;
}
```

the generated code looks like
```c++
    query_enum str2query_enum(const sstring& str) {
        static const sstring arr[] = {
        "VAL1",
"VAL2",
"VAL3"
        };
        int i;
        for (i = 0; i < 3; i++) {
            if (arr[i] == str) {
                return (query_enum)i;
            }
        }
        return (query_enum)i;
    }
```

Signed-off-by: Kefu Chai <[email protected]>
move `not_first()` up so it can be reused, and use it for indent.

before this change, the rendered code looks like
```c++
    query_enum str2query_enum(const sstring& str) {
        static const sstring arr[] = {
        "VAL1",
"VAL2",
"VAL3"
        };
        int i;
        for (i = 0; i < 3; i++) {
            if (arr[i] == str) {
                return (query_enum)i;
            }
        }
        return (query_enum)i;
    }
```

after this change, the rendered code looks like:
```c++
    query_enum str2query_enum(const sstring& str) {
        static const sstring arr[] = {
            "VAL1",
            "VAL2",
            "VAL3"
        };
        int i;
        for (i = 0; i < 3; i++) {
            if (arr[i] == str) {
                return (query_enum)i;
            }
        }
        return (query_enum)i;
    }
```

Signed-off-by: Kefu Chai <[email protected]>
instead of checking for the existence of a key in dict, just go
ahead and access it using dict.get(key, []). so that we can
reduce the indent level. this should improve the readability
a little bit.

Signed-off-by: Kefu Chai <[email protected]>
use `join()` to simplify the implementation to print the names of
required query parameters, simpler this way. this change also
prepares for scylladb#2082.

Refs scylladb#2082

Signed-off-by: Kefu Chai <[email protected]>
@tchaikov
Copy link
Contributor Author

@amnonh hi Amnon, could you help review this change?

@amnonh
Copy link
Contributor

amnonh commented Feb 15, 2024

LGTM

@tchaikov
Copy link
Contributor Author

@scylladb/seastar-maint hello maintainers, could you help merge this change?

@avikivity avikivity closed this in b210ba2 Feb 18, 2024
@avikivity avikivity merged commit b210ba2 into scylladb:master Feb 18, 2024
14 checks passed
graphcareful pushed a commit to graphcareful/seastar that referenced this pull request Mar 20, 2024
…rom Kefu Chai

this change refactors `seastar-json2code.py` by splitting `create_h_file()` into smaller pieces, and also reformats the rendered code with better formatting.

Refs scylladb#2082

Closes scylladb#2090

* github.com:scylladb/seastar:
  seastar-json2code: collect required_query_params using a list
  seastar-json2code: reduce the indent level
  seastar-json2code: indent the enum and array elements
  seastar-json2code: generate code for enum type using Template
  seastar-json2code: extract add_operation() out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants