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

fix(extensions): allow multiple decorators #352

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Core/Generators/GeneratorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,11 @@ private static void WriteDecorators(Utf8JsonWriter writer, List<SchemaDecorator>
{
return;
}
writer.WriteStartObject("decorators");
writer.WriteStartArray("decorators");
foreach (var decorator in decorators)
{
writer.WriteStartObject(decorator.Identifier);
writer.WriteStartObject();
writer.WriteString("identifier", decorator.Identifier);
if (decorator.Arguments.Count > 0)
{
writer.WriteStartObject("arguments");
Expand All @@ -368,7 +369,7 @@ private static void WriteDecorators(Utf8JsonWriter writer, List<SchemaDecorator>
}
writer.WriteEndObject();
}
writer.WriteEndObject();
writer.WriteEndArray();
}

private static string GetDefinitionKind(Definition def)
Expand Down
2 changes: 1 addition & 1 deletion Runtime/TypeScript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@
"vitest": "^0.32.2"
},
"dependencies": {}
}
}
2 changes: 0 additions & 2 deletions docs/src/content/docs/chords/chordc.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Here's the updated content with a caution note added at the top:

---
title: chordc
---
Expand Down
4 changes: 2 additions & 2 deletions extensions/edks/tinygo/chord.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
}
},
"engine": {
"bebopc": "^3.0.0"
"bebopc": "^3.1.1"
},
"readme": "README.md"
}
}
38 changes: 19 additions & 19 deletions extensions/edks/tinygo/kernel/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func deserializeStruct(json gjson.Result) StructDefinition {
},
Fields: deserializeFields(json.Get("fields")),
},
IsReadOnly: json.Get("readonly").Bool(),
Mutable: json.Get("mutable").Bool(),
IsFixedSize: json.Get("isFixedSize").Bool(),
}
}
Expand Down Expand Up @@ -283,27 +283,26 @@ func deserializeEnumMember(json gjson.Result) EnumMember {
}
}

