Skip to content

Commit

Permalink
Updated following
Browse files Browse the repository at this point in the history
1. Mutable byte arrays use aws_byte_buf
2. clang-tidy and clang-format integration
  • Loading branch information
blacktooth committed Aug 3, 2022
1 parent 939148f commit d994043
Show file tree
Hide file tree
Showing 19 changed files with 250 additions and 111 deletions.
57 changes: 57 additions & 0 deletions native-schema-registry/c/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
Language: Cpp
# BasedOnStyle: Mozilla
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
BinPackArguments: false
BinPackParameters: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakStringLiterals: true
ColumnLimit: 120
ContinuationIndentWidth: 4
DerivePointerAlignment: false
IncludeBlocks: Preserve
IndentCaseLabels: true
IndentPPDirectives: AfterHash
IndentWidth: 4
IndentWrappedFunctionNames: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 100000
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
...
2 changes: 1 addition & 1 deletion native-schema-registry/c/.clang-tidy
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
Checks: 'clang-diagnostic-*,clang-analyzer-*,readability-*,modernize-*,bugprone-*,misc-*,google-runtime-int,llvm-header-guard,fuchsia-restrict-system-includes,-clang-analyzer-valist.Uninitialized,-clang-analyzer-security.insecureAPI.rand,-clang-analyzer-alpha.*,-readability-magic-numbers,-readability-non-const-parameter,-readability-avoid-const-params-in-decls,-readability-else-after-return,-readability-isolate-declaration,-readability-uppercase-literal-suffix,-bugprone-sizeof-expression'
WarningsAsErrors: '*'
HeaderFilterRegex: '.*\.[h]$'
HeaderFilterRegex: '(./c/src/.*\.c$)|(./c/include/.*\.h$)'
FormatStyle: 'file'
CheckOptions:
- key: readability-braces-around-statements.ShortStatementLines
Expand Down
1 change: 1 addition & 0 deletions native-schema-registry/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ set(NATIVE_SCHEMA_REGISTRY_MODULE_NAME libnativeschemaregistry)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

include_directories("include")
include(cmake/FetchAwsCommon.cmake)
include_directories(${LIB_NATIVE_SCHEMA_REGISTRY_PATH})

add_subdirectory("src")
Expand Down
12 changes: 12 additions & 0 deletions native-schema-registry/c/cmake/FetchAwsCommon.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
include(FetchContent)

set(AWS_C_COMMON aws-c-common)

FetchContent_Declare(
AWS_C_COMMON
GIT_REPOSITORY https://github.com/awslabs/aws-c-common.git
GIT_TAG v0.7.4
GIT_SHALLOW 1
)

FetchContent_MakeAvailable(AWS_C_COMMON)
24 changes: 24 additions & 0 deletions native-schema-registry/c/format-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

if [[ -z $CLANG_FORMAT ]] ; then
CLANG_FORMAT=clang-format
fi

if NOT type $CLANG_FORMAT 2> /dev/null ; then
echo "No appropriate clang-format found."
exit 1
fi

FAIL=0
SOURCE_FILES=`find src include -type f \( -name '*.h' -o -name '*.c' \)`
for i in $SOURCE_FILES
do
$CLANG_FORMAT -output-replacements-xml $i | grep -c "<replacement " > /dev/null
if [ $? -ne 1 ]
then
echo "$i failed clang-format check."
FAIL=1
fi
done

exit $FAIL
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
#ifndef GLUE_SCHEMA_REGISTRY_DESERIALIZER_H
#define GLUE_SCHEMA_REGISTRY_DESERIALIZER_H

#include "glue_schema_registry_schema.h"
#include "glue_schema_registry_error.h"
#include "glue_schema_registry_schema.h"
#include "mutable_byte_array.h"
#include "read_only_byte_array.h"
#include <stdbool.h>

typedef struct glue_schema_registry_deserializer {
//This is used for storing the instance context. Currently, being used for managing GraalVM instance.
/* This is used for storing the instance context. Currently, being used for
* managing GraalVM instance. */
void *instance_context;
} glue_schema_registry_deserializer;

glue_schema_registry_deserializer *new_glue_schema_registry_deserializer(glue_schema_registry_error **p_err);

void delete_glue_schema_registry_deserializer(glue_schema_registry_deserializer *deserializer);

mutable_byte_array *glue_schema_registry_deserializer_decode(glue_schema_registry_deserializer *deserializer,
read_only_byte_array *array,
glue_schema_registry_error **p_err);
mutable_byte_array *glue_schema_registry_deserializer_decode(
const glue_schema_registry_deserializer *deserializer,
const read_only_byte_array *array,
glue_schema_registry_error **p_err);

