[svn-commits] dlee: branch dlee/ari-event-remodel r391891 - in /team/dlee/ari-event-remodel...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Fri Jun 14 15:55:42 CDT 2013


Author: dlee
Date: Fri Jun 14 15:55:40 2013
New Revision: 391891

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=391891
Log:
First cut at creating JSON validators.

Modified:
    team/dlee/ari-event-remodel/Makefile
    team/dlee/ari-event-remodel/include/asterisk/json.h
    team/dlee/ari-event-remodel/main/json.c
    team/dlee/ari-event-remodel/res/Makefile
    team/dlee/ari-event-remodel/res/res_stasis_json_asterisk.c
    team/dlee/ari-event-remodel/res/stasis_http/resource_channels.c
    team/dlee/ari-event-remodel/res/stasis_json/resource_asterisk.h
    team/dlee/ari-event-remodel/rest-api-templates/asterisk_processor.py
    team/dlee/ari-event-remodel/rest-api-templates/make_stasis_http_stubs.py
    team/dlee/ari-event-remodel/rest-api-templates/swagger_model.py
    team/dlee/ari-event-remodel/rest-api/api-docs/bridges.json
    team/dlee/ari-event-remodel/rest-api/api-docs/events.json
    team/dlee/ari-event-remodel/rest-api/api-docs/recordings.json

Modified: team/dlee/ari-event-remodel/Makefile
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/Makefile?view=diff&rev=391891&r1=391890&r2=391891
==============================================================================
--- team/dlee/ari-event-remodel/Makefile (original)
+++ team/dlee/ari-event-remodel/Makefile Fri Jun 14 15:55:40 2013
@@ -971,7 +971,7 @@
 	@false
 else
 	$(PYTHON) rest-api-templates/make_stasis_http_stubs.py \
-		rest-api/resources.json res/
+		rest-api/resources.json .
 endif
 
 .PHONY: menuselect

Modified: team/dlee/ari-event-remodel/include/asterisk/json.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/include/asterisk/json.h?view=diff&rev=391891&r1=391890&r2=391891
==============================================================================
--- team/dlee/ari-event-remodel/include/asterisk/json.h (original)
+++ team/dlee/ari-event-remodel/include/asterisk/json.h Fri Jun 14 15:55:40 2013
@@ -157,6 +157,15 @@
  * \return Type of \a value.
  */
 enum ast_json_type ast_json_typeof(const struct ast_json *value);
+
+/*!
+ * \brief Get the string name for the given type.
+ * \since 12.0.0
+ * \param type Type to convert to string.
+ * \return Simple string for the type name (object, array, string, etc.)
+ * \return \c "?" for invalid types.
+ */
+const char *ast_json_typename(enum ast_json_type type);
 
 /*!@}*/
 

Modified: team/dlee/ari-event-remodel/main/json.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/main/json.c?view=diff&rev=391891&r1=391890&r2=391891
==============================================================================
--- team/dlee/ari-event-remodel/main/json.c (original)
+++ team/dlee/ari-event-remodel/main/json.c Fri Jun 14 15:55:40 2013
@@ -100,6 +100,23 @@
 	ast_assert(0); /* Unexpect return from json_typeof */
 	return r;
 }
