WebAssembly barcode country case study



Function as a Service -FaaS- orchestration

Barcode.bpmn 

Get_barcode_country.dmn 

Exercise in AssemblyScript

Create a Barcode_country function based on the following templates

Compilation

asc assembly/Barcode_country.ts --outFile ./build/Barcode_country.wasm --textFile ./build/Barcode_country.wat --target release --use abort=wasi_abort

Execution

wasmtime --invoke Barcode_country ./build/Barcode_country.wasm 4719512002889

Barcode_country.ts

import * as WASI from "as-wasi/assembly"; // Required to compile with '--use abort=wasi_abort' option...

enum ISO_3166_international_code {
    Austria = 36,
    France = 250,
    Monaco = 492,
    Portugal = 620,
    Japan = 392,
    Russia = 643,
    Taiwan = 158,
    Incorrect = -1,
    Invalid_format = -2
}

const Countries: Map<ISO_3166_international_code, Array<i32>> = new Map;
Countries.set(ISO_3166_international_code.Austria, [900, 919]);
Countries.set(ISO_3166_international_code.France, [300, 379]);
Countries.set(ISO_3166_international_code.Monaco, [300, 379]);
Countries.set(ISO_3166_international_code.Portugal, [560, 560]);
Countries.set(ISO_3166_international_code.Japan, [450, 459]);
Countries.set(ISO_3166_international_code.Russia, [460, 469]);
Countries.set(ISO_3166_international_code.Taiwan, [471, 471]);

// 'Barcode_country' function here...

Barcode_country.ts cont'd

export function Barcode_country(barcode: i64): ISO_3166_international_code {
    const thirteen: i32 = 13;
    if (barcode.toString().length === thirteen) {
        const country_code: i32 = parseInt(barcode.toString().substr(0, 3)) as i32;
        if (isNaN(country_code)) return ISO_3166_international_code.Invalid_format;
        const result: Array<ISO_3166_international_code> = new Array;
        for (let i = 0; i < Countries.keys().length; i++) { // No lambda in AssemblyScript!
            const interval = Countries.values()[i];
            if (country_code >= interval[0] && country_code <= interval[1])
                result.push(Countries.keys()[i]);
        }
        // 'result[0]' raises problem between, for instance, 'France' and 'Monaco':
        return result.length === 0 ? ISO_3166_international_code.Incorrect : result[0];
    }
    return ISO_3166_international_code.Invalid_format;
}
Exercise in Go

Create a Barcode_country function based on the following templates

Compilation

tinygo build -o ./TinyGo/Barcode_country.wasm -target=wasi ./TinyGo/Barcode_country.go

Execution

wasmtime --invoke Barcode_country ./TinyGo/Barcode_country.wasm 4719512002889

Barcode_country.go

package main

import (
	"fmt"
	"strconv"
)

func main() {}

type ISO_3166_international_code int

const (
	Austria        ISO_3166_international_code = 36
	France                                     = 250
	Monaco                                     = 492
	Portugal                                   = 620
	Japan                                      = 392
	Russia                                     = 643
	Taiwan                                     = 158
	Incorrect                                  = -1
	Invalid_format                             = -2
)

// Seulement au sein des fonctions : 'Countries := map[ISO_3166_international_code]array[int] { Etc.'
var Countries = map[ISO_3166_international_code][2]int{
	Austria:  {900, 919},
	France:   {300, 379},
	Monaco:   {300, 379},
	Portugal: {560, 560},
	Japan:    {450, 459},
	Russia:   {460, 469},
	Taiwan:   {471, 471}, // Dernière virgule obligatoire !
}

// 'Barcode_country' function here...

Barcode_country.go cont'd

//export Barcode_country
func Barcode_country(bar_code int64) ISO_3166_international_code {
	thirteen := 13
	if len(strconv.FormatInt(bar_code, 10)) == thirteen {
		country_code, error := strconv.Atoi(strconv.FormatInt(bar_code, 10)[0:3])
		if error != nil {
			return Invalid_format
		}
		result := []ISO_3166_international_code{}
		for key, value := range Countries {
			if country_code >= value[0] && country_code <= value[1] {
				result = append(result, key)
			}
		}
		if len(result) == 0 {
			return Incorrect
		}
		fmt.Println("Barcode country: ", result[0]) // WASI support generated by '-target=wasi' at build time...
		// 'result[0]' raises problem between, for instance, 'France' and 'Monaco':
		return result[0]
	}
	return Invalid_format
}