func deserializeDecorators(json gjson.Result) map[string]Decorator {
decorators := make(map[string]Decorator)
json.ForEach(func(key, value gjson.Result) bool {
decorators[key.String()] = deserializeDecorator(value)
func deserializeDecorators(json gjson.Result) []Decorator {
var decorators []Decorator
json.ForEach(func(_, value gjson.Result) bool {
decorators = append(decorators, deserializeDecorator(value))
return true
})
return decorators
}

func deserializeDecorator(json gjson.Result) Decorator {
decorator := Decorator{
Arguments: make(map[string]DecoratorArgument),
Identifier: json.Get("identifier").String(),
Arguments: make(map[string]DecoratorArgument),
}
arguments := json.Get("arguments")
if !arguments.Exists() {
return decorator
if arguments.Exists() {
arguments.ForEach(func(key, value gjson.Result) bool {
decorator.Arguments[key.String()] = deserializeDecoratorArgument(value)
return true
})
}
arguments.ForEach(func(key, value gjson.Result) bool {
decorator.Arguments[key.String()] = deserializeDecoratorArgument(value)
return true
})
return decorator
}

Expand Down Expand Up @@ -417,7 +416,8 @@ func (m MapType) IsBaseType() bool {
}

type Decorator struct {
Arguments map[string]DecoratorArgument
Identifier string
Arguments map[string]DecoratorArgument
}

type DecoratorArgument struct {
Expand All @@ -432,7 +432,7 @@ type Definition interface {
type BaseDefinition struct {
Kind string
Documentation string
Decorators map[string]Decorator
Decorators []Decorator
Parent string
}

Expand All @@ -449,7 +449,7 @@ type FieldsDefinition struct {

type StructDefinition struct {
FieldsDefinition
IsReadOnly bool
Mutable bool
IsFixedSize bool
}

Expand All @@ -474,7 +474,7 @@ func (ed EnumDefinition) Kind() string { return "enum" }
type EnumMember struct {
Documentation string
Value string
Decorators map[string]Decorator
Decorators []Decorator
}

type UnionDefinition struct {
Expand All @@ -491,7 +491,7 @@ type ServiceDefinition struct {

type MethodDefinition struct {
Documentation string
Decorators map[string]Decorator
Decorators []Decorator
Type string
RequestType string
ResponseType string
Expand All @@ -505,7 +505,7 @@ type ConstantDefinition struct {

type Field struct {
Documentation string
Decorators map[string]Decorator
Decorators []Decorator
Type FieldType
Index int
}
Expand Down
2 changes: 1 addition & 1 deletion extensions/edks/typescript/chord.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
}
},
"engine": {
"bebopc": "^3.0.0"
"bebopc": "^3.1.1"
},
"readme": "README.md"
}
29 changes: 17 additions & 12 deletions extensions/edks/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,36 @@ function generateValidators(context: CompilerContext): string {
const builder = new IndentedStringBuilder();

for (const [name, definition] of Object.entries(context.definitions)) {
if (definition.kind === "struct" && definition.decorators?.validate) {
if (definition.kind === "struct" && definition.decorators?.some(d => d.identifier === "validator")) {
builder.appendLine(`export function validate${name}(record: I${name}): void {`);
builder.indent(2);

for (const [fieldName, field] of Object.entries(definition.fields)) {
if (field.type === 'string' && field.decorators?.length?.arguments) {
const { min, max } = field.decorators.length.arguments;
if (field.type === 'string') {
const lengthDecorator = field.decorators?.find(d => d.identifier === "length");
if (lengthDecorator && lengthDecorator.arguments) {
const { min, max } = lengthDecorator.arguments;

if (min?.value !== undefined) {
builder.appendLine(`if (record.${fieldName}.length < ${min.value}) {`);
builder.appendLine(` throw new Error(\`${fieldName} must be at least ${min.value} characters long\`);`);
builder.appendLine('}');
}
if (min && min.value !== undefined) {
builder.appendLine(`if (record.${fieldName}.length < ${min.value}) {`);
builder.appendLine(` throw new Error(\`${fieldName} must be at least ${min.value} characters long\`);`);
builder.appendLine('}');
}

if (max?.value !== undefined) {
builder.appendLine(`if (record.${fieldName}.length > ${max.value}) {`);
builder.appendLine(` throw new Error(\`${fieldName} must be no more than ${max.value} characters long\`);`);
builder.appendLine('}');
if (max && max.value !== undefined) {
builder.appendLine(`if (record.${fieldName}.length > ${max.value}) {`);
builder.appendLine(` throw new Error(\`${fieldName} must be no more than ${max.value} characters long\`);`);
builder.appendLine('}');
}
}
}
}

builder.dedent(2);
builder.appendLine('}');
builder.appendLine();
}
}

return builder.toString();
}
23 changes: 12 additions & 11 deletions extensions/edks/typescript/src/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,15 @@ export type BaseType = "uint8" | "uint16" | "uint32" | "uint64" | "int8" | "int1
export type Kind = "enum" | "struct" | "message" | "union" | "service" | "const";


export type DecoratorArgument = {
type: BaseType;
value: string;
};

export type Decorator = {
[key: string]: {
arguments?: {
[key: string]: {
type: BaseType;
value: string;
};
};
identifier: string;
arguments?: {
[key: string]: DecoratorArgument;
};
};

Expand All @@ -209,7 +210,7 @@ export type MapType = {

export type Field<T extends BaseType | "array" | "map"> = {
documentation?: string;
decorators?: Decorator;
decorators?: Decorator[];
type: T;
index?: number;
array?: T extends "array" ? ArrayType : never;
Expand All @@ -218,14 +219,14 @@ export type Field<T extends BaseType | "array" | "map"> = {

export type EnumMember = {
documentation?: string;
decorators?: Decorator;
decorators?: Decorator[];
value: string;
};

export type BaseDefinition<K extends Kind> = {
kind: K;
documentation?: string;
decorators?: Decorator;
decorators?: Decorator[];
minimalEncodedSize: number;
discriminatorInParent?: number;
parent?: string;
Expand Down Expand Up @@ -260,7 +261,7 @@ export type UnionDefinition = BaseDefinition<"union"> & {
};

export type Method = {
decorators?: Decorator;
decorators?: Decorator[];
documentation?: string;
type: "Unary" | "DuplexStream" | "ClientStream" | "ServerStream";
requestType: string;
Expand Down
Loading