grammar

package
v0.2.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Bool   = "bool"
	Int    = "int"
	Float  = "float"
	String = "string"
	Type   = "type"
	Any    = "any"
)

Variables

View Source
var Builtin = Grammar{

	Package: Package{
		Annotations: Annotations{base_next("package", lang_package(), lang_imports())},
	},
	Const: Const{
		Annotations: Annotations{base_next("const"), deprecated()},
		Types:       validConstTypes,
	},
	Enum: Enum{
		Annotations: Annotations{base_next("enum", type_()), deprecated()},
		Member: EnumMember{
			Annotations: Annotations{base_next("enum.member"), deprecated()},
			Types:       validEnumMemberTypes,
		},
	},
	Struct: Struct{
		Annotations: Annotations{base_next("struct", lang_alias()), deprecated()},
		Field: StructField{
			Annotations: Annotations{base_next("struct.field", lang_default(), default_(), lang_alias()), optional(), deprecated()},
		},
	},
	Interface: Interface{
		Annotations: Annotations{base_next("interface", prompt(), lang_alias()), deprecated()},
		Method: InterfaceMethod{
			Annotations: Annotations{base_next("interface.method", prompt(), lang_alias(), mut(), error_()), deprecated()},
			Parameter: InterfaceMethodParameter{
				Annotations: Annotations{next("interface.method.parameter", tokens(), mut(), lang_alias())},
			},
		},
	},
	// contains filtered or unexported fields
}

@api(Grammar/Builtin) represents the built-in grammar rules. Here is an example source code for demonstration built-in grammar rules:

```next
@next(
	// available is used to set the available expression for the file.
	available="cpp|java",
	// tokens is used to set the space separated tokens for the declaration name.
	tokens="DB",
	// <LANG>_package is used to set the package name for target languages.
	cpp_package="db",
	java_package="com.example.db",
	// <LANG>_imports is used to set the import declarations for target languages.
	cpp_imports="<iostream>",
)
package db;

@next(
	available="cpp",
	tokens="HTTP Version",
)
@deprecated(since="2.0", message="Use HTTPVersion2 instead.")
const int HTTPVersion = 2;

@next(
	available="cpp|java",
	tokens="HTTP Method",
	// <LANG>_alias is used to set the alias name for target languages.
	java_alias="org.springframework.http.HttpMethod",
)
@deprecated(since="2.0", message="Use HTTPMethod2 instead.")
enum HTTPMethod {
	Get = "GET",
	Post = "POST",
	Put = "PUT",
	Delete = "DELETE",
	Head = "HEAD",
	Options = "OPTIONS",
	Patch = "PATCH",
	Trace = "TRACE",
	@next(available="!java", tokens="Connect")
	@deprecated(since="2.0", message="Use Connect2 instead.")
	Connect = "CONNECT",
}

@next(
	available="cpp|java",
	tokens="User",
	cpp_alias="model::User",
)
@deprecated(since="2.0", message="Use User2 instead.")
struct User {
	int id;
	@next(
		available="!java",
		tokens="Name",
		default="John Doe",
		cpp_default="\"John Doe\"",
		cpp_alias="std::string",
	)
	@optional
	@deprecated(since="2.0", message="Use Name2 instead.")
	string name;
}

@next(
	available="cpp|java",
	tokens="User Repository",
	prompt="Prompt for AGI.",
	java_alias="org.springframework.data.repository.CrudRepository<User, Long>",
)
@deprecated(since="2.0", message="Use UserRepository2 instead.")
interface UserRepository {
	@next(
		available="!java",
		tokens="Get User",
		mut,
		error,
		cpp_alias="std::shared_ptr<model::User>",
		prompt="Prompt for AGI.",
	)
	@deprecated(since="2.0", message="Use GetUser2 instead.")
	findById(
		@next(available="!java", tokens="ID", mut, cpp_alias="int64_t")
		int64 id
	) User;
}
```

Functions

This section is empty.

Types

type Annotation

type Annotation struct {
	// @api(Grammar/Common/Annotation.Name) represents the annotation name.
	Name string `json:"name"`

	// @api(Grammar/Common/Annotation.Description) represents the annotation description.
	Description string `json:"description"`

	// @api(Grammar/Common/Annotation.Validators) represents the [Validator](#Grammar/Common/Validator) for the annotation.
	Validators Validators `json:"validators"`

	// @api(Grammar/Common/Annotation.Parameters) represents the annotation parameters.
	Parameters []Options[AnnotationParameter] `json:"parameters"`
}

