schema

package
v0.5.2-0...-176e034 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2020 License: MIT Imports: 9 Imported by: 0

README

WebRPC Schema

WebRPC is a design/schema driven approach to writing backend servers, with fully-generated client libraries. Write your schema, and it will generate strongly-typed bindings between your server and client. The type system is described below.

Some example webrpc schemas:

  • from the _examples/, here is a schema in RIDL or in JSON
  • ..find more in ./_examples

Type system

Basics
  • byte (aka uint8)
  • bool
  • any
  • null
Integers
  • uint8

  • uint16

  • uint32

  • uint64

  • int8

  • int16

  • int32

  • int64

Floats
  • float32
  • float64
Strings
  • string
Timestamps (date/time)
  • timestamp - for date/time
Lists
  • form: []<type>
  • ie.
    • []string
    • []uint8
    • [][]string
    • ..
Map
  • form: map<key,value>
  • ie.
    • map<string,any>
    • map<string,map<string,any>>
    • map<string,[]uint8>
    • map<int64,[]string>
    • map<string,User> - where User is a struct type defined in schema
Enums
  • enum, see examples
Binary (future / v2)
Structs aka Objects / Messages
  • struct or object
    • think of it just as a Javascript object or JSON object
Some notes on structs
  • fields of an object can be optional
  • fields of an object are by default required, unless made optional
  • fields of an object always return default values by default, ie. default of int is 0, string is "", etc. (like in Go)
    • otherwise someone should make it optional which will have it be nullable

Documentation

Index

Constants

View Source
const (
	VERSION = "v1" // todo rename to schema_version
)

Variables

View Source
var DataTypeFromString = map[string]DataType{
	"null": T_Null,
	"any":  T_Any,
	"byte": T_Byte,
	"bool": T_Bool,

	"uint":   T_Uint,
	"uint8":  T_Uint8,
	"uint16": T_Uint16,
	"uint32": T_Uint32,
	"uint64": T_Uint64,

	"int":   T_Int,
	"int8":  T_Int8,
	"int16": T_Int16,
	"int32": T_Int32,
	"int64": T_Int64,

	"float32": T_Float32,
	"float64": T_Float64,

	"string": T_String,

	"timestamp": T_Timestamp,

	"map": T_Map,
	"[]":  T_List,
}
View Source
var DataTypeToString = map[DataType]string{
	T_Null: "null",
	T_Any:  "any",
	T_Byte: "byte",
	T_Bool: "bool",

	T_Uint:   "uint",
	T_Uint8:  "uint8",
	T_Uint16: "uint16",
	T_Uint32: "uint32",
	T_Uint64: "uint64",

	T_Int:   "int",
	T_Int8:  "int8",
	T_Int16: "int16",
	T_Int32: "int32",
	T_Int64: "int64",

	T_Float32: "float32",
	T_Float64: "float64",

	T_String: "string",

	T_Timestamp: "timestamp",

	T_Map:  "map",
	T_List: "[]",
}
View Source
var NameWhitelistRexp = regexp.MustCompile(`^[a-zA-Z]+[a-zA-Z0-9_]*$`)

Functions

func IsValidArgName

func IsValidArgName(s string) bool

func ParseVarTypeExpr

func ParseVarTypeExpr(schema *WebRPCSchema, expr string, vt *VarType) error

Types

type DataType

type DataType int
const (
	T_Unknown DataType = iota

	T_Null
	T_Any
	T_Byte
	T_Bool

	T_Uint
	T_Uint8
	T_Uint16
	T_Uint32
	T_Uint64

	T_Int
	T_Int8
	T_Int16
	T_Int32
	T_Int64

	T_Float32
	T_Float64

	T_String

	T_Timestamp

	T_List
	T_Map

	T_Primitive
	T_Struct // aka, a reference to our own webrpc proto struct/message
)

func (DataType) MarshalJSON

func (t DataType) MarshalJSON() ([]byte, error)

func (DataType) String

func (t DataType) String() string

func (*DataType) UnmarshalJSON

func (t *DataType) UnmarshalJSON(b []byte) error

type Import

type Import struct {
	Path    string   `json:"path"`
	Members []string `json:"members"`
}

type Message

type Message struct {
	Name   VarName         `json:"name"`
	Type   MessageType     `json:"type"`
	Fields []*MessageField `json:"fields"`

	// EnumType determined for enum types during parsing time
	EnumType *VarType `json:"-"`
}