glue_schema_registry_schema *
glue_schema_registry_deserializer_decode_schema(glue_schema_registry_deserializer *deserializer,
read_only_byte_array *array,
glue_schema_registry_error **p_err);
glue_schema_registry_schema *glue_schema_registry_deserializer_decode_schema(
const glue_schema_registry_deserializer *deserializer,
const read_only_byte_array *array,
glue_schema_registry_error **p_err);

bool glue_schema_registry_deserializer_can_decode(glue_schema_registry_deserializer *deserializer,
read_only_byte_array *array,
glue_schema_registry_error **p_err);
bool glue_schema_registry_deserializer_can_decode(
const glue_schema_registry_deserializer *deserializer,
const read_only_byte_array *array,
glue_schema_registry_error **p_err);

#endif //GLUE_SCHEMA_REGISTRY_DESERIALIZER_H
#endif /* GLUE_SCHEMA_REGISTRY_DESERIALIZER_H */
44 changes: 23 additions & 21 deletions native-schema-registry/c/include/glue_schema_registry_error.h
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
#ifndef NATIVE_SCHEMA_REGISTRY_GLUE_SCHEMA_REGISTRY_ERROR_H
#define NATIVE_SCHEMA_REGISTRY_GLUE_SCHEMA_REGISTRY_ERROR_H
#ifndef GLUE_SCHEMA_REGISTRY_ERROR_H
#define GLUE_SCHEMA_REGISTRY_ERROR_H

#include <stdio.h>

//Error codes are arbitrarily listed from 5000. No specific reason.
#define ERR_CODE_INVALID_STATE 5000
#define ERR_CODE_NULL_PARAMETERS 5001
#define ERR_CODE_GRAALVM_INIT_EXCEPTION 5002
#define ERR_CODE_GRAALVM_TEARDOWN_EXCEPTION 5003
#define ERR_CODE_INVALID_PARAMETERS 5004
#define ERR_CODE_RUNTIME_ERROR 5005

//TODO: Improve error reporting to respect logging levels.
/* Error codes are arbitrarily listed from 5000. No specific reason. */
enum aws_gsr_error_code {
AWS_GSR_ERR_CODE_INVALID_STATE = 5000,
AWS_GSR_ERR_CODE_NULL_PARAMETERS = 5001,
AWS_GSR_ERR_CODE_GRAALVM_INIT_EXCEPTION = 5002,
AWS_GSR_ERR_CODE_GRAALVM_TEARDOWN_EXCEPTION = 5003,
AWS_GSR_ERR_CODE_INVALID_PARAMETERS = 5004,
AWS_GSR_ERR_CODE_RUNTIME_ERROR = 5005
};

/* TODO: Improve error reporting to respect logging levels. */
#define log_warn(msg, code) fprintf(stderr, "WARN: %s, Code: %d\n", msg, code)

#define MAX_ERROR_MSG_LEN 10000
#define AWS_GSR_MAX_ERROR_MSG_LEN 10000

/** Defines the glue_schema_registry_error structure for holding error messages and codes
* resulting from function executions.
/** Defines the glue_schema_registry_error structure for holding error messages
* and codes resulting from function executions.
*/
typedef struct glue_schema_registry_error {
char * msg;
char *msg;
int code;
} glue_schema_registry_error;

glue_schema_registry_error * new_glue_schema_registry_error(const char * err_msg, int err_code);
glue_schema_registry_error *new_glue_schema_registry_error(const char *err_msg, int err_code);

void delete_glue_schema_registry_error(glue_schema_registry_error *error);

//Copies the given error's msg into dst array trimming the size as necessary.
void glue_schema_registry_error_get_msg(glue_schema_registry_error *error, char *dst, size_t len);
/* Copies the given error's msg into dst array trimming the size as necessary. */
void glue_schema_registry_error_get_msg(const glue_schema_registry_error *error, char *dst, size_t len);

/**
* Creates an instance of glue_schema_registry_error and writes it to the given
* glue_schema_registry_error pointer holder (*p_err). It is expected that *p_err
* is initialized by caller.
* glue_schema_registry_error pointer holder (*p_err). It is expected that
* *p_err is initialized by caller.
* @param p_err Initialized glue_schema_registry_error pointer holder.
* @param msg Error message to write.
* @param code Non-zero error code.
Expand All @@ -51,4 +53,4 @@ glue_schema_registry_error **new_glue_schema_registry_error_holder(void);
*/
void delete_glue_schema_registry_error_holder(glue_schema_registry_error **p_err);

#endif //NATIVE_SCHEMA_REGISTRY_GLUE_SCHEMA_REGISTRY_ERROR_H
#endif /* GLUE_SCHEMA_REGISTRY_ERROR_H */
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef NATIVE_SCHEMA_REGISTRY_MEMORY_ALLOCATOR_H
#define NATIVE_SCHEMA_REGISTRY_MEMORY_ALLOCATOR_H
#ifndef GLUE_SCHEMA_REGISTRY_MEMORY_ALLOCATOR_H
#define GLUE_SCHEMA_REGISTRY_MEMORY_ALLOCATOR_H

