Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit d1a0847

Browse files
Chris McConnellninggao
authored andcommitted
Enable template: as a $ref protocol. (#424)
* Enable template: as a $ref protocol. Add utterance to mappings. Simplify stringAsk. * Enable template: in requires.
1 parent 874ad69 commit d1a0847

File tree

5 files changed

+44
-31
lines changed

5 files changed

+44
-31
lines changed

packages/dialog/src/library/processSchemas.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let parser: any = require('json-schema-ref-parser')
1313

1414
type idToSchema = { [id: string]: any }
1515

16+
// All .schema files found in template directories
1617
async function templateSchemas(templateDirs: string[], feedback: fg.Feedback): Promise<idToSchema> {
1718
let map: idToSchema = {}
1819
for (let dir of templateDirs) {
@@ -27,10 +28,11 @@ async function templateSchemas(templateDirs: string[], feedback: fg.Feedback): P
2728
return map
2829
}
2930

30-
async function findRequires(schema: any, map: idToSchema, found: idToSchema, feedback: fg.Feedback): Promise<void> {
31+
// Find recursive requires
32+
async function findRequires(schema: any, map: idToSchema, found: idToSchema, resolver: any, feedback: fg.Feedback): Promise<void> {
3133
let addRequired = async (required: string) => {
3234
if (!found[required]) {
33-
let schema = map[required] || await getSchema(required, feedback)
35+
let schema = map[required] || await getSchema(required, feedback, resolver)
3436
if (!schema) {
3537
feedback(fg.FeedbackType.error, `Schema ${required} cannot be found`)
3638
} else {
@@ -45,7 +47,7 @@ async function findRequires(schema: any, map: idToSchema, found: idToSchema, fee
4547
await addRequired(required)
4648
}
4749
} else {
48-
await findRequires(val, map, found, feedback)
50+
await findRequires(val, map, found, resolver, feedback)
4951
}
5052
}
5153
}
@@ -55,14 +57,15 @@ async function findRequires(schema: any, map: idToSchema, found: idToSchema, fee
5557
async function getSchema(path: string, feedback: fg.Feedback, resolver?: any): Promise<any> {
5658
let schema
5759
try {
58-
let noref = await parser.dereference(path, { template: resolver })
60+
let noref = await parser.dereference(path, { resolve: { template: resolver } })
5961
schema = allof(noref)
6062
} catch (err) {
6163
feedback(fg.FeedbackType.error, err)
6264
}
6365
return schema
6466
}
6567

68+
// Merge together multiple schemas
6669
function mergeSchemas(allSchema: any, schemas: any[]) {
6770
for (let schema of schemas) {
6871
allSchema.properties = { ...allSchema.properties, ...schema.properties }
@@ -75,20 +78,23 @@ function mergeSchemas(allSchema: any, schemas: any[]) {
7578
}
7679

7780
// Process the root schema to generate all schemas
78-
// 1) A property can $ref to a property definition to reuse a type like address. Ref resolver includes.
79-
// 2) $requires:[] can be in a property or at the top. This is handled by finding all of the referenced schemas and then merging.
81+
// 1) A property can $ref to a property definition to reuse a type like address.
82+
// Ref resolver includes template: for referring to template files.
83+
// 2) $requires:[] can be in a property or at the top.
84+
// This is handled by finding all of the referenced schemas and then merging.
8085
export async function processSchemas(schemaPath: string, templateDirs: string[], feedback: fg.Feedback)
8186
: Promise<any> {
8287
let allRequired = await templateSchemas(templateDirs, feedback)
8388
let resolver: any = {
84-
canRead: true,
85-
read(file: string): any {
86-
return allRequired[ppath.basename(file)]
89+
canRead: /template:/,
90+
read(file: any): any {
91+
let base = file.url.substring(file.url.indexOf(':') + 1)
92+
return allRequired[base]
8793
}
8894
}
8995
let formSchema = await getSchema(schemaPath, feedback, resolver)
9096
let required = {}
91-
await findRequires(formSchema, allRequired, required, feedback)
97+
await findRequires(formSchema, allRequired, required, resolver, feedback)
9298
let allSchema = clone(formSchema)
9399
if (!allSchema.required) allSchema.required = []
94100
if (!allSchema.$expectedOnly) allSchema.$expectedOnly = []
@@ -99,7 +105,7 @@ export async function processSchemas(schemaPath: string, templateDirs: string[],
99105
// Default to properties in root schema
100106
allSchema.$public = Object.keys(formSchema.properties)
101107
}
102-
mergeSchemas(allSchema, Object.values(allRequired));
108+
mergeSchemas(allSchema, Object.values(required));
103109

104110
return new s.Schema(schemaPath, allSchema)
105111
}

packages/dialog/src/library/schema.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,11 @@ export class Schema {
141141
mappings(): string[] {
142142
let mappings: string[] = this.schema.$mappings
143143
if (!mappings && this.path) {
144-
if (this.schema.type === 'number') {
144+
let type = this.typeName()
145+
if (type === 'number') {
145146
mappings = [`number:${this.path}`, 'number']
147+
} else if (type === 'string') {
148+
mappings = [this.path + 'Entity', 'utterance']
146149
} else {
147150
mappings = [this.path + 'Entity']
148151
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"currency": {
4+
"type": "object",
5+
"properties": {
6+
"quantity": {
7+
"type": "number"
8+
},
9+
"currency": {
10+
"type": "string"
11+
}
12+
},
13+
"$templates": []
14+
}
15+
}

packages/dialog/templates/stringAsk.dialog.lg

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,10 @@
99
"condition":"!$@{property} && contains(dialog.requiredProperties, '@{property}')",
1010
"actions": [
1111
{
12-
"$kind": "Microsoft.IfCondition",
13-
"condition": "contains(turn.schema.properties['@{property}'].$mappings, 'utterance')",
14-
"actions":[
15-
{
16-
"$kind": "Microsoft.Ask",
17-
"activity": "@{callAsk()}",
18-
"expectedProperties": [
19-
"utterance","@{property}"
20-
]
21-
}
22-
],
23-
"elseActions":[
24-
{
25-
"$kind": "Microsoft.Ask",
26-
"activity": "@{callAsk()}",
27-
"expectedProperties": [
28-
"@{property}"
29-
]
30-
}
12+
"$kind": "Microsoft.Ask",
13+
"activity": "@{callAsk()}",
14+
"expectedProperties": [
15+
"@{property}"
3116
]
3217
},
3318
]

packages/dialog/test/commands/dialog/forms/sandwich.schema

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@
5757
},
5858
"Name": {
5959
"type": "string"
60+
},
61+
"Price": {
62+
"$ref": "template:currency.schema#/currency"
6063
}
6164
},
6265
"required": [
66+
"Name",
6367
"Meat",
6468
"Bread",
6569
"Cheese"

0 commit comments

Comments
 (0)