func (*Message) Parse

func (m *Message) Parse(schema *WebRPCSchema) error

type MessageField

type MessageField struct {
	Name VarName  `json:"name"`
	Type *VarType `json:"type"`

	Optional bool   `json:"optional"`
	Value    string `json:"value"` // used by enums

	// Meta store extra metadata on a field for plugins
	Meta []MessageFieldMeta `json:"meta"`
}

type MessageFieldMeta

type MessageFieldMeta map[string]interface{}

type MessageType

type MessageType string // "enum" | "struct"

type Method

type Method struct {
	Name VarName `json:"name"`

	StreamInput  bool `json:"streamInput,omitempty"`
	StreamOutput bool `json:"streamOutput,omitempty"`
	Proxy        bool `json:"-"` // TODO: actual implementation

	Inputs  []*MethodArgument `json:"inputs"`
	Outputs []*MethodArgument `json:"outputs"`

	Service *Service `json:"-"` // denormalize/back-reference
}

func (*Method) Parse

func (m *Method) Parse(schema *WebRPCSchema, service *Service) error

type MethodArgument

type MethodArgument struct {
	Name     VarName  `json:"name"`
	Type     *VarType `json:"type"`
	Optional bool     `json:"optional"`

	InputArg  bool `json:"-"` // denormalize/back-reference
	OutputArg bool `json:"-"` // denormalize/back-reference
}

type Parser

type Parser interface {
	Parse(schema *WebRPCSchema) error
}

type Reader

type Reader struct {
	File string
	// contains filtered or unexported fields
}

func NewReader

func NewReader(r io.Reader, path string) *Reader

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

type Service

type Service struct {
	Name    VarName   `json:"name"`
	Methods []*Method `json:"methods"`

	Schema *WebRPCSchema `json:"-"` // denormalize/back-reference
}

func (*Service) Parse

func (s *Service) Parse(schema *WebRPCSchema) error

type VarListType

type VarListType struct {
	Elem *VarType
}

type VarMapType

type VarMapType struct {
	Key   DataType // see, VarMapKeyDataTypes -- only T_String or T_XintX supported
	Value *VarType
}

type VarName

type VarName string

func (VarName) TitleDowncase

func (v VarName) TitleDowncase() string

TitleDowncase will downcase the first letter of a string, ie. convert a name form 'FirstName' to 'firstName'

func (VarName) TitleUpcase

func (v VarName) TitleUpcase() string

TitleUpcase will upcase the first letter of a string, ie. convert a name form 'firstName' to 'FirstName'

type VarStructType

type VarStructType struct {
	Name    string
	Message *Message
}

type VarType

type VarType struct {
	Type DataType

	List   *VarListType
	Map    *VarMapType
	Struct *VarStructType
	// contains filtered or unexported fields
}

func (*VarType) MarshalJSON

func (t *VarType) MarshalJSON() ([]byte, error)

func (*VarType) Parse

func (t *VarType) Parse(schema *WebRPCSchema) error

func (*VarType) String

func (t *VarType) String() string

func (*VarType) UnmarshalJSON

func (t *VarType) UnmarshalJSON(b []byte) error

type WebRPCSchema

type WebRPCSchema struct {
	WebRPCVersion string    `json:"webrpc"`
	Name          string    `json:"name"`
	SchemaVersion string    `json:"version"`
	Imports       []*Import `json:"imports"`

	Messages []*Message `json:"messages"`
	Services []*Service `json:"services"`
}

schema of webrpc json file, and validations

func ParseSchemaJSON

func ParseSchemaJSON(jsondata []byte) (*WebRPCSchema, error)

func (*WebRPCSchema) GetMessageByName

func (s *WebRPCSchema) GetMessageByName(name string) *Message

func (*WebRPCSchema) GetServiceByName

func (s *WebRPCSchema) GetServiceByName(name string) *Service

func (*WebRPCSchema) HasFieldType

func (s *WebRPCSchema) HasFieldType(fieldType string) (bool, error)

func (*WebRPCSchema) SchemaHash

func (s *WebRPCSchema) SchemaHash() (string, error)

func (*WebRPCSchema) ToJSON

func (s *WebRPCSchema) ToJSON(optIndent ...bool) (string, error)

func (*WebRPCSchema) Validate

func (s *WebRPCSchema) Validate() error

Validate validates the schema through the AST, intended to be called after the json has been unmarshalled

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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