Skip to content

Commit

Permalink
reflect test
Browse files Browse the repository at this point in the history
  • Loading branch information
bhelx committed Jul 17, 2024
1 parent 0f58b93 commit 6e600c1
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 126 deletions.
35 changes: 7 additions & 28 deletions mock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,15 @@ import (
"github.com/extism/go-pdk"
)

//go:export logMessage
func logMessage(msgPtr uint64) uint64 {
msgMem := pdk.FindMemory(msgPtr)
msg := string(msgMem.ReadBytes())
//go:export reflectObjectHost
func reflectObjectHost(kPtr uint64) uint64 {
kMem := pdk.FindMemory(kPtr)
k := string(kMem.ReadBytes())

fmt.Println(msg)
fmt.Println(k)

valMem := pdk.AllocateString("true")
return valMem.Offset()
}

//go:export databaseRead
func databaseRead(keyPtr uint64) uint64 {
keyMem := pdk.FindMemory(keyPtr)
key := string(keyMem.ReadBytes())

fmt.Println("databaseRead at key: " + key)

valMem := pdk.AllocateBytes([]byte("some bytes"))
return valMem.Offset()
}

//go:export databaseWrite
func databaseWrite(wPtr uint64) uint64 {
wMem := pdk.FindMemory(wPtr)
writeS := string(wMem.ReadBytes())

fmt.Println("databaseWrite: " + writeS)

return 0
kRet := pdk.AllocateString(k)
return kRet.Offset()
}

func main() {}
29 changes: 22 additions & 7 deletions runner/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { Test } from "@dylibso/xtp-test";

export function test() {
let res = Test.callString("processText", "hello world")
Test.assertEqual("processText('hello world') = [5,5]", res, "[5,5]")
const EmbeddedObject = {
aBoolArray: [true, false, true],
aStringArray: ["Hello", "🌍", "World!"],
anEnumArray: ['option1', 'option2', 'option3'],
anIntArray: [1, 2, 3],
}

res = Test.callString("convertTemperature", "celsius")
let parsed = JSON.parse(res)
Test.assertEqual("convertTemperature.value = 123", parsed.value, 123)
Test.assertEqual("convertTemperature.scale = celsius", parsed.scale, 'celsius')
const KitchenSink = {
aString: "🌍Hello 🌍 World!🌍",
anInt: 42,
aFloat: 3.14,
aDouble: 3.141592653589793238462643383279502884197,
aBool: true,
//anUntypedObject: { hello: 'world' }, TODO uncomment after bug fix
anEnum: 'option1',
anEmbeddedObject: EmbeddedObject,
anEmbeddedObjectArray: [EmbeddedObject, EmbeddedObject]
}

export function test() {
let input = JSON.stringify(KitchenSink)
let output = JSON.parse(Test.callString("reflectObject", input))
// assuming if we re-stringify them here the formatting should be the same
Test.assertEqual("reflectObject preserved the KitchenSink", JSON.stringify(output), JSON.stringify(KitchenSink))
return 0;
}
183 changes: 92 additions & 91 deletions schema.yaml
Original file line number Diff line number Diff line change
@@ -1,109 +1,110 @@
version: v1-draft
exports:
- name: greetUser
- name: reflectObject
description: |
This function greets a user without requiring any input or output.
This function takes a KitchenSinkObject and returns a KitchenSinkObject.
It should come out the same way it came in.
codeSamples:
- lang: typescript
source: |
console.log('Hello User!')
- name: processText
description: |
This function demonstrates how you can handle text input and return
a JSON encoded array of word lengths.
codeSamples:
- lang: typescript
source: |
return input.split(' ').map(c => c.length)
input:
type: string
contentType: text/plain; charset=UTF-8
description: A plain text string input
output:
type: array
items:
type: integer
contentType: application/json
description: An array of word lengths encoded as JSON
- name: convertTemperature
description: |
This function demonstrates the use of complex parameters. It takes a
TemperatureScale enum and returns a TemperatureReading JSON object.
codeSamples:
- lang: typescript
source: |
return { scale: TemperatureScale.celsius, value: 123 }
// pass this through the host function and return it back
return reflectObjectHost(input)
input:
$ref: "#/schemas/TemperatureScale"
$ref: "#/schemas/KitchenSinkObject"
output:
$ref: "#/schemas/TemperatureReading"
$ref: "#/schemas/KitchenSinkObject"
imports:
- name: logMessage
input:
type: string
contentType: text/plain; charset=UTF-8
description: The message to log
output:
type: boolean
contentType: application/json
description: Result of the logging operation as a boolean
description: >
This is a host function that logs a message to the system. Host functions
can perform system-level tasks.
- name: databaseRead
description: Reads data from the database using the provided key
- name: reflectObjectHost
description: |
This function takes a KitchenSinkObject and returns a KitchenSinkObject.
It should come out the same way it came in. It's the same as the export.
But the export should call this.
input:
type: string
contentType: text/plain; charset=UTF-8
description: The key to look up in the database
$ref: "#/schemas/KitchenSinkObject"
output:
type: buffer
description: The raw byte values stored at the key
- name: databaseWrite
description: Writes data to the database
input:
$ref: "#/schemas/DatabaseWriteParams"
$ref: "#/schemas/KitchenSinkObject"
schemas:
- name: DatabaseWriteParams
contentType: application/json
description: Parameters for writing data to the database
- name: EmbeddedObject
description: An embedded object, has some arrays too
required:
- aBoolArray
- anEnumArray
- aStringArray
- anIntArray
properties:
- name: key
type: string
description: The key for the database entry
- name: value
#type: buffer
type: string
description: The value to store at the key
- name: TemperatureScale
description: Different scales of temperature measurement
enum:
- celsius
- fahrenheit
- kelvin
- name: OperationStatus
description: The status of an operation
- name: aBoolArray
description: an array of bools
type: array
items:
type: boolean
- name: aStringArray
description: an array of strings
type: array
items:
type: string
- name: anEnumArray
description: an array of enums
type: array
items:
$ref: "#/schemas/AStringEnum"
- name: anIntArray
description: an array of enums
type: array
items:
type: integer
- name: AStringEnum
description: A string enum
type: string
enum:
- success
- failure
- name: TemperatureReading
- option1
- option2
- option3
- name: KitchenSinkObject
contentType: application/json
description: A JSON object representing a temperature reading
description: A json object with every type of property
required:
- scale
- value
- aString
- anInt
- aFloat
- aDouble
- aBool
- anEnum
- anEmbeddedObject
- anEmbeddedObjectArray
#- anUntypedObject
properties:
- $ref: "#/schemas/TemperatureScale"
name: scale
description: The scale of the temperature reading
- name: value
#type: number
#format: float
type: integer
description: The temperature value
- name: timestamp
- name: anOptionalString
type: string
format: date-time
description: The time when the temperature was recorded


description: A string but not required
- name: aString
type: string
description: A String
- name: anInt
type: integer
description: An Integer
- name: aFloat
type: number
format: float
description: A Float
- name: aDouble
type: number
format: double
description: A Double
- name: aBool
type: boolean
description: A Boolean
# TODO put back after bug fix
# - name: anUntypedObject
# type: object
# description: An untyped object
- name: anEnum
description: A string enum (prop comment)
$ref: "#/schemas/AStringEnum"
- name: anEmbeddedObject
description: A embedded object array(prop comment)
$ref: "#/schemas/EmbeddedObject"
- name: anEmbeddedObjectArray
description: A embedded object array (prop comment)
type: array
items:
$ref: "#/schemas/EmbeddedObject"

0 comments on commit 6e600c1

Please sign in to comment.