From 2393a5d7b8ad1947cb2dce1096acc8008d837d3b Mon Sep 17 00:00:00 2001 From: mamund Date: Thu, 31 Dec 2020 15:07:19 -0500 Subject: [PATCH 1/6] mods to add Camel and snake_case for proto --- run/proto-sample.yaml | 89 +++++++++++++++++++++++++++++++++++++++++++ src/index.js | 87 +++++++++++++++++++++++++++++++++--------- 2 files changed, 158 insertions(+), 18 deletions(-) create mode 100644 run/proto-sample.yaml diff --git a/run/proto-sample.yaml b/run/proto-sample.yaml new file mode 100644 index 0000000..fb935ad --- /dev/null +++ b/run/proto-sample.yaml @@ -0,0 +1,89 @@ +alps: + version: "1.0" + name: microblogging + doc: "The example profile here contains details on customizing any representation media type for a specific \n application domain: Micro-blogging. It contains descriptions of valid data and transition values that \n can appear within resource representations. This document is presented as a complete blueprint for \n implementing a compliant client or server that supports the basic features of the target application \n domain (Micro-blogging)." + # metadata + ext: + - type: metadata + name: title + value: microblogging + tags: "oas" + - type: metadata + name: id + value: https://corntoole.github.io/api-profiles/microblogging + tags: "oas" + - type: metadata + name: root + value: http://api.example.org/microblogging + tags: "oas" + link: + rel: help + href: "http://amundsen.com/hypermedia/profiles/" + descriptor: + - doc: "Contains the UTC date-time the message was posted. When present, it SHOULD be valid per RFC3339." + id: "date-time" + type: semantic + - doc: "Contains the text description of a user." + id: description + type: semantic + - doc: "" + id: xx + type: semantic + - doc: "A list representation of unfiltered results." + id: all + type: semantic + descriptor: + - href: "#users" + - href: "#messages" + - id: friends + type: semantic + doc: "A list representation." + descriptor: + - href: "#users" + - href: "#messages" + - id: followers + type: semantic + doc: "A list representation of all the users from the designated user's friends list." + descriptor: + href: "#users" + - id: me + type: semantic + doc: "Contains the list of messages posted by the designated user or contains\n the designated user's profile." + descriptor: + - href: "#users" + - href: "#messages" + - id: mentions + type: semantic + doc: "A list representation of all the messages that mention the designated user" + descriptor: + href: "#messages" + - id: message + type: semantic + doc: "A representation of a single message." + descriptor: + - href: "#user-text" + - href: "#user-link" + - href: "#message-text" + - href: "#message-link" + - href: "#user-image" + - href: "#date-time" + - id: empty + - doc: "A link template to add a new message to the system by the designated (logged-in) user." + descriptor: + - href: "#message-text" + rt: empty + id: "message-post" + type: unsafe + - doc: "A link template to reply to an existing message." + descriptor: + - doc: "the author of the post to be replied-to" + href: "#user-text" + name: user + - href: "#message-text" + name: message + id: "message-reply" + type: unsafe + rt: empty + # - doc: "" + # id: xx + # type: semantic diff --git a/src/index.js b/src/index.js index 8513e54..56b5e05 100755 --- a/src/index.js +++ b/src/index.js @@ -167,6 +167,7 @@ function toProto(doc) { var rtn = ""; var obj; var coll; + var val; // preamble rtn += 'syntax = "proto3";\n'; @@ -185,10 +186,11 @@ function toProto(doc) { // params coll = doc.alps.descriptor.filter(semantic); coll.forEach(function(msg) { - rtn += `message ${msg.id}Params {\n`; + val = makeSnakeCase(msg.id); + rtn += `message ${val}_params {\n`; var c = 0; c++; - rtn += ` string ${msg.id} = ${c};\n`; + rtn += ` string ${val} = ${c};\n`; rtn += '}\n'; }); rtn += '\n'; @@ -196,54 +198,69 @@ function toProto(doc) { // objects coll = doc.alps.descriptor.filter(groups); coll.forEach(function(msg) { - rtn += `message ${msg.id} {\n`; + val = makeSnakeCase(msg.id); + rtn += `message ${val} {\n`; var c = 0; msg.descriptor.forEach(function(prop) { c++; rtn += ` string ${prop.href} = ${c};\n`; }); rtn += '}\n'; - rtn += `message ${msg.id}Response {\n`; - rtn += ` repeated ${msg.id} ${msg.id}Collection = 1;\n` + rtn += `message ${val}_response {\n`; + rtn += ` repeated ${val} ${val}_collection = 1;\n` rtn += '}\n'; - rtn += `message ${msg.id}Empty {}\n`; + rtn += `message ${val}_empty {}\n`; }); rtn += '\n'; // procedures - rtn += `service ${doc.alps.ext.filter(metadata_title)[0].value.replace(/ /g,'_')||"ALPS_API"}_Service {\n`; + val = doc.alps.ext.filter(metadata_title)[0].value.replace(/ /g,'-')||"ALPS-API"; + val = makePascalCase(val) + rtn += `service ${val}Service {\n`; coll = doc.alps.descriptor.filter(safe); coll.forEach(function(item) { - rtn += ` rpc ${item.id}(` + val = item.id; + val = makePascalCase(val); + rtn += ` rpc ${val}(` if(item.descriptor) { - rtn += item.descriptor[0].href; + val = makeSnakeCase(item.descriptor[0].href); + rtn += val; } else { - rtn += `${item.rt}Empty`; + val = makeSnakeCase(item.rt); + rtn += `${val}_empty`; } - rtn += `) returns (${item.rt}Response) {};\n`; + rtn += `) returns (${val}_response) {};\n`; }); coll = doc.alps.descriptor.filter(unsafe); coll.forEach(function(item) { - rtn += ` rpc ${item.id}(` + var val = item.id; + val = makePascalCase(val); + rtn += ` rpc ${val}(` if(item.descriptor) { - rtn += item.descriptor[0].href; + val = makeSnakeCase(item.descriptor[0].href); + rtn += val; } - rtn += `) returns (${item.rt}Response) {};\n`; + val = makeSnakeCase(item.rt); + rtn += `) returns (${val}_response) {};\n`; }); coll = doc.alps.descriptor.filter(idempotent); coll.forEach(function(item) { - rtn += ` rpc ${item.id}(` + var val = item.id; + val = makePascalCase(val); + rtn += ` rpc ${val}(` if(item.descriptor) { - rtn += item.descriptor[0].href; + val = makeSnakeCase(item.descriptor[0].href); + rtn += val; if(item.descriptor[0].href === "#id") { - rtn += "Params"; + rtn += "_params"; } } - rtn += `) returns (${item.rt}Response) {};\n`; + val = makeSnakeCase(item.rt); + rtn += `) returns (${val}_response) {};\n`; }); rtn += '}\n'; @@ -516,6 +533,40 @@ function toAsync(doc) { return rtn; } +//******************************************* +// general support +//******************************************* + +function makePascalCase(value) { + var rtn = ""; + var coll = []; + coll = value.split('-'); + if(coll.length===0) { + coll = value.split('_'); + } + if(coll.length===0) { + coll.push(rtn); + } + coll.forEach(function(item) { + rtn += item.charAt(0).toUpperCase() + item.slice(1) + }); + return rtn; +} + +function makeSnakeCase(value) { + var rtn = ""; + var coll = []; + coll = value.split('-'); + if(coll.length===0) { + coll.push(rtn); + } + coll.forEach(function(item) { + rtn += item.toLowerCase()+'_'; + }); + rtn = rtn.substring(0, rtn.length - 1); + return rtn; +} + //******************************************* // collection filters //******************************************* From 7856cba485aed617b022164507409d098b44c277 Mon Sep 17 00:00:00 2001 From: mamund Date: Thu, 31 Dec 2020 15:08:21 -0500 Subject: [PATCH 2/6] tweak code --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 56b5e05..4ead519 100755 --- a/src/index.js +++ b/src/index.js @@ -534,7 +534,7 @@ function toAsync(doc) { } //******************************************* -// general support +// general support routines //******************************************* function makePascalCase(value) { From 5616ea1cd197541b091b2410b7d95347541cc506 Mon Sep 17 00:00:00 2001 From: mamund Date: Thu, 31 Dec 2020 15:09:00 -0500 Subject: [PATCH 3/6] tweak code for issue #10 --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 4ead519..def7ee5 100755 --- a/src/index.js +++ b/src/index.js @@ -534,7 +534,7 @@ function toAsync(doc) { } //******************************************* -// general support routines +// general support //******************************************* function makePascalCase(value) { From e9d560d80083e568f1fa65565307f7b66bb7b66d Mon Sep 17 00:00:00 2001 From: mamund Date: Thu, 31 Dec 2020 19:33:43 -0500 Subject: [PATCH 4/6] mods to improve proto/sdl generation --- run/todo-alps.proto | 30 +++++++++++++++++++++++ run/todo-alps.yaml | 16 ++++++------ src/index.js | 59 +++++++++++++++++++++++++++++++++++---------- 3 files changed, 84 insertions(+), 21 deletions(-) create mode 100644 run/todo-alps.proto diff --git a/run/todo-alps.proto b/run/todo-alps.proto new file mode 100644 index 0000000..7381a0b --- /dev/null +++ b/run/todo-alps.proto @@ -0,0 +1,30 @@ +syntax = "proto3"; +package ToDo_API; + +// ******************************************************************* +// generated by "unified" from todo-alps.yaml +// date: Thu Dec 31 2020 19:28:00 GMT-0500 (Eastern Standard Time) +// http://github.com/mamund/2020-11-unified +// ******************************************************************* + +message Id_params { + string Id = 1; +} +message Body_params { + string Body = 1; +} + +message TodoItem { + string id = 1; + string body = 2; +} +message TodoItem_response { + repeated TodoItem TodoItem_collection = 1; +} +message TodoItem_empty {} + +service TodoApiService { + rpc TodoList(todo_item_empty) returns (todo_item_response) {}; + rpc TodoAdd(todo_item) returns (todo_item_response) {}; + rpc TodoRemove(id_params) returns (todo_item_response) {}; +} diff --git a/run/todo-alps.yaml b/run/todo-alps.yaml index 1d955e7..cde0a61 100644 --- a/run/todo-alps.yaml +++ b/run/todo-alps.yaml @@ -42,7 +42,7 @@ alps: # groupings # - these are the storage objects - - id: todoItem + - id: todo-Item type: group text: todo item descriptor: @@ -51,22 +51,22 @@ alps: # actions # - these are the operations - - id: todoList + - id: todo-List type: safe - rt: todoItem + rt: todo-Item title: return list of todo items - - id: todoAdd + - id: todo-Add type: unsafe - rt: todoItem + rt: todo-Item title: create a new todo item descriptor: - - href: '#todoItem' + - href: '#todo-Item' - - id: todoRemove + - id: todo-Remove type: idempotent tags: delete - rt: todoItem + rt: todo-Item title: remove a single todo item descriptor: - href: '#id' diff --git a/src/index.js b/src/index.js index def7ee5..c3c2d87 100755 --- a/src/index.js +++ b/src/index.js @@ -186,7 +186,7 @@ function toProto(doc) { // params coll = doc.alps.descriptor.filter(semantic); coll.forEach(function(msg) { - val = makeSnakeCase(msg.id); + val = makePascalCase(msg.id); rtn += `message ${val}_params {\n`; var c = 0; c++; @@ -198,7 +198,7 @@ function toProto(doc) { // objects coll = doc.alps.descriptor.filter(groups); coll.forEach(function(msg) { - val = makeSnakeCase(msg.id); + val = makePascalCase(msg.id); rtn += `message ${val} {\n`; var c = 0; msg.descriptor.forEach(function(prop) { @@ -279,7 +279,8 @@ function toProto(doc) { function toSDL(doc) { var rtn = ""; var coll; - + var val1, val2; + // signature rtn += '?? *******************************************************************\n'; rtn += `?? generated by "unified" from ${options.file}\n`; @@ -292,9 +293,11 @@ function toSDL(doc) { // types coll = doc.alps.descriptor.filter(groups); coll.forEach(function(item) { - rtn += `type ${item.id} {\n`; + val1 = makePascalCase(item.id); + rtn += `type ${val1} {\n`; item.descriptor.forEach(function(prop) { - rtn += ` ${prop.href}: String!\n`; + val1 = makeCamelCase(prop.href); + rtn += ` ${val1}: String!\n`; }); rtn += '}\n'; }); @@ -303,8 +306,10 @@ function toSDL(doc) { // query coll = doc.alps.descriptor.filter(safe); coll.forEach(function(item) { + val1 = makeCamelCase(item.id); + val2 = makeCamelCase(item.rt); rtn += 'type Query {\n'; - rtn += ` ${item.id}: [${item.rt}]\n`; + rtn += ` ${val1}: [${val2}]\n`; rtn += '}\n'; }); rtn += '\n'; @@ -313,19 +318,25 @@ function toSDL(doc) { rtn += 'type Mutation {\n'; coll = doc.alps.descriptor.filter(unsafe); coll.forEach(function(item) { - rtn += ` ${item.id}(`; + val1 = makeCamelCase(item.id); + rtn += ` ${val1}(`; if(item.descriptor) { - rtn += `${item.descriptor[0].href}: String!`; + val1 = makeCamelCase(item.descriptor[0].href); + rtn += `${val1}: String!`; } - rtn += `): ${item.rt}\n`; + val1 = makeCamelCase(item.rt); + rtn += `): ${val1}\n`; }); coll = doc.alps.descriptor.filter(idempotent); coll.forEach(function(item) { - rtn += ` ${item.id}(`; + val1 = makeCamelCase(item.id); + rtn += ` ${val1}(`; if(item.descriptor) { - rtn += `${item.descriptor[0].href}: String!`; + val1 = makeCamelCase(item.descriptor[0].href); + rtn += `${val1}: String!`; } - rtn += `): ${item.rt}\n`; + val1 = makeCamelCase(item.rt); + rtn += `): ${val1}\n`; }); rtn += '}\n'; @@ -548,7 +559,7 @@ function makePascalCase(value) { coll.push(rtn); } coll.forEach(function(item) { - rtn += item.charAt(0).toUpperCase() + item.slice(1) + rtn += item.charAt(0).toUpperCase() + item.slice(1).toLowerCase() }); return rtn; } @@ -567,6 +578,28 @@ function makeSnakeCase(value) { return rtn; } +function makeCamelCase(value) { + var rtn = ""; + var coll = []; + seg = ""; + x = 0; + coll = value.split('-'); + if(coll.length===0) { + coll.push(rtn); + } + coll.forEach(function(item) { + seg = ""; + if(x===0) { + seg = item.toLowerCase(); + } else { + set = item.charAt(0).toUpperCase() + item.slice(1).toLowerCase(); + } + rtn += seg; + x += 1; + }); + return rtn; +} + //******************************************* // collection filters //******************************************* From 397d97160fad65774b67f09e5259527170358663 Mon Sep 17 00:00:00 2001 From: mamund Date: Fri, 1 Jan 2021 13:55:29 -0500 Subject: [PATCH 5/6] update branch to force proto vlaidation --- run/todo-alps.proto | 12 ++++----- src/index.js | 63 +++++++++++++++++++++++---------------------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/run/todo-alps.proto b/run/todo-alps.proto index 7381a0b..a9d7848 100644 --- a/run/todo-alps.proto +++ b/run/todo-alps.proto @@ -3,7 +3,7 @@ package ToDo_API; // ******************************************************************* // generated by "unified" from todo-alps.yaml -// date: Thu Dec 31 2020 19:28:00 GMT-0500 (Eastern Standard Time) +// date: Fri Jan 01 2021 13:49:19 GMT-0500 (Eastern Standard Time) // http://github.com/mamund/2020-11-unified // ******************************************************************* @@ -15,8 +15,8 @@ message Body_params { } message TodoItem { - string id = 1; - string body = 2; + string Id = 1; + string Body = 2; } message TodoItem_response { repeated TodoItem TodoItem_collection = 1; @@ -24,7 +24,7 @@ message TodoItem_response { message TodoItem_empty {} service TodoApiService { - rpc TodoList(todo_item_empty) returns (todo_item_response) {}; - rpc TodoAdd(todo_item) returns (todo_item_response) {}; - rpc TodoRemove(id_params) returns (todo_item_response) {}; + rpc TodoList(TodoItem_empty) returns (TodoItem_response) {}; + rpc TodoAdd(TodoItem) returns (TodoItem_response) {}; + rpc TodoRemove(Id_params) returns (TodoItem_response) {}; } diff --git a/src/index.js b/src/index.js index c3c2d87..801e787 100755 --- a/src/index.js +++ b/src/index.js @@ -167,7 +167,7 @@ function toProto(doc) { var rtn = ""; var obj; var coll; - var val; + var val, val1, val2; // preamble rtn += 'syntax = "proto3";\n'; @@ -198,40 +198,41 @@ function toProto(doc) { // objects coll = doc.alps.descriptor.filter(groups); coll.forEach(function(msg) { - val = makePascalCase(msg.id); - rtn += `message ${val} {\n`; + val1 = makePascalCase(msg.id); + rtn += `message ${val1} {\n`; var c = 0; msg.descriptor.forEach(function(prop) { c++; - rtn += ` string ${prop.href} = ${c};\n`; + val2 = makePascalCase(prop.href.slice(1)); + rtn += ` string ${val2} = ${c};\n`; }); rtn += '}\n'; - rtn += `message ${val}_response {\n`; - rtn += ` repeated ${val} ${val}_collection = 1;\n` + rtn += `message ${val1}_response {\n`; + rtn += ` repeated ${val1} ${val1}_collection = 1;\n` rtn += '}\n'; - rtn += `message ${val}_empty {}\n`; + rtn += `message ${val1}_empty {}\n`; }); rtn += '\n'; // procedures - val = doc.alps.ext.filter(metadata_title)[0].value.replace(/ /g,'-')||"ALPS-API"; - val = makePascalCase(val) - rtn += `service ${val}Service {\n`; + val1 = doc.alps.ext.filter(metadata_title)[0].value.replace(/ /g,'-')||"ALPS-API"; + val1 = makePascalCase(val1) + rtn += `service ${val1}Service {\n`; coll = doc.alps.descriptor.filter(safe); coll.forEach(function(item) { - val = item.id; - val = makePascalCase(val); - rtn += ` rpc ${val}(` + val1 = item.id; + val1 = makePascalCase(val1); + rtn += ` rpc ${val1}(` if(item.descriptor) { - val = makeSnakeCase(item.descriptor[0].href); - rtn += val; + val1 = makePascalCase(item.descriptor[0].href.slice(1)); + rtn += val1; } else { - val = makeSnakeCase(item.rt); - rtn += `${val}_empty`; + val1 = makePascalCase(item.rt); + rtn += `${val1}_empty`; } - rtn += `) returns (${val}_response) {};\n`; + rtn += `) returns (${val1}_response) {};\n`; }); coll = doc.alps.descriptor.filter(unsafe); @@ -240,10 +241,10 @@ function toProto(doc) { val = makePascalCase(val); rtn += ` rpc ${val}(` if(item.descriptor) { - val = makeSnakeCase(item.descriptor[0].href); + val = makePascalCase(item.descriptor[0].href.slice(1)); rtn += val; } - val = makeSnakeCase(item.rt); + val = makePascalCase(item.rt); rtn += `) returns (${val}_response) {};\n`; }); @@ -253,13 +254,13 @@ function toProto(doc) { val = makePascalCase(val); rtn += ` rpc ${val}(` if(item.descriptor) { - val = makeSnakeCase(item.descriptor[0].href); + val = makePascalCase(item.descriptor[0].href.slice(1)); rtn += val; if(item.descriptor[0].href === "#id") { rtn += "_params"; } } - val = makeSnakeCase(item.rt); + val = makePascalCase(item.rt); rtn += `) returns (${val}_response) {};\n`; }); @@ -296,7 +297,7 @@ function toSDL(doc) { val1 = makePascalCase(item.id); rtn += `type ${val1} {\n`; item.descriptor.forEach(function(prop) { - val1 = makeCamelCase(prop.href); + val1 = makePascalCase(prop.href); rtn += ` ${val1}: String!\n`; }); rtn += '}\n'; @@ -306,8 +307,8 @@ function toSDL(doc) { // query coll = doc.alps.descriptor.filter(safe); coll.forEach(function(item) { - val1 = makeCamelCase(item.id); - val2 = makeCamelCase(item.rt); + val1 = makePascalCase(item.id); + val2 = makePascalCase(item.rt); rtn += 'type Query {\n'; rtn += ` ${val1}: [${val2}]\n`; rtn += '}\n'; @@ -318,10 +319,10 @@ function toSDL(doc) { rtn += 'type Mutation {\n'; coll = doc.alps.descriptor.filter(unsafe); coll.forEach(function(item) { - val1 = makeCamelCase(item.id); + val1 = makePascalCase(item.id); rtn += ` ${val1}(`; if(item.descriptor) { - val1 = makeCamelCase(item.descriptor[0].href); + val1 = makePascalCase(item.descriptor[0].href); rtn += `${val1}: String!`; } val1 = makeCamelCase(item.rt); @@ -329,13 +330,13 @@ function toSDL(doc) { }); coll = doc.alps.descriptor.filter(idempotent); coll.forEach(function(item) { - val1 = makeCamelCase(item.id); + val1 = makePascalCase(item.id); rtn += ` ${val1}(`; if(item.descriptor) { - val1 = makeCamelCase(item.descriptor[0].href); + val1 = makePascalCase(item.descriptor[0].href); rtn += `${val1}: String!`; } - val1 = makeCamelCase(item.rt); + val1 = makePascalCase(item.rt); rtn += `): ${val1}\n`; }); rtn += '}\n'; @@ -556,7 +557,7 @@ function makePascalCase(value) { coll = value.split('_'); } if(coll.length===0) { - coll.push(rtn); + coll.push(value); } coll.forEach(function(item) { rtn += item.charAt(0).toUpperCase() + item.slice(1).toLowerCase() From 16ed0f1ba88aa1c11358481153cb363a68021912 Mon Sep 17 00:00:00 2001 From: mamund Date: Sat, 20 Mar 2021 13:23:27 -0400 Subject: [PATCH 6/6] update ACLs --- src/index.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 src/index.js diff --git a/src/index.js b/src/index.js old mode 100755 new mode 100644