fix(app,manifest): manifest serialization
arcad/edge/pipeline/head This commit looks good Details

This commit is contained in:
wpetit 2023-04-11 15:08:07 +02:00
parent 98ebd7a168
commit f5283b86ed
7 changed files with 86 additions and 22 deletions

View File

@ -4,4 +4,9 @@ title: SDK Test
version: 0.0.0
description: |
Suite de tests pour le SDK client
tags: ["test"]
tags: ["test"]
metadata:
paths:
icon: /icon.png
minimumRole: visitor

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

View File

@ -4,6 +4,7 @@
<meta charset="utf-8" />
<title>Client SDK Test suite</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/png" href="/icon.png">
<link rel="stylesheet" href="/vendor/mocha.css" />
<style>
body {

View File

@ -12,12 +12,12 @@ import (
type ID string
type Manifest struct {
ID ID `yaml:"id" json:"id"`
Version string `yaml:"version" json:"version"`
Title string `yaml:"title" json:"title"`
Description string `yaml:"description" json:"description"`
Tags []string `yaml:"tags" json:"tags"`
Metadata map[string]any `yaml:"metadata" json:"metadata"`
ID ID `yaml:"id" json:"id"`
Version string `yaml:"version" json:"version"`
Title string `yaml:"title" json:"title"`
Description string `yaml:"description" json:"description"`
Tags []string `yaml:"tags" json:"tags"`
Metadata MapStr `yaml:"metadata" json:"metadata"`
}
type MetadataValidator func(map[string]any) (bool, error)

61
pkg/app/map_str.go Normal file
View File

@ -0,0 +1,61 @@
package app
import (
"fmt"
"github.com/pkg/errors"
)
type MapStr map[string]interface{}
func MapStrUnion(dict1 MapStr, dict2 MapStr) MapStr {
dict := MapStr{}
for k, v := range dict1 {
dict[k] = v
}
for k, v := range dict2 {
dict[k] = v
}
return dict
}
func (ms *MapStr) UnmarshalYAML(unmarshal func(interface{}) error) error {
var result map[interface{}]interface{}
err := unmarshal(&result)
if err != nil {
return errors.WithStack(err)
}
*ms = cleanUpInterfaceMap(result)
return nil
}
func cleanUpInterfaceArray(in []interface{}) []interface{} {
result := make([]interface{}, len(in))
for i, v := range in {
result[i] = cleanUpMapValue(v)
}
return result
}
func cleanUpInterfaceMap(in map[interface{}]interface{}) MapStr {
result := make(MapStr)
for k, v := range in {
result[fmt.Sprintf("%v", k)] = cleanUpMapValue(v)
}
return result
}
func cleanUpMapValue(v interface{}) interface{} {
switch v := v.(type) {
case []interface{}:
return cleanUpInterfaceArray(v)
case map[interface{}]interface{}:
return cleanUpInterfaceMap(v)
case string:
return v
default:
return fmt.Sprintf("%v", v)
}
}

View File

@ -26,28 +26,23 @@ func WithNamedPathsValidator(names ...NamedPath) app.MetadataValidator {
return true, nil
}
paths, ok := rawPaths.(map[any]any)
paths, ok := rawPaths.(app.MapStr)
if !ok {
return false, errors.Errorf("metadata['paths']: unexpected named path value type '%T'", rawPaths)
}
for n, p := range paths {
name, ok := n.(string)
if !ok {
return false, errors.Errorf("metadata['paths']: unexpected named path type '%T'", n)
}
if _, exists := set[NamedPath(name)]; !exists {
return false, errors.Errorf("metadata['paths']: unexpected named path '%s'", name)
if _, exists := set[NamedPath(n)]; !exists {
return false, errors.Errorf("metadata['paths']: unexpected named path '%s'", n)
}
path, ok := p.(string)
if !ok {
return false, errors.Errorf("metadata['paths']['%s']: unexpected named path value type '%T'", name, path)
return false, errors.Errorf("metadata['paths']['%s']: unexpected named path value type '%T'", n, path)
}
if !strings.HasPrefix(path, "/") {
return false, errors.Errorf("metadata['paths']['%s']: named path value should start with a '/'", name)
return false, errors.Errorf("metadata['paths']['%s']: named path value should start with a '/'", n)
}
}

View File

@ -14,11 +14,12 @@ type Module struct {
}
type gojaManifest struct {
ID string `goja:"id" json:"id"`
Version string `goja:"version" json:"version"`
Title string `goja:"title" json:"title"`
Description string `goja:"description" json:"description"`
Tags []string `goja:"tags" json:"tags"`
ID string `goja:"id" json:"id"`
Version string `goja:"version" json:"version"`
Title string `goja:"title" json:"title"`
Description string `goja:"description" json:"description"`
Tags []string `goja:"tags" json:"tags"`
Metadata map[string]any `goja:"metadata" json:"metadata"`
}
func toGojaManifest(manifest *app.Manifest) *gojaManifest {
@ -28,6 +29,7 @@ func toGojaManifest(manifest *app.Manifest) *gojaManifest {
Title: manifest.Title,
Description: manifest.Description,
Tags: manifest.Tags,
Metadata: manifest.Metadata,
}
}