@api(Grammar/Common/Annotation) represents the annotation grammar rules. Only the built-in annotations are supported if no annotations are defined in the grammar.

Example:

<Tabs
	defaultValue="yaml"
	values={[
		{label: 'YAML', value: 'yaml'},
		{label: 'JSON', value: 'json'},
	]}
>
<TabItem value="json">
```json
{
  "struct": {
    "annotations": [
      {
        "name": "message",
        "description": "Sets the struct as a message.",
        "parameters": [
          {
            "name": "type",
            "description": "Sets the message type id.",
            "type": "int",
            "required": true
            "validators": [
              {
                "name": "MessageTypeMustBePositive",
                "expression": "{{gt . 0}}",
                "message": "message type must be positive"
              }
            ]
          }
        ]
      }
    ]
  }
}
```
</TabItem>
<TabItem value="yaml">
```yaml
struct:
  annotations:
    - name: message
      description: Sets the struct as a message.
      parameters:
        - name: type
          description: Sets the message type id.
          types: [int]
          required: true
          validators:
            - name: MessageTypeMustBePositive
              expression: "{{gt . 0}}"
              message: message type must be positive
```
</TabItem>
</Tabs>

```next
package demo;

// Good
@message(type=1)
struct User {
  int id;
  string name;
}

// This will error
@message
struct User {
  int id;
  string name;
}
// Error: message type is required

// This will error
@message(type)
struct User {
  int id;
  string name;
}
// Error: message type must be an integer

// This will error
@message(type=-1)
struct User {
  int id;
  string name;
}
// Error: message type must be positive
```

func LookupAnnotation

func LookupAnnotation(annotations Annotations, name string) *Annotation

type AnnotationParameter

type AnnotationParameter struct {
	// @api(Grammar/Common/AnnotationParameter.Name) represents the parameter name pattern.
	// The name pattern is a regular expression that can be used to match the annotation parameter name.
	//
	// Example:
	//
	// - "**type**": matches the annotation name `type`
	// - "**x|y**": matches the annotation name `x` or `y`
	// - "**.+_package**": matches the annotation name that ends with `_package`, for example, `cpp_package`, `java_package`, etc.
	Name string `json:"name"`

	// @api(Grammar/Common/AnnotationParameter.Description) represents the parameter description.
	Description string `json:"description"`

	// @api(Grammar/Common/AnnotationParameter.Types) represents the parameter type.
	// The type is a string that can be one of the following types:
	//
	// - **bool**: boolean type, the value can be `true` or `false`
	// - **int**: integer type, the value can be a positive or negative integer, for example, `123`
	// - **float**: float type, the value can be a positive or negative float, for example, `1.23`
	// - **string**: string type, the value can be a string, for example, `"hello"`
	// - **type**: any type name, for example, `int`, `float`, `string`, etc. Custom type names are supported.
	Types []string `json:"types"`

	// @api(Grammar/Common/AnnotationParameter.Required) represents the parameter is required or not.
	Required bool `json:"required"`

	// @api(Grammar/Common/AnnotationParameter.Validators) represents the [Validator](#Grammar/Common/Validator) for the annotation parameter.
	Validators Validators `json:"validators"`
	// contains filtered or unexported fields
}

@api(Grammar/Common/AnnotationParameter) represents the annotation parameter grammar rules. If no parameters are defined, the annotation does not have any parameters.

func LookupAnnotationParameter

func LookupAnnotationParameter(parameters []Options[AnnotationParameter], name string) *AnnotationParameter

type Annotations

type Annotations []Options[Annotation]

@api(Grammar/Common/Annotations) represents a list of annotations.

type Const

