syslwrapper

package
v0.677.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2023 License: Apache-2.0 Imports: 4 Imported by: 2

README

syslwrapper

This package adds an abstraction layer around sysl proto types defined in the sysl package. This is currently a work in progress, but is intended to make working with sysl types much easier and to solve common issues around working with sysl type references, as well as parsing return statements.

Usage

func main() {
	filename := "demo/grocerystore/GroceryStore.sysl"
	syslModules, err := parse.NewParser().Parse(filename, afero.NewOsFs())
	if err != nil {
		panic(err)
	}
   
	mapper := MakeAppMapper(syslModules)
    mapper.IndexTypes() // This indexes all the sysl.Type in each application in a map, which can be accessed using "AppName:TypeName"
    mapper.ConvertTypes() // This converts the indexed types into syslwrapper.Type
    simpleApps, err := mapper.Map() // Maps the applications in syslModules into simple syslwrapperApps
    
    getInventoryEndpoint := simpleApps.["GroceryStore"].Endpoints["GET /inventory"]
    params := getInventoryEndpoint.Params
    responses := getInventoryEndpoint.Responses
    
    prettyPrintParams, err := json.MarshalIndent(params, "", " ")
	prettyPrintResponses, err := json.MarshalIndent(responses, "", " ")
	prettyPrintInventoryResponse, err := json.MarshalIndent(mapper.SimpleTypes["GroceryStore:fooid"], "", " ")
    fmt.Println(string(prettyPrintParams))
	fmt.Println(string(prettyPrintResponses))
	fmt.Println(string(prettyPrintInventoryResponse))
}
Response Statement Parsing

Responses are returned as

{
 "200": {
  "In": "",
  "Description": "",
  "Name": "200",
  "Type": {
   "Description": "",
   "Optional": false,
   "Reference": "",
   "Type": "list",
   "Items": [
    {
     "Description": "",
     "Optional": false,
     "Reference": "GroceryStore:InventoryResponse",
     "Type": "ref",
     "Items": null,
     "Enum": null,
     "Properties": null
    }
   ],
   "Enum": null,
   "Properties": null
  }
 }
}

Let's compare this with what the sysl.Application type gives us if we called syslModules.Apps["GroceryStore"].Endpoints["GET /inventory"].Stmt

[
 {
  "Stmt": {
   "Call": {
    "target": {
     "part": [
      "Inventory"
     ]
    },
    "endpoint": "GET /inventory"
   }
  }
 },
 {
  "Stmt": {
   "Ret": {
    "payload": "sequence of InventoryResponse"
   }
  }
 }
]
Parameter Type Conversion

Here's what simpleApps["GroceryStore"].Endpoints["GET /inventory"].Params gives us

{
 "fooid": {
  "In": "header",
  "Description": "",
  "Name": "fooid",
  "Type": {
   "Description": "",
   "Optional": false,
   "Reference": "",
   "Type": "string",
   "Items": null,
   "Enum": null,
   "Properties": null
  }
 }
}

Let's compare this with what the sysl.Application type gives us if we called syslModules.Apps["GroceryStore"].Endpoints["GET /inventory"].Param

[
 {
  "name": "fooid",
  "type": {
   "Type": {
    "Primitive": 6
   },
   "attrs": {
    "name": {
     "Attribute": {
      "S": "FooID"
     }
    },
    "patterns": {
     "Attribute": {
      "A": {
       "elt": [
        {
         "Attribute": {
          "S": "header"
         }
        },
        {
         "Attribute": {
          "S": "required"
         }
        }
       ]
      }
     }
    }
   }
  }
 }
]
Type Lookups

If we wanted to lookup what InventoryResponse was, we can just call

    mapper.SimpleTypes["GroceryStore:InventoryResponse"]

which returns

{
 "Description": "",
 "Optional": false,
 "Reference": "",
 "Type": "tuple",
 "Items": null,
 "Enum": null,
 "Properties": {
  "item_id": {
   "Description": "",
   "Optional": false,
   "Reference": "",
   "Type": "string",
   "Items": null,
   "Enum": null,
   "Properties": null
  },
  "quantity": {
   "Description": "",
   "Optional": false,
   "Reference": "",
   "Type": "int",
   "Items": null,
   "Enum": null,
   "Properties": null
  }
 }
}
Type Resolution (WIP)