+
+const char *ast_json_typename(enum ast_json_type type)
+{
+	switch (type) {
+	case AST_JSON_OBJECT: return "object";
+	case AST_JSON_ARRAY: return "array";
+	case AST_JSON_STRING: return "string";
+	case AST_JSON_INTEGER: return "integer";
+	case AST_JSON_REAL: return "real";
+	case AST_JSON_TRUE: return "boolean";
+	case AST_JSON_FALSE: return "boolean";
+	case AST_JSON_NULL: return "null";
+	}
+	ast_assert(0);
+	return "?";
+}
+
 
 struct ast_json *ast_json_true(void)
 {

Modified: team/dlee/ari-event-remodel/res/Makefile
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/res/Makefile?view=diff&rev=391891&r1=391890&r2=391891
==============================================================================
--- team/dlee/ari-event-remodel/res/Makefile (original)
+++ team/dlee/ari-event-remodel/res/Makefile Fri Jun 14 15:55:40 2013
@@ -80,5 +80,8 @@
 $(if $(filter res_parking,$(EMBEDDED_MODS)),modules.link,res_parking.so): $(subst .c,.o,$(wildcard parking/*.c))
 $(subst .c,.o,$(wildcard parking/*.c)): _ASTCFLAGS+=$(call MOD_ASTCFLAGS,res_parking)
 
+res_ari_model.so: stasis_http/ari_model_native.o
+stasis_http/ari_model_native.o: _ASTCFLAGS+=$(call MOD_ASTCFLAGS,res_ari_model)
+
 # Dependencies for res_stasis_http_*.so are generated, so they're in this file
 include stasis_http.make

Modified: team/dlee/ari-event-remodel/res/res_stasis_json_asterisk.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/res/res_stasis_json_asterisk.c?view=diff&rev=391891&r1=391890&r2=391891
==============================================================================
--- team/dlee/ari-event-remodel/res/res_stasis_json_asterisk.c (original)
+++ team/dlee/ari-event-remodel/res/res_stasis_json_asterisk.c Fri Jun 14 15:55:40 2013
@@ -1,60 +1,0 @@
-/*
- * Asterisk -- An open source telephony toolkit.
- *
- * Copyright (C) 2012 - 2013, Digium, Inc.
- *
- * David M. Lee, II <dlee at digium.com>
- *
- * See http://www.asterisk.org for more information about
- * the Asterisk project. Please do not directly contact
- * any of the maintainers of this project for assistance;
- * the project provides a web site, mailing lists and IRC
- * channels for your use.
- *
- * This program is free software, distributed under the terms of
- * the GNU General Public License Version 2. See the LICENSE file
- * at the top of the source tree.
- */
-
-/*
- * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- * !!!!!                               DO NOT EDIT                        !!!!!
- * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- * This file is generated by a mustache template. Please see the original
- * template in rest-api-templates/res_stasis_http_resource.c.mustache
- */
-
-/*! \file
- *
- * \brief Asterisk resources
- *
- * \author David M. Lee, II <dlee at digium.com>
- */
-
-/*** MODULEINFO
-	<support_level>core</support_level>
- ***/
-
-#include "asterisk.h"
-
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
-#include "asterisk/module.h"
-#include "asterisk/json.h"
-#include "stasis_json/resource_asterisk.h"
-static int load_module(void)
-{
-	return 0;
-}
-
-static int unload_module(void)
-{
-	return 0;
-}
-
-AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER,
-	"Stasis JSON Generators and Validators - Asterisk resources",
-	.load = load_module,
-	.unload = unload_module,
-	.load_pri = AST_MODPRI_DEFAULT,
-	);

Modified: team/dlee/ari-event-remodel/res/stasis_http/resource_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/res/stasis_http/resource_channels.c?view=diff&rev=391891&r1=391890&r2=391891
==============================================================================
--- team/dlee/ari-event-remodel/res/stasis_http/resource_channels.c (original)
+++ team/dlee/ari-event-remodel/res/stasis_http/resource_channels.c Fri Jun 14 15:55:40 2013
@@ -1,4 +1,4 @@
-/* -*- C -*-
+/*
  * Asterisk -- An open source telephony toolkit.
  *
  * Copyright (C) 2012 - 2013, Digium, Inc.

Modified: team/dlee/ari-event-remodel/res/stasis_json/resource_asterisk.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/res/stasis_json/resource_asterisk.h?view=diff&rev=391891&r1=391890&r2=391891
==============================================================================
--- team/dlee/ari-event-remodel/res/stasis_json/resource_asterisk.h (original)
+++ team/dlee/ari-event-remodel/res/stasis_json/resource_asterisk.h Fri Jun 14 15:55:40 2013
@@ -1,46 +1,0 @@
-/*
- * Asterisk -- An open source telephony toolkit.
- *
- * Copyright (C) 2012 - 2013, Digium, Inc.
- *
- * David M. Lee, II <dlee at digium.com>
- *
- * See http://www.asterisk.org for more information about
- * the Asterisk project. Please do not directly contact
- * any of the maintainers of this project for assistance;
- * the project provides a web site, mailing lists and IRC
- * channels for your use.
- *
- * This program is free software, distributed under the terms of
- * the GNU General Public License Version 2. See the LICENSE file
- * at the top of the source tree.
- */
-
-/*! \file
- *
- * \brief Generated file - declares stubs to be implemented in
- * res/stasis_json/resource_asterisk.c
- *
- * Asterisk resources
- *
- * \author David M. Lee, II <dlee at digium.com>
- */
-
-/*
- * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- * !!!!!                               DO NOT EDIT                        !!!!!
- * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- * This file is generated by a mustache template. Please see the original
- * template in rest-api-templates/stasis_http_resource.h.mustache
- */
-
-#ifndef _ASTERISK_RESOURCE_ASTERISK_H
-#define _ASTERISK_RESOURCE_ASTERISK_H
-
-/*
- * JSON models
- *
- * AsteriskInfo
- */
-
-#endif /* _ASTERISK_RESOURCE_ASTERISK_H */

Modified: team/dlee/ari-event-remodel/rest-api-templates/asterisk_processor.py
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/rest-api-templates/asterisk_processor.py?view=diff&rev=391891&r1=391890&r2=391891
==============================================================================
--- team/dlee/ari-event-remodel/rest-api-templates/asterisk_processor.py (original)
+++ team/dlee/ari-event-remodel/rest-api-templates/asterisk_processor.py Fri Jun 14 15:55:40 2013
@@ -144,17 +144,6 @@
                 segment = resource_api.root_path.get_child(api.path.split('/'))
                 for operation in api.operations:
                     segment.operations.append(operation)
-            resource_api.api_declaration.has_events = False
-            for model in resource_api.api_declaration.models:
-                if model.id == "Event":
-                    resource_api.api_declaration.has_events = True
-                    break
-            if resource_api.api_declaration.has_events:
-                resource_api.api_declaration.events = \
-                    [self.process_model(model, context) for model in \
-                        resource_api.api_declaration.models if model.id != "Event"]
-            else:
-                resource_api.api_declaration.events = []
 
             # Since every API path should start with /[resource], root should
             # have exactly one child.
@@ -191,41 +180,14 @@
 
     def process_model(self, model, context):
         model.description_dox = model.description.replace('\n', '\n * ')
+        model.description_dox = re.sub(' *\n', '\n', model.description_dox)
         model.c_id = snakify(model.id)
-        model.channel = False
-        model.channel_desc = ""
-        model.bridge = False
-        model.bridge_desc = ""
-        model.properties = [self.process_property(model, prop, context) for prop in model.properties]
-        model.properties = [prop for prop in model.properties if prop]
 	model.has_properties = (len(model.properties) != 0)
         return model
 
-    def process_property(self, model, prop, context):
-        # process channel separately since it will be pulled out
-        if prop.name == 'channel' and prop.type == 'Channel':
-            model.channel = True
-            model.channel_desc = prop.description or ""
-            return None
-
-        # process bridge separately since it will be pulled out
-        if prop.name == 'bridge' and prop.type == 'Bridge':
-            model.bridge = True
-            model.bridge_desc = prop.description or ""
-            return None
-
-	prop.c_name = snakify(prop.name)
-        if prop.type in self.type_mapping:
-            prop.c_type = self.type_mapping[prop.type]
-            prop.c_convert = self.convert_mapping[prop.c_type]
-        else:
-            prop.c_type = "Property type %s not mappable to a C type" % (prop.type)
-            prop.c_convert = "Property type %s not mappable to a C conversion" % (prop.type)
-            #raise SwaggerError(
-            #    "Invalid property type %s" % prop.type, context)
-        # You shouldn't put a space between 'char *' and the variable
-        if prop.c_type.endswith('*'):
-            prop.c_space = ''
-        else:
-            prop.c_space = ' '
-        return prop
+    def process_property(self, prop, context):
+	if "-" in prop.name:
+            raise SwaggerError("Property names cannot have dashes", context)
+        if prop.name != prop.name.lower():
+            raise SwaggerError("Property name should not be lowercase", context)
+        prop.snake_type = snakify(prop.type)

Modified: team/dlee/ari-event-remodel/rest-api-templates/make_stasis_http_stubs.py
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/rest-api-templates/make_stasis_http_stubs.py?view=diff&rev=391891&r1=391890&r2=391891
==============================================================================
--- team/dlee/ari-event-remodel/rest-api-templates/make_stasis_http_stubs.py (original)
+++ team/dlee/ari-event-remodel/rest-api-templates/make_stasis_http_stubs.py Fri Jun 14 15:55:40 2013
@@ -42,21 +42,17 @@
 
 API_TRANSFORMS = [
     Transform(rel('res_stasis_http_resource.c.mustache'),
-              'res_stasis_http_{{name}}.c'),
+              'res/res_stasis_http_{{name}}.c'),
     Transform(rel('stasis_http_resource.h.mustache'),
-              'stasis_http/resource_{{name}}.h'),
+              'res/stasis_http/resource_{{name}}.h'),
     Transform(rel('stasis_http_resource.c.mustache'),
-              'stasis_http/resource_{{name}}.c', False),
-    Transform(rel('res_stasis_json_resource.c.mustache'),
-              'res_stasis_json_{{name}}.c'),
-    Transform(rel('res_stasis_json_resource.exports.mustache'),
-              'res_stasis_json_{{name}}.exports.in'),
-    Transform(rel('stasis_json_resource.h.mustache'),
-              'stasis_json/resource_{{name}}.h'),
+              'res/stasis_http/resource_{{name}}.c', overwrite=False),
 ]
 
 RESOURCES_TRANSFORMS = [
     Transform(rel('stasis_http.make.mustache'), 'stasis_http.make'),
+    Transform(rel('ari_model.h.mustache'), 'include/asterisk/ari_model.h'),
+    Transform(rel('res_ari_model.c.mustache'), 'res/res_ari_model.c'),
 ]
 
 

