From e837ff5dde793229068e74b0fd67997617e482c6 Mon Sep 17 00:00:00 2001 From: Dave Gramlich Date: Thu, 28 Mar 2019 15:19:02 -0700 Subject: [PATCH] refactor(samples): use pb-util to encode/decode structs (#311) --- dialogflow/detect.js | 14 ++--- dialogflow/detect.v2beta1.js | 8 ++- dialogflow/package.json | 5 +- dialogflow/structjson.js | 102 ----------------------------------- 4 files changed, 11 insertions(+), 118 deletions(-) delete mode 100644 dialogflow/structjson.js diff --git a/dialogflow/detect.js b/dialogflow/detect.js index dd4ec5a550..f7f3531012 100644 --- a/dialogflow/detect.js +++ b/dialogflow/detect.js @@ -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'); @@ -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, @@ -114,7 +112,7 @@ async function detectEventIntent( queryInput: { event: { name: eventName, - parameters: structjson.jsonToStructProto({foo: 'bar'}), + parameters: struct.encode({foo: 'bar'}), languageCode: languageCode, }, }, @@ -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}`); diff --git a/dialogflow/detect.v2beta1.js b/dialogflow/detect.v2beta1.js index a96f60441c..83b0563b4a 100644 --- a/dialogflow/detect.v2beta1.js +++ b/dialogflow/detect.v2beta1.js @@ -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] @@ -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}`); diff --git a/dialogflow/package.json b/dialogflow/package.json index 5dad24fba1..7d24070259 100644 --- a/dialogflow/package.json +++ b/dialogflow/package.json @@ -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", diff --git a/dialogflow/structjson.js b/dialogflow/structjson.js deleted file mode 100644 index 739735db57..0000000000 --- a/dialogflow/structjson.js +++ /dev/null @@ -1,102 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @fileoverview Utilities for converting between JSON and goog.protobuf.Struct - * proto. - */ - -'use strict'; - -function jsonToStructProto(json) { - const fields = {}; - for (const k in json) { - fields[k] = jsonValueToProto(json[k]); - } - - return {fields}; -} - -const JSON_SIMPLE_TYPE_TO_PROTO_KIND_MAP = { - [typeof 0]: 'numberValue', - [typeof '']: 'stringValue', - [typeof false]: 'boolValue', -}; - -const JSON_SIMPLE_VALUE_KINDS = new Set([ - 'numberValue', - 'stringValue', - 'boolValue', -]); - -function jsonValueToProto(value) { - const valueProto = {}; - - if (value === null) { - valueProto.kind = 'nullValue'; - valueProto.nullValue = 'NULL_VALUE'; - } else if (value instanceof Array) { - valueProto.kind = 'listValue'; - valueProto.listValue = {values: value.map(jsonValueToProto)}; - } else if (typeof value === 'object') { - valueProto.kind = 'structValue'; - valueProto.structValue = jsonToStructProto(value); - } else if (typeof value in JSON_SIMPLE_TYPE_TO_PROTO_KIND_MAP) { - const kind = JSON_SIMPLE_TYPE_TO_PROTO_KIND_MAP[typeof value]; - valueProto.kind = kind; - valueProto[kind] = value; - } else { - console.warn('Unsupported value type ', typeof value); - } - return valueProto; -} - -function structProtoToJson(proto) { - if (!proto || !proto.fields) { - return {}; - } - const json = {}; - for (const k in proto.fields) { - json[k] = valueProtoToJson(proto.fields[k]); - } - return json; -} - -function valueProtoToJson(proto) { - if (!proto || !proto.kind) { - return null; - } - - if (JSON_SIMPLE_VALUE_KINDS.has(proto.kind)) { - return proto[proto.kind]; - } else if (proto.kind === 'nullValue') { - return null; - } else if (proto.kind === 'listValue') { - if (!proto.listValue || !proto.listValue.values) { - console.warn('Invalid JSON list value proto: ', JSON.stringify(proto)); - } - return proto.listValue.values.map(valueProtoToJson); - } else if (proto.kind === 'structValue') { - return structProtoToJson(proto.structValue); - } else { - console.warn('Unsupported JSON value proto kind: ', proto.kind); - return null; - } -} - -module.exports = { - jsonToStructProto, - structProtoToJson, -};