Lets say you don't care for having to deal with looking up type references, you just want definite types for all of these

mapper.ResolveTypes()

Or if you want to just resolve one type, use mapper.ResolveTypes() Not Yet Fully Implemented

This function goes through each type reference and replaces it with the actual data type. This converts the data types from a graph representation to a tree representation.

This feature is still under construction, with some recursion that needs to be resolved.

Example

func main() {
	filename := "demo/grocerystore/GroceryStore.sysl"
	syslModules, err := parse.NewParser().Parse(filename, afero.NewOsFs())
	if err != nil {
		panic(err)
	}

	mapper := syslwrapper.MakeAppMapper(syslModules)
	mapper.IndexTypes()
	mapper.ResolveTypes()
	mapper.ConvertTypes()
	simpleApps, err := mapper.Map()

	responses := simpleApps["GroceryStore"].Endpoints["GET /inventory"].Response
	prettyPrintResponses, err := json.MarshalIndent(responses, "", " ")
	fmt.Println(string(prettyPrintResponses))
}
{
 "200": {
  "In": "",
  "Description": "",
  "Name": "200",
  "Type": {
   "Description": "",
   "Optional": false,
   "Reference": "",
   "Type": "list",
   "Items": [
    {
     "Description": "",
     "Optional": false,
     "Reference": "GroceryStore:InventoryResponse",
     "Type": "ref",
     "Items": null,
     "Enum": null,
     "Properties": null
    }
   ],
   "Enum": null,
   "Properties": null
  }
 }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasPattern

func HasPattern(attrs map[string]*sysl.Attribute, pattern string) bool

func IsPrimitive added in v0.97.0

func IsPrimitive(typeName string) bool

IsPrimitive takes an input type string and returns true if the string is a builtin sysl primitive type. This includes double, int64, float64, string, bool, date, datetime Mostly used for parsing return statements

func MakeApp

func MakeApp(name string, params []*sysl.Param, types map[string]*sysl.Type) *sysl.Application

func MakeAppName

func MakeAppName(name string) *sysl.AppName

func MakeEnum

func MakeEnum(enum map[int64]string) *sysl.Type

func MakeList

func MakeList(listType *sysl.Type) *sysl.Type

func MakeMap

func MakeMap(keyType *sysl.Type, valueType *sysl.Type) *sysl.Type

func MakeNoType

func MakeNoType() *sysl.Type

func MakeOneOf

func MakeOneOf(oneOfType []*sysl.Type) *sysl.Type

func MakeParam

func MakeParam(name string, paramType *sysl.Type) *sysl.Param

func MakePrimitive

func MakePrimitive(primType string) *sysl.Type

func MakeRelation added in v0.77.0

func MakeRelation(types map[string]*sysl.Type, primaryKey string, keys []string) *sysl.Type

TODO relation, set, sequence, notype

func MakeReturnStatement

func MakeReturnStatement(payload string) *sysl.Statement

func MakeSequence added in v0.138.0

func MakeSequence(seqType *sysl.Type) *sysl.Type

func MakeSet added in v0.77.0

func MakeSet(setType *sysl.Type) *sysl.Type

func MakeTuple

func MakeTuple(tuple map[string]*sysl.Type) *sysl.Type

func MakeType

func MakeType(name string, value interface{}, t string) *sysl.Type

func MakeTypeRef

func MakeTypeRef(contextApp string, contextTypePath []string, refApp string, refTypePath []string) *sysl.Type

Creates a type reference from one context to another. contextApp is the name of the app where the typeref is used refApp is the app where the

func ParamIn

func ParamIn(attrs map[string]*sysl.Attribute) string

Types

type App

type App struct {
	Name       string
	Attributes map[string]string    // Contains app level attributes. All attributes assumed to be strings.
	Endpoints  map[string]*Endpoint // Contains all application endpoints
	Types      map[string]*Type     // Contains all type definitions in the application, excluding types defined in Params and Responses
}

App is a simplified representation of an application in sysl

type AppMapper

type AppMapper struct {
	Module      *sysl.Module
	Types       map[string]*sysl.Type // A map of all sysl types. Key is in format "appname:typename".
	SimpleTypes map[string]*Type      // A map of all simplified types. Key is in format "appname:typename".
}

func MakeAppMapper

func MakeAppMapper(m *sysl.Module) *AppMapper

MakeAppMapper creates an appmapper

func (*AppMapper) BuildApplication

func (am *AppMapper) BuildApplication(a *sysl.Application) *App

BuildApplication returns a clean representation of a Sysl Application which hides the complexities of the protobuf generated type.

func (*AppMapper) ConvertTypes

func (am *AppMapper) ConvertTypes() map[string]*Type

func (*AppMapper) GetAttribute

func (am *AppMapper) GetAttribute(attribute map[string]*sysl.Attribute, attributeName string) string

func (*AppMapper) GetRefDetails

func (am *AppMapper) GetRefDetails(t *sysl.Type) (appName string, typeName string)

TypeRefs can have various formats. Case 1: When a type defined in the same app is referenced in a TYPE

  • no appname is provided in the path
  • the ref.path[0] element is the type name

Case 2: When a type from another app is referenced in a TYPE

  • context is provided
  • the ref.path[0] element is the application name

Case 3: When a type from another app is referenced in a parameter

  • context is NOT provided
  • ref.appName is provided
  • the ref.path[0] element is the type name

Case 4: When a type from the same app is referenced in a parameter

  • context is NOT provided
  • ref.appName is provided AND is the type name (This is crazy and needs to be fixed)

func (*AppMapper) IndexTypes

func (am *AppMapper) IndexTypes() map[string]*sysl.Type

Creates a map of all types TODO Check if colon is valid in typename

func (*AppMapper) Map

func (am *AppMapper) Map() (map[string]*App, error)

ImportModule takes a sysl module and maps them into an array of simplified App structs It resolves any type references and cross application calls

func (*AppMapper) MapSyslType

func (am *AppMapper) MapSyslType(t *sysl.Type) (*sysl.Type, error)

MapSyslType converts types from sysl.Type to Type

func (*AppMapper) MapType

func (am *AppMapper) MapType(t *sysl.Type) *Type

Converts sysl type to a string representation of the type

func (*AppMapper) ResolveTypes

func (am *AppMapper) ResolveTypes()

Iterates over types and resolves typerefs

type Endpoint

type Endpoint struct {
	Summary     string                 // Short human-readable description of what the endpoint does
	Description string                 // Longer description of what the endpoint does
	Path        string                 // Path
	Params      map[string]*Parameter  // Request parameters
	Response    map[string]*Parameter  // Response parameters
	Downstream  []string               // TODO: Work out the dependency graph of each application. Store downstreams in this field.
	Extensions  map[string]interface{} // Any attributes starting with x-
}

Endpoint is a simplified representation of a Sysl endpoint

type Parameter

type Parameter struct {
	In          string // Valid values include {body, header, path, query}. Cookie not supported
	Description string
	Name        string
	Type        *Type
}

type Type

type Type struct {
	Description string
	PrimaryKey  string           // Used to represent the primary key for type relation and key for map types.
	Optional    bool             // Used to represent if the type is optional
	Reference   string           // Used to represent type references. In the format of app:typename.
	Type        string           // Used to indicate the type. Can be one of {"bool", "int", "float", "decimal", "string", "string_8", "bytes", "date", "datetime", "xml", "uuid", "ref", "list", "map", "enum", "tuple", relation}
	Items       []*Type          // Used to represent map types, where the 0 index is the key type, and 1 index is the value type.
	Enum        map[int64]string // Used to represent enums
	Properties  map[string]*Type // Used to represent tuple and relation types.
}

Type represents a simplified Sysl Type.

Jump to

Keyboard shortcuts

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