Skip to content

Commit

Permalink
Support arbitrary data unter the key custom in meta.yaml or meta.js…
Browse files Browse the repository at this point in the history
…on is accessible via the api

References #62, #71, #73

This will enable creators of custom frontends to use custom data fields / values
/ configuration defined in metadata. Data needs to be evaluated and validated in
the frontend.

- Expose custom data through api
- Add custom data synonyms example
- Demostrate custom data and synonyms in yaml

With the help of dyno:
https://stackoverflow.com/questions/40737122/convert-yaml-to-json-without-struct/40737676#40737676
  • Loading branch information
rockitbaby authored and mariuswilms committed Jun 27, 2019
1 parent caa3f46 commit ab98c83
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
2 changes: 2 additions & 0 deletions apiv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type APIv1Node struct {
Modified int64 `json:"modified"`
Version string `json:"version"`
Tags []string `json:"tags"`
Custom interface{} `json:"custom"`
Docs []*APIv1NodeDoc `json:"docs"`
Assets []*APIv1NodeAsset `json:"assets"`
Crumbs []*APIv1RefNode `json:"crumbs"`
Expand Down Expand Up @@ -263,6 +264,7 @@ func (api APIv1) NewNode(n *Node) (*APIv1Node, error) {
Related: related,
Prev: prev,
Next: next,
Custom: n.Custom(),

// Deprecated, to be removed in APIv3:
Downloads: assets,
Expand Down
5 changes: 5 additions & 0 deletions example/02_Components/Displaying Content/List/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ tags:
- Progress/Draft
- Release/0.6

custom:
synonyms:
- Listing
- Index

version: 1
5 changes: 5 additions & 0 deletions example/02_Components/Input/Textfield/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ tags:
- Build 8

version: 1

custom:
synonyms:
- Inputfield
- Entryfield
7 changes: 7 additions & 0 deletions frontend/src/Page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ function Page(props) {
authors = <Meta title={title}>{authorLinks}</Meta>;
}

let synonyms;
if (props.custom && props.custom.synonyms) {
let synonymText = Array.isArray(props.custom.synonyms) ? props.custom.synonyms.join(", ") : props.custom.synonyms
synonyms = <Meta title="Synonyms">{synonymText}</Meta>;
}

return (
<div className="page">
<div className="page__header">
Expand All @@ -150,6 +156,7 @@ function Page(props) {
<div className="page__meta">
<Meta title="Last Changed">{new Date(props.modified * 1000).toLocaleDateString()}</Meta>
{authors}
{synonyms}
</div>
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ func (n *Node) Description() string {
return n.meta.Description
}

func (n *Node) Custom() interface{} {
return n.meta.Custom
}

// Returns a list of related nodes.
func (n *Node) Related(get NodeGetter) []*Node {
nodes := make([]*Node, 0, len(n.meta.Related))
Expand Down
16 changes: 11 additions & 5 deletions node_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ import (
"path/filepath"

"github.com/go-yaml/yaml"
"github.com/icza/dyno"
)

// Metadata parsed from node configuration.
type NodeMeta struct {
path string

// Email addresses of node authors.
Authors []string `json:"authors,omitempty" yaml:"authors,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Related []string `json:"related,omitempty" yaml:"related,omitempty"`
Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"`
Authors []string `json:"authors,omitempty" yaml:"authors,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Related []string `json:"related,omitempty" yaml:"related,omitempty"`
Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"`
Custom interface{} `json:"custom,omitempty" yaml:"custom,omitempty"`

// Freeform version string.
Version string `json:"version,omitempty" yaml:"version,omitempty"`
Expand Down Expand Up @@ -59,7 +61,11 @@ func (m *NodeMeta) Load() error {
case ".json":
return json.Unmarshal(contents, &m)
case ".yaml", ".yml":
return yaml.Unmarshal(contents, &m)
if err := yaml.Unmarshal(contents, &m); err != nil {
return err
}
m.Custom = dyno.ConvertMapI2MapS(m.Custom)
return nil
default:
return fmt.Errorf("Unsupported format: %s", prettyPath(m.path))
}
Expand Down

0 comments on commit ab98c83

Please sign in to comment.