github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/native/test/example.h (about)

     1  /**
     2   * Copyright 2023 CloudWeGo Authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  #include "../thrift.h"
    18  #include "xprintf.h"
    19  #include "../map.h"
    20  
    21  #ifndef EXAMPLE_H
    22  #define EXAMPLE_H
    23  
    24  const char EXAMPLE_JSON[] = "{\"Msg\":\"中文\",\"Base\":{\"LogID\":\"a\",\"Caller\":\"b\",\"Addr\":\"c\",\"Client\":\"d\"}}";
    25  
    26  const size_t EXMAPLE_MAX_KEY_LEN = 12;
    27  
    28  tTypeDesc STRING_DESC = {
    29      .type = TTYPE_STRING,
    30      .name = (GoString){.buf = "string", .len = 6},
    31  };
    32  
    33  tTypeDesc INT32_DESC = {
    34      .type = TTYPE_I32,
    35      .name = (GoString){.buf = "i32", .len = 3},
    36  };
    37  
    38  tTypeDesc EXTRA_DESC = {
    39      .type = TTYPE_I32,
    40      .name = (GoString){.buf = "map", .len = 3},
    41      .key = &STRING_DESC,
    42      .elem = &STRING_DESC,
    43  };
    44  
    45  tTypeDesc BASE_DESC = {
    46      .is_base = true,
    47      .type = TTYPE_STRUCT,
    48      .name = (GoString){.buf = "struct", .len = 6},
    49      .st = &(tStructDesc){
    50          .name = (GoString){.buf = "Base", .len = 4},
    51          .ids = (GoSlice){
    52              .buf = (char *)(&(tFieldDesc *[7]){}), //TODO
    53              .cap = 7,
    54              .len = 7,
    55          },
    56          .names = (tFieldNameMap){
    57              .trie = &(TrieTree){
    58                  .positions = (GoSlice){
    59                      .buf = (char *)(&(size_t[1]){1}), // o,a,d,l,r,x
    60                      .cap = 1,
    61                      .len = 1,
    62                  },
    63                  .node = (TrieNode){
    64                      .index = (GoSlice){
    65                          .buf = (char *)(&(TrieNode[127]){}), //TODO: o,a,d,l,r,x
    66                          .cap = 127,
    67                          .len = 127,
    68                      },
    69                  },
    70              },
    71              .hash = &(HashMap){
    72                  .N = 24, .b = (Entry[24]){}, //TODO
    73              },
    74          },
    75          .reqs = &(ReqBitMap){
    76              .buf = (uint64_t[1]){0x1540000000000000ull},
    77              .len = 1,
    78          },
    79      },
    80  };
    81  
    82  tFieldDesc BASE_FIELD_1 = {
    83      .required = REQ_DEFAULT,
    84      .ID = 1,
    85      .name = (GoString){.buf = "LogID", .len = 5},
    86      .alias = (GoString){.buf = "log_id", .len = 5},
    87      .type = &STRING_DESC,
    88  };
    89  
    90  tFieldDesc BASE_FIELD_2 = {
    91      .required = REQ_DEFAULT,
    92      .ID = 2,
    93      .name = (GoString){.buf = "Caller", .len = 6},
    94      .alias = (GoString){.buf = "caller", .len = 6},
    95      .type = &STRING_DESC,
    96  };
    97  
    98  tFieldDesc BASE_FIELD_3 = {
    99      .required = REQ_DEFAULT,
   100      .ID = 3,
   101      .name = (GoString){.buf = "Addr", .len = 4},
   102      .alias = (GoString){.buf = "addr", .len = 4},
   103      .type = &STRING_DESC,
   104  };
   105  
   106  tFieldDesc BASE_FIELD_4 = {
   107      .required = REQ_DEFAULT,
   108      .ID = 4,
   109      .name = (GoString){.buf = "Client", .len = 6},
   110      .alias = (GoString){.buf = "Client", .len = 6},
   111      .type = &STRING_DESC,
   112  };
   113  
   114  tFieldDesc BASE_FIELD_5 = {
   115      .required = REQ_OPTIONAL,
   116      .ID = 5,
   117      .name = (GoString){.buf = "TrafficEnv", .len = 10},
   118      .alias = (GoString){.buf = "traffic_env", .len = 11},
   119      .type = &STRING_DESC,
   120  };
   121  
   122  tFieldDesc BASE_FIELD_6 = {
   123      .required = REQ_OPTIONAL,
   124      .ID = 6,
   125      .name = (GoString){.buf = "Extra", .len = 5},
   126      .alias = (GoString){.buf = "extra", .len = 5},
   127      .type = &EXTRA_DESC,
   128  };
   129  
   130  tTypeDesc EXAMPLE_DESC = {
   131      .name = (GoString){.buf = "struct", .len = 6},
   132      .type = TTYPE_STRUCT,
   133      .st = &(tStructDesc){
   134          .name = (GoString){.buf = "ExampleReq", .len = 10},
   135          .ids = (GoSlice){
   136              .buf = (char *)(&(tFieldDesc *[256]){}), //TODO
   137              .cap = 256,
   138              .len = 256,
   139          },
   140          .names = (tFieldNameMap){
   141              .trie = &(TrieTree){
   142                  .positions = (GoSlice){
   143                      .buf = (char *)(&(size_t[1]){0}), // M,I,B
   144                      .cap = 1,
   145                      .len = 1,
   146                  },
   147                  .node = (TrieNode){
   148                      .index = (GoSlice){
   149                          .buf = (char *)(&(TrieNode[127]){}), //TODO
   150                          .cap = 127,
   151                          .len = 127,
   152                      },
   153                  },
   154              },
   155              .hash = &(HashMap){
   156                  .N = 12, .b = (Entry[12]){}, //TODO
   157              },
   158          },
   159          .reqs = &(ReqBitMap){
   160              .buf = (uint64_t[8]){0x100000000000000ull, 0x0ull, 0x0ull, 0x0ull, 0x0ull, 0x0ull, 0x0ull, 0x2ull},
   161              .len = 8,
   162          },
   163      },
   164  };
   165  
   166  tFieldDesc EXAMPLE_FIELD_1 = {
   167      .required = REQ_OPTIONAL,
   168      .ID = 1,
   169      .name = (GoString){.buf = "Msg", .len = 3},
   170      .alias = (GoString){.buf = "msg", .len = 3},
   171      .type = &STRING_DESC,
   172  };
   173  
   174  tFieldDesc EXAMPLE_FIELD_3 = {
   175      .required = REQ_DEFAULT,
   176      .ID = 3,
   177      .name = (GoString){.buf = "InnerBase", .len = 9},
   178      .alias = (GoString){.buf = "inner_base", .len = 10},
   179      .type = &INT32_DESC,
   180  };
   181  
   182  tFieldDesc EXAMPLE_FIELD_255 = {
   183      .required = REQ_REQUIRED,
   184      .ID = 255,
   185      .name = (GoString){.buf = "Base", .len = 4},
   186      .alias = (GoString){.buf = "base", .len = 4},
   187      .type = &BASE_DESC,
   188  };
   189  
   190  TrieNode EXAMPLE_TN_1 = {
   191      .leaves = &(GoSlice){
   192          .buf = (char *)&(Pair){
   193              .key = (GoString){.buf = "Msg", .len = 3},
   194              .val = &EXAMPLE_FIELD_1,
   195          },
   196          .len = 1,
   197          .cap = 1,
   198      },
   199  };
   200  
   201  TrieNode EXAMPLE_TN_3 = {
   202      .leaves = &(GoSlice){
   203          .buf = (char *)&(Pair){
   204              .key = (GoString){.buf = "InnerBase", .len = 9},
   205              .val = &EXAMPLE_FIELD_3,
   206          },
   207          .len = 1,
   208          .cap = 1,
   209      },
   210  };
   211  
   212  TrieNode EXAMPLE_TN_255 = {
   213      .leaves = &(GoSlice){
   214          .buf = (char *)&(Pair){
   215              .key = (GoString){.buf = "Base", .len = 4},
   216              .val = &EXAMPLE_FIELD_255,
   217          },
   218          .len = 1,
   219          .cap = 1,
   220      },
   221  };
   222  
   223  #define SIZE_GO_SLICE sizeof(GoSlice)
   224  
   225  TrieNode BASE_TN_1 = {
   226      .leaves = &(GoSlice){
   227          .buf = (char *)&(Pair){
   228              .key = (GoString){.buf = "LogID", .len = 5},
   229              .val = &BASE_FIELD_1,
   230          },
   231          .len = 1,
   232          .cap = 1,
   233      },
   234  };
   235  
   236  TrieNode BASE_TN_2 = {
   237      .leaves = &(GoSlice){
   238          .buf = (char *)&(Pair){
   239              .key = (GoString){.buf = "Caller", .len = 6},
   240              .val = &BASE_FIELD_2,
   241          },
   242          .len = 1,
   243          .cap = 1,
   244      },
   245  };
   246  
   247  TrieNode BASE_TN_3 = {
   248      .leaves = &(GoSlice){
   249          .buf = (char *)&(Pair){
   250              .key = (GoString){.buf = "Addr", .len = 4},
   251              .val = &BASE_FIELD_3,
   252          },
   253          .len = 1,
   254          .cap = 1,
   255      },
   256  };
   257  
   258  TrieNode BASE_TN_4 = {
   259      .leaves = &(GoSlice){
   260          .buf = (char *)&(Pair){
   261              .key = (GoString){.buf = "Client", .len = 6},
   262              .val = &BASE_FIELD_4,
   263          },
   264          .len = 1,
   265          .cap = 1,
   266      },
   267  };
   268  
   269  TrieNode BASE_TN_5 = {
   270      .leaves = &(GoSlice){
   271          .buf = (char *)&(Pair){
   272              .key = (GoString){.buf = "TrafficEnv", .len = 10},
   273              .val = &BASE_FIELD_5,
   274          },
   275          .len = 1,
   276          .cap = 1,
   277      },
   278  };
   279  
   280  TrieNode BASE_TN_6 = {
   281      .leaves = &(GoSlice){
   282          .buf = (char *)&(Pair){
   283              .key = (GoString){.buf = "Extra", .len = 5},
   284              .val = &BASE_FIELD_6,
   285          },
   286          .len = 1,
   287          .cap = 1,
   288      },
   289  };
   290  
   291  #define INIT_BASE_DESC()                                                     \
   292      do                                                                       \
   293      {                                                                        \
   294          tFieldDesc **p0 = (tFieldDesc **)BASE_DESC.st->ids.buf;              \
   295          HashMap *hm = BASE_DESC.st->names.hash;                              \
   296          p0[1] = &BASE_FIELD_1;                                               \
   297          hm_set(hm, &(p0[1]->name), p0[1]);                                   \
   298          p0[2] = &BASE_FIELD_2;                                               \
   299          hm_set(hm, &(p0[2]->name), p0[2]);                                   \
   300          p0[3] = &BASE_FIELD_3;                                               \
   301          hm_set(hm, &(p0[3]->name), p0[3]);                                   \
   302          p0[4] = &BASE_FIELD_4;                                               \
   303          hm_set(hm, &(p0[4]->name), p0[4]);                                   \
   304          p0[5] = &BASE_FIELD_5;                                               \
   305          hm_set(hm, &(p0[5]->name), p0[5]);                                   \
   306          p0[6] = &BASE_FIELD_6;                                               \
   307          hm_set(hm, &(p0[6]->name), p0[6]);                                   \
   308          BASE_DESC.st->names.hash = NULL;                                     \
   309          TrieNode *p1 = (TrieNode *)BASE_DESC.st->names.trie->node.index.buf; \
   310          p1['o' - CHAR_POINT] = BASE_TN_1;                                    \
   311          p1['a' - CHAR_POINT] = BASE_TN_2;                                    \
   312          p1['d' - CHAR_POINT] = BASE_TN_3;                                    \
   313          p1['l' - CHAR_POINT] = BASE_TN_4;                                    \
   314          p1['r' - CHAR_POINT] = BASE_TN_5;                                    \
   315          p1['x' - CHAR_POINT] = BASE_TN_6;                                    \
   316      } while (0)
   317  
   318  #define INIT_EXAMPLE_DESC()                                                     \
   319      do                                                                          \
   320      {                                                                           \
   321          tFieldDesc **p0 = (tFieldDesc **)EXAMPLE_DESC.st->ids.buf;              \
   322          HashMap *hm = EXAMPLE_DESC.st->names.hash;                              \
   323          p0[1] = &EXAMPLE_FIELD_1;                                               \
   324          hm_set(hm, &(p0[1]->name), p0[1]);                                      \
   325          p0[3] = &EXAMPLE_FIELD_3;                                               \
   326          hm_set(hm, &(p0[3]->name), p0[3]);                                      \
   327          p0[255] = &EXAMPLE_FIELD_255;                                           \
   328          hm_set(hm, &(p0[255]->name), p0[255]);                                  \
   329          EXAMPLE_DESC.st->names.hash = NULL;                                     \
   330          TrieNode *p1 = (TrieNode *)EXAMPLE_DESC.st->names.trie->node.index.buf; \
   331          p1['M' - CHAR_POINT] = EXAMPLE_TN_1;                                    \
   332          p1['I' - CHAR_POINT] = EXAMPLE_TN_3;                                    \
   333          p1['B' - CHAR_POINT] = EXAMPLE_TN_255;                                  \
   334      } while (0)
   335  
   336  #endif // SCANNING_H