type Const struct {
	// @api(Grammar/Const.Disabled) represents the const declaration is off or not.
	// If the const declaration is off, the const declaration is not allowed in the next files.
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "const": {
	//	    "disabled": true
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	const:
	//	  disabled: true
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	package demo;
	//
	//	// This will error
	//	const x = 1;
	//	// Error: const declaration is not allowed
	//	“`
	Disabled bool `json:"disabled"`

	// @api(Grammar/Const.Types) represents a list of type names that are supported in the const declaration.
	// If no types are defined, the const declaration supports all types. Otherwise, the const declaration
	// only supports the specified types.
	//
	// Currently, each type name can be one of the following types:
	//
	// - **bool**: boolean type, the value can be `true` or `false`
	// - **int**: integer type, the value can be a positive or negative integer, for example, `123`
	// - **float**: float type, the value can be a positive or negative float, for example, `1.23`
	// - **string**: string type, the value can be a string, for example, `"hello"`
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "const": {
	//	    "types": ["int", "float"]
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	const:
	//	  types:
	//	    - int
	//	    - float
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	package demo;
	//
	//	const x = 1; // Good
	//	const y = 1.23; // Good
	//
	//	// This will error
	//	const z = "hello";
	//	// Error: string type is not allowed in the const declaration
	//	“`
	Types []string `json:"types"`

	// @api(Grammar/Const.Annotations) represents the [Annotation](#Grammar/Common/Annotation) grammar rules for the const declaration.
	Annotations Annotations `json:"annotations"`

	// @api(Grammar/Const.Validators) represents the [Validator](#Grammar/Common/Validator) for the const declaration.
	// It's used to validate the const name. You can access the const name by `.Name`.
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "const": {
	//	    "validators": [
	//	      {
	//	        "name": "ConstNameMustBeCapitalized",
	//	        "expression": "{{eq .Name (.Name | capitalize)}}",
	//	        "message": "const name must be capitalized"
	//	      }
	//	    ]
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	const:
	//	  validators:
	//	    - name: ConstNameMustBeCapitalized
	//	      expression: "{{eq .Name (.Name | capitalize)}}"
	//	      message: const name must be capitalized
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	package demo;
	//
	//	const Hello = 1; // Good
	//
	//	// This will error
	//	const world = 1;
	//	// Error: const name must be capitalized, expected: World
	//	“`
	Validators Validators `json:"validators"`
}

@api(Grammar/Const) represents the grammar rules for the const declaration.

type Context

type Context struct {
	Annotations          map[string]json.RawMessage `json:"annotations"`
	AnnotationParameters map[string]json.RawMessage `json:"annotation_parameters"`
	Validators           map[string]json.RawMessage `json:"validators"`
}

Context represents the contextual data by the key-value pair. The key is a string and the value is a JSON object.

type Enum

type Enum struct {
	// @api(Grammar/Enum.Disabled) represents the enum declaration is off or not.
	// If the enum declaration is off, the enum declaration is not allowed in the next files.
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "enum": {
	//	    "disabled": true
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	enum:
	//	  disabled: true
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	package demo;
	//
	//	// This will error
	//	enum Color {
	//	  Red;
	//	  Green;
	//	  Blue;
	//	}
	//	// Error: enum declaration is not allowed
	//	“`
	Disabled bool `json:"disabled"`

	// @api(Grammar/Enum.Annotations) represents the [Annotation](#Grammar/Common/Annotation) grammar rules for the enum declaration.
	Annotations Annotations `json:"annotations"`

	// @api(Grammar/Enum.Validators) represents the [Validator](#Grammar/Common/Validator) for the enum declaration.
	// It's used to validate the enum name. You can access the enum name by `.Name`.
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "enum": {
	//	    "validators": [
	//	      {
	//	        "name": "EnumNameMustBeCapitalized",
	//	        "expression": "{{eq .Name (.Name | capitalize)}}",
	//	        "message": "enum name must be capitalized"
	//	      }
	//	    ]
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	enum:
	//	  validators:
	//	    - name: EnumNameMustBeCapitalized
	//	      expression: "{{eq .Name (.Name | capitalize)}}"
	//	      message: enum name must be capitalized
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	package demo;
	//
	//	// Good
	//	enum Color {
	//	  Red;
	//	  Green;
	//	  Blue;
	//	}
	//
	//	// This will error
	//	enum size {
	//	  Small;
	//	  Medium;
	//	  Large;
	//	}
	//	// Error: enum name must be capitalized, expected: Size
	//	“`
	Validators Validators `json:"validators"`

	// @api(Grammar/Enum.Member) represents the [EnumMember](#Grammar/EnumMember) grammar rules for the enum declaration.
	Member EnumMember `json:"member"`
}

@api(Grammar/Enum) represents the grammar rules for the enum declaration.

type EnumMember

type EnumMember struct {
	// @api(Grammar/EnumMember.Types) represents a list of type names that are supported in the enum declaration.
	//
	// If no types are defined, the enum declaration supports all types. Otherwise, the enum declaration
	// only supports the specified types.
	//
	// Currently, each type name can be one of the following types:
	//
	// - **int**: integer type, the value can be a positive or negative integer, for example, `123`
	// - **float**: float type, the value can be a positive or negative float, for example, `1.23`
	// - **string**: string type, the value can be a string, for example, `"hello"`
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "enum": {
	//	    "member": {
	//	      "types": ["int"]
	//	    }
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	enum:
	//	  member:
	//	    types:
	//	      - int
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	package demo;
	//
	//	// Good
	//	enum Color {
	//	  Red = 1,
	//	  Green = 2,
	//	  Blue = 3
	//	}
	//
	//	// This will error
	//	enum Size {
	//	  Small = "small",
	//	  Medium = "medium",
	//	  Large = "large"
	//	}
	//	// Error: string type is not allowed in the enum declaration
	//	“`
	Types []string `json:"types"`

	// @api(Grammar/EnumMember.ValueRequired) represents the enum member value is required or not.
	// If the enum member value is required, the enum member value must be specified in the next files.
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "enum": {
	//	    "member": {
	//	      "value_required": true
	//	    }
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	enum:
	//	  member:
	//	    value_required: true
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	package demo;
	//
	//	enum Size {
	//	  Small = 1,
	//	  Medium = 2;
	//	  // This will error
	//	  Large;
	//	  // Error: enum member value is required
	//	}
	//	“`
	ValueRequired bool `json:"value_required"`

	// @api(Grammar/EnumMember.ZeroRequired) represents the enum member zero value for integer types is required or not.
	//
	// If the enum member zero value is required, the enum member zero value must be specified in the next files.
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "enum": {
	//	    "member": {
	//	      "zero_required": true
	//	    }
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	enum:
	//	  member:
	//	    zero_required: true
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	package demo;
	//
	//	// This will error
	//	enum Size {
	//	  Small = 1,
	//	  Medium = 2,
	//	  Large = 3;
	//	}
	//	// Error: enum member zero value is required, for example:
	//	// enum Size {
	//	//   Small = 0,
	//	//   Medium = 1,
	//	//   Large = 2
	//	// }
	//	“`
	ZeroRequired bool `json:"zero_required"`

	// @api(Grammar/EnumMember.Annotations) represents the [Annotation](#Grammar/Common/Annotation) grammar rules for the enum member declaration.
	Annotations Annotations `json:"annotations"`

	// @api(Grammar/EnumMember.Validators) represents the [Validator](#Grammar/Common/Validator) for the enum member declaration.
	//
	// It's used to validate the enum member name. You can access the enum member name by `.Name`.
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "enum": {
	//	    "member": {
	//	      "validators": [
	//	        {
	//	          "name": "EnumMemberNameMustBeCapitalized",
	//	          "expression": "{{eq .Name (.Name | capitalize)}}",
	//	          "message": "enum member name must be capitalized"
	//	        }
	//	      ]
	//	    }
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	enum:
	//	  member:
	//	    validators:
	//	      - name: EnumMemberNameMustBeCapitalized
	//	        expression: "{{eq .Name (.Name | capitalize)}}"
	//	        message: enum member name must be capitalized
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	package demo;
	//
	//	enum Size {
	//	  Small = 1,
	//	  Medium = 2,
	//	  // This will error
	//	  large = 3;
	//	  // Error: enum member name must be capitalized, expected: Large
	//	}
	//	“`
	Validators Validators `json:"validators"`
}

@api(Grammar/EnumMember) represents the grammar rules for the enum member declaration.

type Grammar

type Grammar struct {
	Context   Context   `json:"context"`
	Package   Package   `json:"package"`
	Import    Import    `json:"import"`
	Const     Const     `json:"const"`
	Enum      Enum      `json:"enum"`
	Struct    Struct    `json:"struct"`
	Interface Interface `json:"interface"`
	// contains filtered or unexported fields
}

@api(Grammar) represents the custom grammar for the next files.

import CodeBlock from "@theme/CodeBlock";
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";
import ExampleGrammarYAMLSource from "!!raw-loader!@site/example/grammar.yaml";
import ExampleGrammarJSONSource from "!!raw-loader!@site/example/grammar.json";

The grammar is used to define a subset of the next files. It can limit the features of the next code according by your requirements. The grammar is a yaml file that contains rules.

Here is an example of the grammar file:

<Tabs
	defaultValue="yaml"
	values={[
		{label: 'YAML', value: 'yaml'},
		{label: 'JSON', value: 'json'},
	]}
>
<TabItem value="yaml">
	<CodeBlock language="yaml" title="grammar.yaml">
		{ExampleGrammarYAMLSource}
	</CodeBlock>
</TabItem>
<TabItem value="json">
	<CodeBlock language="json" title="grammar.json">
		{ExampleGrammarJSONSource}
	</CodeBlock>
</TabItem>
</Tabs>

It extends **built-in** grammar and defines a `@message` annotation for `struct` objects. For example:

```next
package demo;

@message(type=1, req)
struct LoginRequest {/*...*/}

@message(type=2)
struct LoginResponse {/*...*/}
```

:::tip
Run the following command to show the **built-in** grammar:

```sh
next grammar
# Or outputs the grammar to a file
next grammar grammar.yaml
next grammar grammar.json
```
:::

func (*Grammar) Resolve

func (g *Grammar) Resolve() error

Resolve resolves the grammar rules.

type Import

type Import struct {
	// @api(Grammar/Import.Disabled) represents the import declaration is off or not.
	// If the import declaration is off, the import declaration is not allowed in the next files.
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "import": {
	//	    "disabled": true
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	import:
	//	  disabled: true
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	package demo;
	//
	//	// This will error
	//	import "other.next";
	//	// Error: import declaration is not allowed
	//	“`
	Disabled bool `json:"disabled"`
}

@api(Grammar/Import) represents the grammar rules for the import declaration.

type Interface

type Interface struct {
	// @api(Grammar/Interface.Disabled) represents the interface declaration is off or not.
	// If the interface declaration is off, the interface declaration is not allowed in the next files.
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "interface": {
	//	    "disabled": true
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	interface:
	//	  disabled: true
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	package demo;
	//
	//	// This will error
	//	interface User {
	//	  GetID() int;
	//	}
	//	// Error: interface declaration is not allowed
	//	“`
	Disabled bool `json:"disabled"`

	// @api(Grammar/Interface.Annotations) represents the [Annotation](#Grammar/Common/Annotation) grammar rules for the interface declaration.
	Annotations Annotations `json:"annotations"`

	// @api(Grammar/Interface.Validators) represents the [Validator](#Grammar/Common/Validator) for the interface declaration.
	// It's used to validate the interface name. You can access the interface name by `.Name`.
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "interface": {
	//	    "validators": [
	//	      {
	//	        "name": "InterfaceNameMustBeCapitalized",
	//	        "expression": "{{eq .Name (.Name | capitalize)}}",
	//	        "message": "interface name must be capitalized"
	//	      }
	//	    ]
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	interface:
	//	  validators:
	//	    - name: InterfaceNameMustBeCapitalized
	//	      expression: "{{eq .Name (.Name | capitalize)}}"
	//	      message: interface name must be capitalized
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	package demo;
	//
	//	// Good
	//	interface User {
	//	  GetID() int;
	//	}
	//
	//	// This will error
	//	interface user {
	//	  GetName() string;
	//	}
	//	// Error: interface name must be capitalized, expected: User
	//	“`
	Validators Validators `json:"validators"`

	// @api(Grammar/Interface.Method) represents the [InterfaceMethod](#Grammar/InterfaceMethod) grammar rules for the interface declaration.
	Method InterfaceMethod `json:"method"`
}

@api(Grammar/Interface) represents the grammar rules for the interface declaration.

type InterfaceMethod

type InterfaceMethod struct {
	// @api(Grammar/InterfaceMethod.Annotations) represents the [Annotation](#Grammar/Common/Annotation) grammar rules for the interface method declaration.
	Annotations Annotations `json:"annotations"`
	// @api(Grammar/InterfaceMethod.Validators) represents the [Validator](#Grammar/Common/Validator) for the interface method declaration.
	Validators Validators `json:"validators"`
	// @api(Grammar/InterfaceMethod.Parameter) represents the [InterfaceMethodParameter](#Grammar/InterfaceMethodParameter) grammar rules for the interface method declaration.
	Parameter InterfaceMethodParameter `json:"parameter"`
}

@api(Grammar/InterfaceMethod) represents the grammar rules for the interface method declaration.

Example:

<Tabs
	defaultValue="yaml"
	values={[
		{label: 'YAML', value: 'yaml'},
		{label: 'JSON', value: 'json'},
	]}
>
<TabItem value="json">
```json
{
  "interface": {
    "method": {
      "annotations": [
        {
          "name": "http",
          "description": "Sets the method as an HTTP handler.",
          "parameters": [
            {
              "name": "method",
              "description": "Sets the HTTP method.",
              "type": "string",
              "required": true
              "validators": [
                {
                  "name": "HTTPMethodMustBeValid",
                  "expression": "{{includes (list `GET` `POST` `PUT` `DELETE` `PATCH` `HEAD` `OPTIONS` `TRACE` `CONNECT`) .}}",
                  "message": "http method must be valid"
                }
              ]
            }
          ]
        }
      ],
      "validators": [
        {
          "name": "MethodNameMustBeCapitalized",
          "expression": "{{eq .Name (.Name | capitalize)}}",
          "message": "method name must be capitalized"
        }
      ]
    }
  }
}
```
</TabItem>
<TabItem value="yaml">
```yaml
interface:
  method:
    annotations:
      - name: http
        description: Sets the method as an HTTP handler.
        parameters:
          - name: method
            description: Sets the HTTP method.
            types: [string]
            required: true
            validators:
              - name: HTTPMethodMustBeValid
                expression: "{{includes (list `GET` `POST` `PUT` `DELETE` `PATCH` `HEAD` `OPTIONS` `TRACE` `CONNECT`) .}}"
                message: http method must be valid
    validators:
      - name: MethodNameMustBeCapitalized
        expression: "{{eq .Name (.Name | capitalize)}}"
        message: method name must be capitalized
```
</TabItem>
</Tabs>

type InterfaceMethodParameter

type InterfaceMethodParameter struct {
	// @api(Grammar/InterfaceMethodParameter.Annotations) represents the [Annotation](#Grammar/Common/Annotation) grammar rules for the interface method parameter declaration.
	Annotations Annotations `json:"annotations"`
	// @api(Grammar/InterfaceMethodParameter.Validators) represents the [Validator](#Grammar/Common/Validator) for the interface method parameter declaration.
	Validators Validators `json:"validators"`
}

@api(Grammar/InterfaceMethodParameter) represents the grammar rules for the interface method parameter declaration.

type Options

type Options[T any] struct {
	// contains filtered or unexported fields
}

Options represents the options with the id and/or the options. If the id is empty, the options are used. Otherwise, the id is used. The options are resolved by the context using the id as the key.

func (Options[T]) MarshalJSON

func (o Options[T]) MarshalJSON() ([]byte, error)

func (*Options[T]) UnmarshalJSON

func (o *Options[T]) UnmarshalJSON(data []byte) error

func (Options[T]) Value

func (o Options[T]) Value() T

type Package

type Package struct {
	// @api(Grammar/Package.Annotations) represents the [Annotation](#Grammar/Common/Annotation) grammar rules for the package declaration.
	Annotations Annotations `json:"annotations"`

	// @api(Grammar/Package.Validators) represents the [Validator](#Grammar/Common/Validator) for the package declaration.
	// It's used to validate the package name. For example, You can limit the package name must be
	// not start with a "_" character. The validator expression is a template string that can access
	// package name by `.Name`.
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "package": {
	//	    "validators": [
	//	      {
	//	        "name": "PackageNameNotStartWithUnderscore",
	//	        "expression": "{{not (hasPrefix `_` .Name)}}",
	//	        "message": "package name must not start with an underscore"
	//	      }
	//	    ]
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	package:
	//	  validators:
	//	    - name: PackageNameNotStartWithUnderscore
	//	      expression: "{{not (hasPrefix `_` .Name)}}"
	//	      message: package name must not start with an underscore
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	// This will error
	//	package _test;
	//	// Error: package name must not start with an underscore
	//	“`
	Validators Validators `json:"validators"`
}

@api(Grammar/Package) represents the grammar rules for the package declaration.

type Struct

type Struct struct {
	// @api(Grammar/Struct.Disabled) represents the struct declaration is off or not.
	// If the struct declaration is off, the struct declaration is not allowed in the next files.
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "struct": {
	//	    "disabled": true
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	struct:
	//	  disabled: true
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	package demo;
	//
	//	// This will error
	//	struct User {
	//	  int id;
	//	  string name;
	//	}
	//	// Error: struct declaration is not allowed
	//	“`
	Disabled bool `json:"disabled"`

	// @api(Grammar/Struct.Annotations) represents the [Annotation](#Grammar/Common/Annotation) grammar rules for the struct declaration.
	Annotations Annotations `json:"annotations"`

	// @api(Grammar/Struct.Validators) represents the [Validator](#Grammar/Common/Validator) for the struct declaration.
	// It's used to validate the struct name. You can access the struct name by `.Name`.
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "struct": {
	//	    "validators": [
	//	      {
	//	        "name": "StructNameMustBeCapitalized",
	//	        "expression": "{{eq .Name (.Name | capitalize)}}",
	//	        "message": "struct name must be capitalized"
	//	      }
	//	    ]
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	struct:
	//	  validators:
	//	    - name: StructNameMustBeCapitalized
	//	      expression: "{{eq .Name (.Name | capitalize)}}"
	//	      message: struct name must be capitalized
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	package demo;
	//
	//	// Good
	//	struct User {
	//	  int id;
	//	  string name;
	//	}
	//
	//	// This will error
	//	struct point {
	//	  int x;
	//	  int y;
	//	}
	//	// Error: struct name must be capitalized, expected: Point
	//	“`
	Validators Validators `json:"validators"`

	// @api(Grammar/Struct.Field) represents the [StructField](#Grammar/StructField) grammar rules for the struct declaration.
	Field StructField `json:"field"`
}

@api(Grammar/Struct) represents the grammar rules for the struct declaration.

type StructField

type StructField struct {
	// @api(Grammar/StructField.Annotations) represents the [Annotation](#Grammar/Common/Annotation) grammar rules for the struct field declaration.
	Annotations Annotations `json:"annotations"`

	// @api(Grammar/StructField.Validators) represents the [Validator](#Grammar/Common/Validator) for the struct field declaration.
	// It's used to validate the struct field name. You can access the struct field name by `.Name`.
	//
	// Example:
	//
	//	<Tabs
	//		defaultValue="yaml"
	//		values={[
	//			{label: 'YAML', value: 'yaml'},
	//			{label: 'JSON', value: 'json'},
	//		]}
	//	>
	//	<TabItem value="json">
	//	“`json
	//	{
	//	  "struct": {
	//	    "field": {
	//	      "validators": [
	//	        {
	//	          "name": "StructFieldNameMustNotBeCapitalized",
	//	          "expression": "{{ne .Name (capitalize .Name)}}",
	//	          "message": "struct field name must not be capitalized"
	//	        }
	//	      ]
	//	    }
	//	  }
	//	}
	//	“`
	//	</TabItem>
	//	<TabItem value="yaml">
	//	“`yaml
	//	struct:
	//	  field:
	//	    validators:
	//	      - name: StructFieldNameMustNotBeCapitalized
	//	        expression: "{{ne .Name (capitalize .Name)}}"
	//	        message: struct field name must not be capitalized
	//	“`
	//	</TabItem>
	//	</Tabs>
	//
	//	“`next
	//	package demo;
	//
	//	struct User {
	//	  int id;
	//	  // This will error
	//	  string Name;
	//	  // Error: struct field name must not be capitalized, expected: name
	//	}
	//	“`
	Validators Validators `json:"validators"`
}

@api(Grammar/StructField) represents the grammar rules for the struct field declaration.

type Validator

type Validator struct {
	// @api(Grammar/Common/Validator.Name) represents the validator name.
	Name string `json:"name"`

	// @api(Grammar/Common/Validator.Expression) represents the validator expression.
	// The expression is a template string that can access the data by the `.` operator. The expression
	// must return a boolean value.
	//
	// The data is the current context object. For example, **package** object for the package validator.
	Expression string `json:"expression"`

	// @api(Grammar/Common/Validator.Message) represents the error message when the validator is failed.
	Message string `json:"message"`
}

@api(Grammar/Common/Validator) represents the validator for the grammar rules.

func (Validator) Validate

func (v Validator) Validate(data any) (bool, error)

type Validators

type Validators []Options[Validator]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL