-
Notifications
You must be signed in to change notification settings - Fork 53
Add schema builders to Unity #81
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
53f3ff4
4e0c4f3
d0cdaad
a7f185e
f9406db
edadf09
66fb899
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace NeuroSdk.Json.Builders | ||
| { | ||
| public sealed class ArrayBuilder : SchemaBuilder<IEnumerator<object>> | ||
| { | ||
| public ArrayBuilder() | ||
| { | ||
| Schema.Type = JsonSchemaType.Array; | ||
| } | ||
|
|
||
| public ArrayBuilder Items<TBuilder>(Func<JsonSchemaBuilders, SchemaBuilder<TBuilder>> build) | ||
| { | ||
| Schema.Items = build(new JsonSchemaBuilders()).Build(); | ||
| return this; | ||
| } | ||
|
|
||
| public ArrayBuilder MinItems(int value) | ||
| { | ||
| Schema.MinItems = value; | ||
| return this; | ||
| } | ||
|
|
||
| public ArrayBuilder MaxItems(int value) | ||
| { | ||
| Schema.MaxItems = value; | ||
| return this; | ||
| } | ||
|
|
||
| public ArrayBuilder UniqueItems(bool value = true) | ||
| { | ||
| Schema.UniqueItems = value; | ||
| return this; | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace NeuroSdk.Json.Builders | ||
| { | ||
| public sealed class BooleanBuilder : SchemaBuilder<bool> | ||
| { | ||
| public BooleanBuilder() | ||
| { | ||
| Schema.Type = JsonSchemaType.Boolean; | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace NeuroSdk.Json.Builders | ||
| { | ||
| public sealed class FloatBuilder : NumberBuilder<FloatBuilder> | ||
| { | ||
| public FloatBuilder() | ||
| { | ||
| Schema.Type = JsonSchemaType.Float; | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace NeuroSdk.Json.Builders | ||
| { | ||
| public sealed class IntegerBuilder : NumberBuilder<IntegerBuilder> | ||
| { | ||
| public IntegerBuilder() | ||
| { | ||
| Schema.Type = JsonSchemaType.Integer; | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| namespace NeuroSdk.Json.Builders | ||
| { | ||
| public interface IRootSchemaBuilders | ||
| { | ||
| ObjectBuilder Object(); | ||
| ArrayBuilder Array(); | ||
| } | ||
|
|
||
| public sealed class JsonSchemaBuilders : IRootSchemaBuilders | ||
| { | ||
| // singleton schizophrenia xd | ||
| public static IRootSchemaBuilders Instance { get; } = new JsonSchemaBuilders(); | ||
|
|
||
| internal JsonSchemaBuilders() {} | ||
|
|
||
| public ObjectBuilder Object() => new(); | ||
| public ArrayBuilder Array() => new(); | ||
| public StringBuilder String() => new(); | ||
| public IntegerBuilder Integer() => new(); | ||
| public FloatBuilder Float() => new(); | ||
| public BooleanBuilder Boolean() => new(); | ||
| public NullBuilder Null() => new(); | ||
| } | ||
|
|
||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace NeuroSdk.Json.Builders | ||
| { | ||
| public sealed class NullBuilder : SchemaBuilder<object> | ||
| { | ||
| public NullBuilder() | ||
| { | ||
| Schema.Type = JsonSchemaType.Null; | ||
| Schema.Const = null; | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| namespace NeuroSdk.Json.Builders | ||
| { | ||
| public abstract class NumberBuilder<TSelf> : SchemaBuilder<TSelf> | ||
| where TSelf : NumberBuilder<TSelf> | ||
| { | ||
| protected TSelf Self => (TSelf)this; | ||
|
|
||
| public TSelf Min(double value) | ||
| { | ||
| Schema.Minimum = (float)value; | ||
| return Self; | ||
| } | ||
|
|
||
| public TSelf Max(double value) | ||
| { | ||
| Schema.Maximum = (float)value; | ||
| return Self; | ||
| } | ||
|
|
||
| public TSelf ExclusiveMin(double value) | ||
| { | ||
| Schema.ExclusiveMinimum = (float)value; | ||
| return Self; | ||
| } | ||
|
|
||
| public TSelf ExclusiveMax(double value) | ||
| { | ||
| Schema.ExclusiveMaximum = (float)value; | ||
| return Self; | ||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace NeuroSdk.Json.Builders | ||
| { | ||
| public sealed class ObjectBuilder : SchemaBuilder<object> | ||
| { | ||
| public ObjectBuilder() | ||
| { | ||
| Schema.Type = JsonSchemaType.Object; | ||
| Schema.Properties = new Dictionary<string, JsonSchema>(); | ||
| } | ||
|
|
||
| public ObjectBuilder Property<TBuilder>( | ||
| string name, | ||
| Func<JsonSchemaBuilders, SchemaBuilder<TBuilder>> build, | ||
| bool required = true) | ||
| { | ||
| var subBuilder = build(new JsonSchemaBuilders()); | ||
| Schema.Properties[name] = subBuilder.Build(); | ||
|
|
||
| if (required) | ||
| Schema.Required.Add(name); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| namespace NeuroSdk.Json.Builders | ||
| { | ||
| public class SchemaBuilder<THolds> | ||
| { | ||
| protected readonly JsonSchema Schema = new(); | ||
|
|
||
| public JsonSchema Build() => Schema; | ||
|
|
||
| public SchemaBuilder<THolds> Const(object value) | ||
| { | ||
| Schema.Const = value; | ||
| return this; | ||
| } | ||
|
|
||
| public SchemaBuilder<THolds> Enum(params THolds[] values) | ||
| { | ||
| Schema.Enum = values.Cast<object>().ToList(); | ||
| return this; | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| namespace NeuroSdk.Json.Builders | ||
| { | ||
| public sealed class StringBuilder : SchemaBuilder<object> | ||
| { | ||
| public StringBuilder() | ||
| { | ||
| Schema.Type = JsonSchemaType.String; | ||
| } | ||
|
|
||
| public StringBuilder MinLength(int value) | ||
| { | ||
| Schema.MinLength = value; | ||
| return this; | ||
| } | ||
|
|
||
| public StringBuilder MaxLength(int value) | ||
| { | ||
| Schema.MaxLength = value; | ||
| return this; | ||
| } | ||
|
|
||
| public StringBuilder Pattern(string regex) | ||
| { | ||
| Schema.Pattern = regex; | ||
| return this; | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.