Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mods to add Camel and snake_case for proto #13

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions run/proto-sample.yaml
Original file line number Diff line number Diff line change
@@ -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
87 changes: 69 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ function toProto(doc) {
var rtn = "";
var obj;
var coll;
var val;

// preamble
rtn += 'syntax = "proto3";\n';
Expand All @@ -185,65 +186,81 @@ function toProto(doc) {
// params
coll = doc.alps.descriptor.filter(semantic);
coll.forEach(function(msg) {
rtn += `message ${msg.id}Params {\n`;
val = makeSnakeCase(msg.id);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makePascalCase() should be applied here

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. will make this change.-- #thanks

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@corntoole :

pretty sure these changes resullt in an invalid proto file:

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) {};
}

wdyt?

rtn += `message ${val}_params {\n`;
Copy link

@corntoole corntoole Jan 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-- updated --
Also capture a snake-case formatted value to be used in the next hunk.

val1 = makeSnakeCase(msg.id);

var c = 0;
c++;
rtn += ` string ${msg.id} = ${c};\n`;
rtn += ` string ${val} = ${c};\n`;
Copy link

@corntoole corntoole Jan 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-- update --
This should be snake-case.

use val1 (whatever variable you used to store the snake-case ID) here.

    rtn += `  string ${val1} = ${c};\n`;    

rtn += '}\n';
});
rtn += '\n';

// objects
coll = doc.alps.descriptor.filter(groups);
coll.forEach(function(msg) {
rtn += `message ${msg.id} {\n`;
val = makeSnakeCase(msg.id);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pascal case should be applied here as well.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. will make this change.-- #thanks

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above -- i think the snake and pascal casing routines no longer match

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';
Expand Down Expand Up @@ -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
//*******************************************
Expand Down