Modified: team/dlee/ari-event-remodel/rest-api-templates/swagger_model.py
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/rest-api-templates/swagger_model.py?view=diff&rev=391891&r1=391890&r2=391891
==============================================================================
--- team/dlee/ari-event-remodel/rest-api-templates/swagger_model.py (original)
+++ team/dlee/ari-event-remodel/rest-api-templates/swagger_model.py Fri Jun 14 15:55:40 2013
@@ -29,6 +29,7 @@
 import json
 import os.path
 import pprint
+import re
 import sys
 import traceback
 
@@ -84,7 +85,6 @@
         if not id_field in json:
             raise SwaggerError("Missing id_field: %s" % id_field, self)
         new_stack = self.stack + ['%s=%s' % (id_field, str(json[id_field]))]
-        print new_stack
         return ParsingContext(self.swagger_version, new_stack)
 
     def next(self, version=None, stack=None):
@@ -135,6 +135,22 @@
         """Post process a Parameter object.
 
         @param parameter: Parameter object.
+        @param context: Current context in the API.
+        """
+        pass
+
+    def process_model(self, model, context):
+        """Post process a Model object.
+
+        @param model: Model object.
+        @param context: Current context in the API.
+        """
+        pass
+
+    def process_property(self, property, context):
+        """Post process a Property object.
+
+        @param property: Property object.
         @param context: Current context in the API.
         """
         pass