#include <stdlib.h>

Expand All @@ -13,4 +13,4 @@ void *aws_common_calloc(size_t count, size_t size);

void aws_common_free(void *ptr);

#endif //NATIVE_SCHEMA_REGISTRY_MEMORY_ALLOCATOR_H
#endif /* GLUE_SCHEMA_REGISTRY_MEMORY_ALLOCATOR_H */
46 changes: 26 additions & 20 deletions native-schema-registry/c/include/glue_schema_registry_schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,40 @@
* schema object required by Glue Schema Registry Serializers / De-serializers.
*/
typedef struct glue_schema_registry_schema {
//String name of the schema
char * schema_name;
/* String name of the schema */
char *schema_name;

//Complete definition of the schema as String
char * schema_def;
/* Complete definition of the schema as String */
char *schema_def;

//Data format name, JSON, AVRO, PROTOBUF as String
char * data_format;
/* Data format name, JSON, AVRO, PROTOBUF as String */
char *data_format;

} glue_schema_registry_schema;

//Creates a new instance of glue_schema_registry_schema
/*
* Creates a new instance of glue_schema_registry_schema
*/
glue_schema_registry_schema *new_glue_schema_registry_schema(
const char * schema_name,
const char * schema_def,
const char * data_format,
glue_schema_registry_error ** p_err
);
const char *schema_name,
const char *schema_def,
const char *data_format,
glue_schema_registry_error **p_err);

//Deletes the glue schema registry schema.
void delete_glue_schema_registry_schema(glue_schema_registry_schema * schema);
/*
* Deletes the glue schema registry schema.
*/
void delete_glue_schema_registry_schema(glue_schema_registry_schema *schema);

//Gets different attributes from glue_schema_registry_schema instance.
//These getter methods are translated into "Getter" methods in target languages.
const char * glue_schema_registry_schema_get_schema_name(glue_schema_registry_schema * schema);
/*
* Gets different attributes from glue_schema_registry_schema instance.
* These getter methods are translated into "Getter" methods in target
* languages.
*/
const char *glue_schema_registry_schema_get_schema_name(const glue_schema_registry_schema *schema);

const char * glue_schema_registry_schema_get_schema_def(glue_schema_registry_schema * schema);
const char *glue_schema_registry_schema_get_schema_def(const glue_schema_registry_schema *schema);

const char * glue_schema_registry_schema_get_data_format(glue_schema_registry_schema * schema);
const char *glue_schema_registry_schema_get_data_format(const glue_schema_registry_schema *schema);

#endif //GLUE_SCHEMA_REGISTRY_SCHEMA_H
#endif /* GLUE_SCHEMA_REGISTRY_SCHEMA_H */
20 changes: 11 additions & 9 deletions native-schema-registry/c/include/glue_schema_registry_serializer.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
#ifndef GLUE_SCHEMA_REGISTRY_SERIALIZER_H
#define GLUE_SCHEMA_REGISTRY_SERIALIZER_H

#include "glue_schema_registry_schema.h"
#include "glue_schema_registry_error.h"
#include "glue_schema_registry_schema.h"
#include "mutable_byte_array.h"
#include "read_only_byte_array.h"

typedef struct glue_schema_registry_serializer {
//This is used for storing the instance context. Currently being used for managing GraalVM instance.
/* This is used for storing the instance context. Currently being used for
* managing GraalVM instance. */
void *instance_context;
} glue_schema_registry_serializer;

glue_schema_registry_serializer *new_glue_schema_registry_serializer(glue_schema_registry_error **p_err);

void delete_glue_schema_registry_serializer(glue_schema_registry_serializer *serializer);

//Encodes the GSR Schema with a byte array.
mutable_byte_array *glue_schema_registry_serializer_encode(glue_schema_registry_serializer *serializer,
read_only_byte_array * array,
const char * transport_name,
glue_schema_registry_schema *gsr_schema,
glue_schema_registry_error **p_err);
// Encodes the GSR Schema with a byte array.
mutable_byte_array *glue_schema_registry_serializer_encode(
const glue_schema_registry_serializer *serializer,
const read_only_byte_array *array,
const char *transport_name,
const glue_schema_registry_schema *gsr_schema,
glue_schema_registry_error **p_err);

#endif //GLUE_SCHEMA_REGISTRY_SERIALIZER_H
#endif /* GLUE_SCHEMA_REGISTRY_SERIALIZER_H */
Loading

0 comments on commit d994043

Please sign in to comment.