Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
6.4 KiB

3 years ago
/*
* Copyright (c) 2009-2021, Google LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Google LLC nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "upb/util/required_fields.h"
#include <setjmp.h>
#include <stdarg.h>
#include <stdio.h>
#include "upb/reflection.h"
// Must be last.
#include "upb/port_def.inc"
typedef struct {
upb_FieldPathEntry *path;
3 years ago
size_t size;
size_t cap;
} upb_FieldPathVector;
typedef struct {
upb_FieldPathVector stack;
upb_FieldPathVector out_fields;
3 years ago
const upb_symtab *ext_pool;
jmp_buf err;
bool has_unset_required;
bool save_paths;
} upb_FindContext;
void upb_FieldPathVector_Init(upb_FieldPathVector *vec) {
vec->path = NULL;
vec->size = 0;
vec->cap = 0;
3 years ago
}
void upb_FieldPathVector_Reserve(upb_FindContext *ctx, upb_FieldPathVector *vec,
size_t elems) {
if (vec->cap - vec->size < elems) {
size_t need = vec->size + elems;
vec->cap = UPB_MAX(4, vec->cap);
while (vec->cap < need) vec->cap *= 2;
vec->path = realloc(vec->path, vec->cap * sizeof(*vec->path));
if (!vec->path) { UPB_LONGJMP(ctx->err, 1); }
3 years ago
}
}
void upb_FindContext_Push(upb_FindContext* ctx, upb_FieldPathEntry ent) {
if (!ctx->save_paths) return;
upb_FieldPathVector_Reserve(ctx, &ctx->stack, 1);
ctx->stack.path[ctx->stack.size++] = ent;
}
3 years ago
void upb_FindContext_Pop(upb_FindContext* ctx) {
if (!ctx->save_paths) return;
assert(ctx->stack.size != 0);
ctx->stack.size--;
3 years ago
}
static void upb_util_FindUnsetRequiredInternal(upb_FindContext *ctx,
const upb_msg *msg,
const upb_msgdef *m) {
3 years ago
// OPT: add markers in the schema for where we can avoid iterating:
// 1. messages with no required fields.
// 2. messages that cannot possibly reach any required fields.
// Iterate over all fields to see if any required fields are missing.
3 years ago
for (int i = 0, n = upb_msgdef_fieldcount(m); i < n; i++) {
const upb_fielddef *f = upb_msgdef_field(m, i);
if (upb_fielddef_label(f) != UPB_LABEL_REQUIRED) continue;
3 years ago
if (!upb_msg_has(msg, f)) {
// A required field is missing.
ctx->has_unset_required = true;
// Append the contents of the stack to the out array, then NULL-terminate.
upb_FieldPathVector_Reserve(ctx, &ctx->out_fields, ctx->stack.size + 1);
memcpy(&ctx->out_fields.path[ctx->out_fields.size], &ctx->stack.path,
ctx->stack.size * sizeof(*ctx->stack.path));
ctx->out_fields.size += ctx->stack.size;
ctx->out_fields.path[ctx->out_fields.size++] =
(upb_FieldPathEntry){.field = NULL};
3 years ago
}
}
// Iterate over all present fields to find sub-messages that might be missing
// required fields. This may revisit some of the fields already inspected
// in the previous loop. We do this separately because this loop will also
// find present extensions, which the previous loop will not.
//
// TODO(haberman): consider changing upb_msg_next() to be capable of visiting
// extensions only, for example with a UPB_MSG_BEGINEXT constant.
3 years ago
size_t iter = UPB_MSG_BEGIN;
const upb_fielddef *f;
upb_msgval val;
while (upb_msg_next(msg, m, ctx->ext_pool, &f, &val, &iter)) {
// Skip non-submessage fields.
if (!upb_fielddef_issubmsg(f)) continue;
upb_FindContext_Push(ctx, (upb_FieldPathEntry){.field = f});
3 years ago
const upb_msgdef *sub_m = upb_fielddef_msgsubdef(f);
3 years ago
if (upb_fielddef_ismap(f)) {
// Map field.
3 years ago
const upb_fielddef *val_f = upb_msgdef_field(sub_m, 1);
const upb_msgdef *val_m = upb_fielddef_msgsubdef(val_f);
if (!val_m) continue;
const upb_map *map = val.map_val;
size_t iter = UPB_MAP_BEGIN;
while (upb_mapiter_next(map, &iter)) {
upb_msgval key = upb_mapiter_value(map, iter);
3 years ago
upb_msgval map_val = upb_mapiter_value(map, iter);
upb_FindContext_Push(ctx, (upb_FieldPathEntry){.key = key});
upb_util_FindUnsetRequiredInternal(ctx, map_val.msg_val, val_m);
upb_FindContext_Pop(ctx);
3 years ago
}
} else if (upb_fielddef_isseq(f)) {
// Repeated field.
3 years ago
const upb_array *arr = val.array_val;
for (size_t i = 0, n = upb_array_size(arr); i < n; i++) {
upb_msgval elem = upb_array_get(arr, i);
upb_FindContext_Push(ctx, (upb_FieldPathEntry){.index = i});
upb_util_FindUnsetRequiredInternal(ctx, elem.msg_val, sub_m);
upb_FindContext_Pop(ctx);
3 years ago
}
} else {
// Scalar field.
upb_util_FindUnsetRequiredInternal(ctx, val.msg_val, sub_m);
3 years ago
}
upb_FindContext_Pop(ctx);
}
3 years ago
}
bool upb_util_HasUnsetRequired(const upb_msg* msg, const upb_msgdef* m,
const upb_symtab* ext_pool,
upb_FieldPathEntry** fields) {
upb_FindContext ctx;
ctx.ext_pool = ext_pool;
upb_FieldPathVector_Init(&ctx.stack);
upb_FieldPathVector_Init(&ctx.out_fields);
upb_util_FindUnsetRequiredInternal(&ctx, msg, m);
free(ctx.stack.path);
if (fields) *fields = ctx.out_fields.path;
return ctx.has_unset_required;
3 years ago
}