@@ -328,12 +344,25 @@
 
     def load(self, property_json, processor, context):
         validate_required_fields(property_json, self.required_fields, context)
+        # Bit of a hack, but properties do not self-identify
+        context = context.next_stack({'name': self.name}, 'name')
         self.type = property_json.get('type')
         self.description = property_json.get('description') or ''
         self.required = property_json.get('required') or False
-        if self.type == 'DISCRIMINATOR' and context.is_version("1.1"):
-            raise SwaggerError("DISCRIMINATOR support added in Swagger 1.2",
-                               context)
+        self.is_discriminator = self.type == 'DISCRIMINATOR'
+        if self.is_discriminator:
+            self.type = "string"
+            if context.is_version("1.1"):
+                raise SwaggerError(
+                    "DISCRIMINATOR support added in Swagger 1.2", context)
+        list_match = re.match('^List\[(.*)\]$', self.type)
+        self.is_list = list_match is not None
+        if self.is_list:
+            self.type_param = list_match.group(1)
+        # Some common errors
+        if self.type == 'integer':
+            raise SwaggerError("The type for integer should be 'int'", context)
+        processor.process_property(self, context)
         return self
 
 
@@ -342,6 +371,8 @@
 
     See https://github.com/wordnik/swagger-core/wiki/datatypes
     """
+
+    required_fields = ['description', 'properties']
 
     def __init__(self):
         self.id = None
@@ -351,14 +382,20 @@
 
     def load(self, id, model_json, processor, context):
         context = context.next_stack(model_json, 'id')
-        # This arrangement is required by the Swagger API spec
+        validate_required_fields(model_json, self.required_fields, context)
+        # The duplication of the model's id is required by the Swagger spec.
         self.id = model_json.get('id')
         if id != self.id:
             raise SwaggerError("Model id doesn't match name", c)
+        self.extends = model_json.get('extends')
+        if self.extends and context.is_version("1.1"):
+            raise SwaggerError("Type extension support added in Swagger 1.2")
         self.description = model_json.get('description')
         props = model_json.get('properties').items() or []
         self.properties = [
             Property(k).load(j, processor, context) for (k, j) in props]
+        self.properties = sorted(self.properties, key=lambda p: p.name)
+        processor.process_model(self, context)
         return self
 
 
@@ -430,7 +467,8 @@
             Api().load(j, processor, context) for j in api_json]
         models = api_decl_json.get('models').items() or []
         self.models = [
-            Model().load(k, j, processor, context) for (k, j) in models]
+            Model().load(id, json, processor, context) for (id, json) in models]
+        self.models = sorted(self.models, key=lambda m: m.id)
 
         return self
 

Modified: team/dlee/ari-event-remodel/rest-api/api-docs/bridges.json
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/rest-api/api-docs/bridges.json?view=diff&rev=391891&r1=391890&r2=391891
==============================================================================
--- team/dlee/ari-event-remodel/rest-api/api-docs/bridges.json (original)
+++ team/dlee/ari-event-remodel/rest-api/api-docs/bridges.json Fri Jun 14 15:55:40 2013
@@ -234,7 +234,7 @@
 			"id": "Bridge",
 			"description": "The merging of media from one or more channels.\n\nEveryone on the bridge receives the same audio.",
 			"properties": {
-				"bridgeType": {
+				"bridge_type": {
 					"type": "string",
 					"description": "Type of bridge technology",
 					"required": true,

Modified: team/dlee/ari-event-remodel/rest-api/api-docs/events.json
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/rest-api/api-docs/events.json?view=diff&rev=391891&r1=391890&r2=391891
==============================================================================
--- team/dlee/ari-event-remodel/rest-api/api-docs/events.json (original)
+++ team/dlee/ari-event-remodel/rest-api/api-docs/events.json Fri Jun 14 15:55:40 2013
@@ -166,7 +166,7 @@
 				"cause": {
 					"required": true,
 					"description": "Integer representation of the cause of the hangup",
-					"type": "integer"
+					"type": "int"
 				},
 				"cause_txt": {
 					"required": true,
@@ -265,7 +265,7 @@
 			"properties": {
 				"caller_presentation": {
 					"required": true,
-					"type": "integer",
+					"type": "int",
 					"description": "The integer representation of the Caller Presentation value."
 				},
 				"caller_presentation_txt": {
@@ -303,7 +303,7 @@
 			"description": "A hangup was requested on the channel.",
 			"properties": {
 				"cause": {
-					"type": "integer",
+					"type": "int",
 					"description": "Integer representation of the cause of the hangup."
 				},
 				"soft": {

Modified: team/dlee/ari-event-remodel/rest-api/api-docs/recordings.json
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/rest-api/api-docs/recordings.json?view=diff&rev=391891&r1=391890&r2=391891
==============================================================================
--- team/dlee/ari-event-remodel/rest-api/api-docs/recordings.json (original)
+++ team/dlee/ari-event-remodel/rest-api/api-docs/recordings.json Fri Jun 14 15:55:40 2013
@@ -238,7 +238,7 @@
 					"required": true,
 					"type": "List[string]"
 				},
-				"durationSeconds": {
+				"duration_seconds": {
 					"required": false,
 					"type": "int"
 				},




More information about the svn-commits mailing list