Skip to content

Commit

Permalink
refactor(samples): use pb-util to encode/decode structs (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
callmehiphop authored and JustinBeckwith committed Mar 28, 2019
1 parent 6bcbec1 commit 94b849b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 118 deletions.
14 changes: 5 additions & 9 deletions dialogflow/detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

const util = require('util');
const fs = require('fs');
const structjson = require('./structjson.js');
const {struct} = require('pb-util');
const pump = require('pump');
const through2 = require('through2');

Expand Down Expand Up @@ -67,9 +67,7 @@ function detectTextIntent(projectId, sessionId, queries, languageCode) {
// value contains fields with value of null, which causes error
// when encoding it back. Converting to JSON and back to proto
// removes those values.
context.parameters = structjson.jsonToStructProto(
structjson.structProtoToJson(context.parameters)
);
context.parameters = struct.encode(struct.decode(context.parameters));
});
request.queryParams = {
contexts: response.queryResult.outputContexts,
Expand Down Expand Up @@ -114,7 +112,7 @@ async function detectEventIntent(
queryInput: {
event: {
name: eventName,
parameters: structjson.jsonToStructProto({foo: 'bar'}),
parameters: struct.encode({foo: 'bar'}),
languageCode: languageCode,
},
},
Expand Down Expand Up @@ -254,16 +252,14 @@ function logQueryResult(sessionClient, result) {
} else {
console.log(` No intent matched.`);
}
const parameters = JSON.stringify(
structjson.structProtoToJson(result.parameters)
);
const parameters = JSON.stringify(struct.decode(result.parameters));
console.log(` Parameters: ${parameters}`);
if (result.outputContexts && result.outputContexts.length) {
console.log(` Output contexts:`);
result.outputContexts.forEach(context => {
const contextId = contextClient.matchContextFromContextName(context.name);
const contextParameters = JSON.stringify(
structjson.structProtoToJson(context.parameters)
struct.decode(context.parameters)
);
console.log(` ${contextId}`);
console.log(` lifespan: ${context.lifespanCount}`);
Expand Down
8 changes: 3 additions & 5 deletions dialogflow/detect.v2beta1.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
'use strict';

const projectId = process.env.GCLOUD_PROJECT;
const {struct} = require('pb-util');
const sessionId = require('uuid/v1')();
const util = require('util');
const structjson = require('./structjson.js');

async function createKnowledgeBase(projectId, displayName) {
// [START dialogflow_create_knowledge_base]
Expand Down Expand Up @@ -482,16 +482,14 @@ async function detectIntentwithModelSelection(
} else {
console.log(` No intent matched.`);
}
const parameters = JSON.stringify(
structjson.structProtoToJson(result.parameters)
);
const parameters = JSON.stringify(struct.decode(result.parameters));
console.log(` Parameters: ${parameters}`);
if (result.outputContexts && result.outputContexts.length) {
console.log(` Output contexts:`);
result.outputContexts.forEach(context => {
const contextId = contextClient.matchContextFromContextName(context.name);
const contextParameters = JSON.stringify(
structjson.structProtoToJson(context.parameters)
struct.decode(context.parameters)
);
console.log(` ${contextId}`);
console.log(` lifespan: ${context.lifespanCount}`);
Expand Down
5 changes: 3 additions & 2 deletions dialogflow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
},
"dependencies": {
"dialogflow": "^0.8.2",
"pb-util": "^0.1.0",
"pump": "^3.0.0",
"through2": "^3.0.0",
"yargs": "^13.0.0",
"uuid": "^3.3.2"
"uuid": "^3.3.2",
"yargs": "^13.0.0"
},
"devDependencies": {
"chai": "^4.2.0",
Expand Down
102 changes: 0 additions & 102 deletions dialogflow/structjson.js

This file was deleted.

0 comments on commit 94b849b

Please sign in to comment.