Documentation
¶
Overview ¶
Package jsonmsg parses API specs. The resulting spec can be used to generate server/client source code in any supported language.
The jsonmsg implementation is based on https://github.com/jsonmsg/spec with the meta-schema https://github.com/jsonmsg/spec/blob/master/meta.json
Generators ¶
go: https://godoc.org/github.com/tfkhsr/jsonmsg/golang
Parse a spec:
schema := `
{
"endpoints": {
"http": "https://jsonmsg.github.io/v1",
"websocket": "wss://jsonmsg.github.io/v1"
},
"messages": {
"findUser": {
"in": "#/definitions/userQuery",
"outs": [
"#/definitions/user",
"#/definitions/error"
],
"group": "user"
}
},
"definitions": {
"user": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": ["id", "name"]
},
"userQuery": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
},
"required": ["id"]
},
"error": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
},
"required": ["message"]
}
}
}
`
// parse spec
spc, err := Parse(spec)
if err != nil {
panic(err)
}
// spc now contains:
// spc.Messages["findUser"] : *Message{...}
// spc.Definitions["user"] : *jsonschema.Schema{...}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Message ¶
type Message struct {
// Short description
Title string
// Detailed description
Description string
// Literal message name as defined in spec
Msg string
// Camel-Cased name
Name string
// JSON Pointer to input schema
In string
// JSON Pointers to possible output schemas
Outs []string
// Pointer to parsed input schema
InSchema *jsonschema.Schema
// Pointers to parsed output schemas
OutSchemas []*jsonschema.Schema
// Pointer to parent spec
Spec *Spec
// Optional: Group name associating message to spec.GroupedMessages
Group string
}
A Message holds details about name, in- and out parameters
func (*Message) NewInstance ¶
Creates a new message instance conforming to the message schema
Example ¶
Generate a sample message conforming to the specified message schema
schema := `
{
"endpoints": {
"http": "https://jsonmsg.github.io/v1",
"websocket": "wss://jsonmsg.github.io/v1"
},
"messages": {
"findUser": {
"in": "#/definitions/userQuery",
"outs": [
"#/definitions/user",
"#/definitions/error"
],
"group": "user"
}
},
"definitions": {
"user": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": ["id", "name"]
},
"userQuery": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
},
"required": ["id"]
},
"error": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
},
"required": ["message"]
}
}
}
`
// parse into spec
spec, err := Parse([]byte(schema))
if err != nil {
panic(err)
}
// create go instance
inst, err := spec.Messages["findUser"].NewInstance()
if err != nil {
panic(err)
}
// marshal to json
raw, err := json.MarshalIndent(inst, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("%s\n", raw)
Output: { "data": { "id": "string" }, "msg": "findUser" }
type Spec ¶
type Spec struct {
// General title of API
Title string
// Further information
Description string
// Map of protocols to URLs (urlString embeds url.URL for unmarshaling)
Endpoints map[string]*urlString
// Map of message names to Messages
Messages map[string]*Message
// Map of groups of message names to Messages
GroupedMessages map[string]map[string]*Message
// JSON Schema definitions for data definition
Definitions jsonschema.Index `json:"-"`
// Raw spec
Raw []byte
}
A Spec holds endpoints, messages and definitions of an API. Parsed spec must validate against meta-schema https://github.com/jsonmsg/spec/blob/master/meta.json
func Parse ¶
Parses a raw schema into a Spec
Example ¶
Parse a schema into an Index of Schemas
schema := `
{
"endpoints": {
"http": "https://jsonmsg.github.io/v1",
"websocket": "wss://jsonmsg.github.io/v1"
},
"messages": {
"findUser": {
"in": "#/definitions/userQuery",
"outs": [
"#/definitions/user",
"#/definitions/error"
],
"group": "user"
}
},
"definitions": {
"user": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": ["id", "name"]
},
"userQuery": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
},
"required": ["id"]
},
"error": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
},
"required": ["message"]
}
}
}
`
// parse into index
spec, err := Parse([]byte(schema))
if err != nil {
panic(err)
}
fmt.Printf("findUser name: %s\n", spec.Messages["findUser"].Name)
fmt.Printf("findUser in name: %s\n", spec.Messages["findUser"].InSchema.Name)
fmt.Printf("findUser in group: %s\n", spec.GroupedMessages["user"]["findUser"].InSchema.Name)
Output: findUser name: FindUser findUser in name: UserQuery findUser in group: UserQuery
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
jsonmsgc
command
|
|
|
Package fixture provides common schemas for testing and evaluation
|
Package fixture provides common schemas for testing and evaluation |
|
Package golang generates go sources implementing a jsonmsg.Spec.
|
Package golang generates go sources implementing a jsonmsg.Spec. |