Compare commits

..

3 Commits

Author SHA1 Message Date
Mark Dumay
6761bedd51 refactor: support recursive argument init
work in progress
2025-09-02 07:18:23 +02:00
Mark Dumay
f002cf06e4 Merge pull request #1560 from gethinode/hugo-mod-dependencies
Update Hugo module dependencies
2025-09-02 07:04:27 +02:00
markdumay
9d742f9165 fix: update Hugo module dependencies 2025-09-02 03:22:58 +00:00
4 changed files with 388 additions and 2 deletions

4
go.mod
View File

@@ -3,7 +3,7 @@ module github.com/gethinode/hinode
go 1.19
require (
github.com/airbnb/lottie-web v5.12.2+incompatible // indirect
github.com/airbnb/lottie-web v5.13.0+incompatible // indirect
github.com/cloudcannon/bookshop/hugo/v3 v3.16.4 // indirect
github.com/gethinode/mod-bootstrap v1.3.4 // indirect
github.com/gethinode/mod-csp v1.0.8 // indirect
@@ -16,6 +16,6 @@ require (
github.com/gethinode/mod-mermaid/v3 v3.0.1 // indirect
github.com/gethinode/mod-simple-datatables/v2 v2.0.2 // indirect
github.com/gethinode/mod-utils/v4 v4.14.0 // indirect
github.com/nextapps-de/flexsearch v0.0.0-20250606060143-c28f52c09b7a // indirect
github.com/nextapps-de/flexsearch v0.0.0-20250901122457-99cddafcdc64 // indirect
github.com/twbs/bootstrap v5.3.8+incompatible // indirect
)

4
go.sum
View File

@@ -1,5 +1,7 @@
github.com/airbnb/lottie-web v5.12.2+incompatible h1:Ldogtlhiucf7mMsgisyxSBY0qunV44+lpa9Icy2KoQc=
github.com/airbnb/lottie-web v5.12.2+incompatible/go.mod h1:nTss557UK9FGnp8QYlCMO29tjUHwbdAHG/DprbGfHGE=
github.com/airbnb/lottie-web v5.13.0+incompatible h1:plBV5Uq/F1kK0EC61Hr0cBGReI9OgUfd/pp0baoDX8o=
github.com/airbnb/lottie-web v5.13.0+incompatible/go.mod h1:nTss557UK9FGnp8QYlCMO29tjUHwbdAHG/DprbGfHGE=
github.com/cloudcannon/bookshop/hugo/v3 v3.14.0 h1:QNLtpTINvXkxAG/RQVpdXzxtFjG6YAmnBr7qbOD5GF8=
github.com/cloudcannon/bookshop/hugo/v3 v3.14.0/go.mod h1:s7mIonDhtsLcn10ZKuVXyqd6BDHI8vT1WQhZw8rPfY8=
github.com/cloudcannon/bookshop/hugo/v3 v3.16.0 h1:Fb76ABHqTyPl9Z2QqYJCwiMBKPyShOe1EnZxXzW3RVo=
@@ -526,6 +528,8 @@ github.com/nextapps-de/flexsearch v0.0.0-20250523180618-1f75f5f9d9b6 h1:vUlZHDX+
github.com/nextapps-de/flexsearch v0.0.0-20250523180618-1f75f5f9d9b6/go.mod h1:5GdMfPAXzbA2gXBqTjC6l27kioSYzHlqDMh0+wyx7sU=
github.com/nextapps-de/flexsearch v0.0.0-20250606060143-c28f52c09b7a h1:CF/f62ufhBzNvAgoC6JhuqQxtqz0yDj3IIXYKl+A650=
github.com/nextapps-de/flexsearch v0.0.0-20250606060143-c28f52c09b7a/go.mod h1:5GdMfPAXzbA2gXBqTjC6l27kioSYzHlqDMh0+wyx7sU=
github.com/nextapps-de/flexsearch v0.0.0-20250901122457-99cddafcdc64 h1:8gn/7ZfERwknYk63DskhEfkwwpoXubGrzLv5LuSq6hc=
github.com/nextapps-de/flexsearch v0.0.0-20250901122457-99cddafcdc64/go.mod h1:5GdMfPAXzbA2gXBqTjC6l27kioSYzHlqDMh0+wyx7sU=
github.com/twbs/bootstrap v5.3.2+incompatible h1:tuiO5acc6xnZUR77Sbi5aKWXxjYxbmsSbJwYrhAKoQQ=
github.com/twbs/bootstrap v5.3.2+incompatible/go.mod h1:fZTSrkpSf0/HkL0IIJzvVspTt1r9zuf7XlZau8kpcY0=
github.com/twbs/bootstrap v5.3.3+incompatible h1:goFoqinzdHfkeegpFP7pvhbd0g+A3O2hbU3XCjuNrEQ=

View File

@@ -0,0 +1,238 @@
<!--
Copyright © 2024 - 2025 The Hinode Team / Mark Dumay. All rights reserved.
Use of this source code is governed by The MIT License (MIT) that can be found in the LICENSE file.
Visit gethinode.com/license for more details.
-->
{{ define "_partials/inline/default.html" }}
{{ $config := split .config "." }}
{{ $default := .default }}
{{ return or (index site.Params $config) $default }}
{{ end }}
{{ define "_partials/inline/alias-type.html" }}
{{ $type := .type }}
{{ $custom := .types }}
{{ $references := .references }}
{{ $aliases := dict "path" "string" "select" "string" "url" "string" "dict" "[]map[string]interface {}" "slice" "[]interface {}" }}
{{ $reserved := slice "bool" "int" "int64" "float" "float64" "string" "dict" "slice" }}
{{ $input := slice }}
{{ if not $type }}
{{ errorf "expected type argument: %s" page.File }}
{{ else }}
{{ $input = slice | append $type }}
{{ end }}
{{ $extra := slice }}
{{ range $element := $input }}
{{ $alias := index $aliases $element }}
{{ if $alias }}{{ $extra = $extra | append slice $alias }}{{ end }}
{{ if not (in $reserved $element) }}
{{ $def := index $references $element }}
{{ if $def }}
{{ $extra = $extra | append slice $def._reflect }}
{{ end }}
{{ end }}
{{ if $extra }}{{ $input = $input | append $extra }}{{ end }}
{{ end }}
{{ return $input }}
{{ end }}
{{/* Initialize arguments */}}
{{ $structure := .structure }}
{{ $bookshop := .bookshop }}
{{ $child := .child }}
{{ $named := .named | default true }}
{{ $args := .args | default dict }}
{{ $group := .group }}
{{/* Initialize local variables */}}
{{ $error := false }}
{{ $errmsg := slice }}
{{ $warnmsg := slice }}
{{ $params := dict }}
{{ $types := dict }}
{{ $default := slice }}
{{/* Validate partial arguments */}}
{{ if and (not $structure) (not $bookshop) }}
{{- $errmsg = $errmsg | append (printf "partial [utilities/InitArgs.html] - Missing value for param 'structure' or 'bookshop'") -}}
{{ $error = true }}
{{ end }}
{{/* Initialize type structure */}}
{{ if hasPrefix $structure "bookshop-" }}{{ $bookshop = strings.TrimPrefix "bookshop-" $structure }}{{ $structure = "" }}{{ end }}
{{ if not $error }}
{{ $types = partial "utilities/InitTypes.html" (dict "structure" $structure "bookshop" $bookshop "child" $child ) }}
{{ if $types.errmsg }}{{ $errmsg = $errmsg | append $types.errmsg }}{{ $error = $types.err }}{{ end }}
{{ if $types.warnmsg }}{{ $warnmsg = $warnmsg | append $types.warnmsg }}{{ end }}
{{ end }}
{{ $namedargs := dict }}
{{ if not $named }}
{{ range $index, $val := $args }}
{{ $found := false }}
{{ range $k, $v := $types.types }}
{{ if eq $index $v.position }}
{{ $namedargs = merge $namedargs (dict $k $val) }}
{{ $found = true }}
{{ break }}
{{ end }}
{{ end }}
{{ if not $found }}
{{ $errmsg = $errmsg | append (printf "[%s] unsupported argument at index %d (value: '%s')" (or $structure $bookshop) $index $val) }}
{{ end }}
{{ end }}
{{ else }}
{{ $namedargs = $args }}
{{ end }}
{{/* Validate passed arguments and initialize their default value when applicable */}}
{{ if not $error }}
{{ range $key, $val := $namedargs }}
{{ $def := index $types.types $key }}
{{ if not $def }}
{{ if eq (printf "%T" $key) "string" }}
{{ $errmsg = $errmsg | append (printf "[%s] unsupported argument '%s'" (or $structure $bookshop) $key) }}
{{ else if eq (printf "%T" $key) "int" }}
{{ $errmsg = $errmsg | append (printf "[%s] unsupported argument at index %d (value: '%s')" (or $structure $bookshop) $key $val) }}
{{ else }}
{{ $errmsg = $errmsg | append (printf "[%s] unsupported argument value '%v'" (or $structure $bookshop) $val) }}
{{ end }}
{{ $error = true }}
{{ break }}
{{ else }}
{{/* initialize default value */}}
{{ if and (eq $val nil) (or $def.config $def.default) }}
{{ $val = (partial "inline/default.html" (dict "config" $def.config "default" $def.default)) }}
{{ $default = $default | append $key }}
{{ end }}
{{/* validate type */}}
{{ $expected := partial "inline/alias-type.html" (dict "type" $def.type "types" $types.types "references" $types.udt) }}
{{ $actual := printf "%T" $val }}
{{/* cast supported types from/to string */}}
{{ if and (in $expected "bool") (in (slice "true" "false") $val) }}
{{ $actual = "bool" }}
{{ $val = cond (eq $val "true") true false }}
{{ else if and (in $expected "int") (findRE `^-?\d+$` $val) }}
{{ $actual = "int" }}
{{ $val = int $val }}
{{ else if and (in $expected "float") (findRE `^(?:[1-9]\d*|0)?(?:\.\d+)?$` $val) }}
{{ $actual = "float" }}
{{ $val = float $val }}
{{ else if and (in $expected "string") (in (slice "bool" "int" "int64" "float" "float64") $actual) }}
{{ $actual = "string" }}
{{ $val = string $val }}
{{ end }}
{{ if and $val (not (in $expected $actual)) }}
{{ $errmsg = $errmsg | append (printf "[%s] argument '%s': expected type '%s', got '%s' with value '%v'" (or $structure $bookshop) (string $key) (delimit $expected ", ") $actual $val) }}
{{ $error = true }}
{{ break }}
{{ end }}
{{/* validate permitted values */}}
{{ if and (reflect.IsMap $def.options) $def.options.values (eq $actual "string") }}
{{ if and $val (not (in $def.options.values $val)) }}
{{ $errmsg = $errmsg | append (printf "[%s] argument '%s': unexpected value '%s'" (or $structure $bookshop) (string $key) $val) }}
{{ $error = true }}
{{ break }}
{{ end }}
{{ else if and (reflect.IsMap $def.options) (or $def.options.min $def.options.max) (in (slice "int" "float" "float64") $actual) }}
{{ if or
(and $def.options.min (lt $val $def.options.min))
(and $def.options.max (gt $val $def.options.max))
}}
{{ $min := (string (or $def.options.min "-")) }}
{{ $max := (string (or $def.options.max "-")) }}
{{ $errmsg = $errmsg | append (printf "[%s] argument '%s': value '%s' out of range [%s, %s]" (or $structure $bookshop) (string $key) (string $val) $min $max) }}
{{ $error = true }}
{{ break }}
{{ end }}
{{ end }}
{{/* validate if argument is deprecated */}}
{{ with $def.deprecated }}
{{ $warn := printf "[%s] argument '%s': deprecated in v%s" (or $structure $bookshop) $key (strings.TrimPrefix "v" .) }}
{{ with $def.alternative }}
{{ $warn = printf "%s, use '%s' instead" $warn . }}
{{ end }}
{{ $warnmsg = $warnmsg | append $warn }}
{{ end }}
{{ end }}
{{/* append the argument to the return set */}}
{{ if not $error }}
{{ $params = merge $params (dict $key $val) }}
{{ end }}
{{ end }}
{{ end }}
{{ if not $error }}
{{/* validate required arguments */}}
{{ $max := len $namedargs }}
{{ $expected := 0 }}
{{ range $key, $val := $types.types }}
{{ $skip := false }}
{{ $groups := slice | append $val.group }}
{{ if and $group $val.group }}
{{ $skip = not (in $groups $group )}}
{{ end }}
{{ if and (not $skip) (not $val.optional) }}
{{ if not (isset $namedargs $key) }}
{{ $errmsg = $errmsg | append (printf "[%s] argument '%s': expected value" (or $structure $bookshop) $key) }}
{{ $error = true }}
{{ end }}
{{ end }}
{{ end }}
{{ if lt $max $expected }}
{{ $errmsg = $errmsg | append (printf "[%s] expected '%d' args, got '%d'" (or $structure $bookshop) $expected $max) }}
{{ $error = true }}
{{ end }}
{{/* add missing keys with default values (nested one level deep) or empty slice */}}
{{ range $key, $val := $types.types }}
{{ if (not (isset $params $key)) }}
{{ $udt := index $types.udt $key }}
{{ if or (isset $val "config") (isset $val "default") }}
{{ $params = merge $params (dict
$key (partial "inline/default.html" (dict "config" $val.config "default" $val.default))
) }}
{{ $default = $default | append $key }}
{{ else if $udt }}
{{ if eq $udt._reflect "[]interface {}" }}
{{ $params = merge $params (dict $key slice) }}
{{ else }}
{{ $nested := dict }}
{{ range $k, $v := $udt }}
{{ if and (reflect.IsMap $v) (or $v.config $v.default) }}
{{ $nested = merge $nested (dict
$k (partial "inline/default.html" (dict "config" $val.config "default" $val.default))
)}}
{{ end }}
{{ end }}
{{ $params = merge $params (dict $key $nested) }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{/* add the key-value pair using camel case to support chaining of the identifier */}}
{{/* see https://gohugo.io/configuration/params/#article */}}
{{ range $key, $val := $params }}
{{ if strings.Contains $key "-" }}
{{ $camelKey := partial "utilities/camelize.html" $key }}
{{ $params = merge $params (dict $camelKey $val) }}
{{ end }}
{{ end }}
{{ end }}
{{ $params = merge $params (dict "err" $error "errmsg" $errmsg "warnmsg" $warnmsg "default" $default) }}
{{ return $params }}

View File

@@ -0,0 +1,144 @@
<!--
Copyright © 2025 The Hinode Team / Mark Dumay. All rights reserved.
Use of this source code is governed by The MIT License (MIT) that can be found in the LICENSE file.
Visit gethinode.com/license for more details.
-->
{{/* Inline partial to retrieve the type definition of the provided key (without recursion) */}}
{{ define "_partials/inline/type-definition.html" }}
{{ $key := .key }}
{{ $val := .val }}
{{ $arguments := .arguments }}
{{ $types := .types }}
{{ $def := index $arguments $key }}
{{ $udt := "" }}
{{ $reflect := "" }}
{{ $reserved := slice "bool" "int" "int64" "float" "float64" "string" "dict" "slice" }}
{{ $errorMsg := slice }}
{{ if and $def $def.type }}
{{ $aliases := slice | append $def.type }}
{{ range $alias := $aliases }}
{{ with index $types $alias }}
<!-- Reduce child elements to slice of argument names -->
{{ $args := slice }}
{{ $reflect = printf "%T" . }}
{{ if reflect.IsMap . }}
{{ range $k, $_ := . }}
{{ $args = $args | append $k }}
{{ end }}
{{ else if reflect.IsSlice . }}
{{ with index . 0 }}
{{ range $k, $_ := . }}
{{ $args = $args | append $k }}
{{ end }}
{{ end }}
{{ end }}
<!-- Retrieve type definition for each argument -->
{{ $definitions := dict }}
{{ $definitions := merge $definitions (dict "_reflect" $reflect) }}
{{ range $args }}
{{ $type := partial "inline/type-definition.html" (dict "key" . "arguments" $arguments "types" $types) }}
{{ if and $type $type.definition }}
{{ $definitions = merge $definitions (dict . $type.definition) }}
{{ else }}
{{- $errorMsg = $errorMsg | append (printf "partial [utilities/InitTypes.html] - Missing type for '%s.%s'" $key . ) -}}
{{ end }}
{{ end }}
{{ $udt = dict $alias $definitions }}
{{ end }}
{{ end }}
{{ end }}
{{ $merged := or $def dict }}
{{ if reflect.IsMap $val }}{{ $merged = merge $merged $val }}{{ end }}
{{ if not $merged.type }}
{{- $errorMsg = $errorMsg | append (printf "partial [utilities/InitTypes.html] - Missing type for '%s'" $key ) -}}
{{ end }}
{{ return (dict "definition" $merged "udt" $udt "errmsg" $errorMsg) }}
{{ end }}
{{/* Initalize arguments and local variables */}}
{{ $error := false }}
{{ $errmsg := slice }}
{{ $warnmsg := slice }}
{{ $params := dict }}
{{ $definitions := dict }}
{{ $udt := dict }}
{{ $structure := .structure }}
{{ $bookshop := .bookshop }}
{{ $group := .group }}
{{ $child := .child }}
{{ $level := .level | default 0 }}
{{ if gt $level 5 }}
{{ errorf "recursion detected: %s / %s" $structure $bookshop }}
{{ return }}
{{ end }}
{{ if and (not $structure) (not $bookshop) }}
{{- $errmsg = $errmsg | append (printf "partial [utilities/InitTypes.html] - Missing value for param 'structure' or 'bookshop'") -}}
{{ $error = true }}
{{ end }}
{{/* Initalize the type structure */}}
{{ if not $error }}
{{ $args := dict }}
{{ $arguments := index (index site.Data.structures "_arguments") "arguments" }}
{{ $types := index (index site.Data.structures "_types") "types" }}
{{/* Initalize the regular or bookshop argument structure */}}
{{ if $structure }}
{{ if index site.Data.structures $structure}}
{{ $args = (index site.Data.structures $structure).arguments | default dict }}
{{ else }}
{{ $args = (index $types $structure) | default dict }}
{{ end }}
{{ else }}
{{ $args = index (index (index site.Data.structures.components $bookshop) (printf "%s.bookshop" $bookshop)) "blueprint" | default dict }}
{{ $args = merge $args (dict "_bookshop_name" nil "_ordinal" nil "id" nil) }}
{{ end }}
{{/* Merge any child arguments */}}
{{ if $child }}
{{ $extra_def := (index site.Data.structures $child).arguments }}
{{ if not $extra_def }}
{{- $errmsg = $errmsg | append (printf "partial [utilities/InitTypes.html] - Missing definitions: %s" $child) -}}
{{ $error = true }}
{{ else }}
{{ range $key, $val := $extra_def }}
{{ if and $val $val.parent }}
{{ $newval := dict }}
{{ range $k, $v := $val }}
{{ if ne $k "default" }}{{ $newval = merge $newval (dict $k $v) }}{{ end }}
{{ end}}
{{ $args = merge $args (dict $key $newval) }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{/* Initialize the arguments and their type definitions recursively */}}
{{ range $key, $v := $args }}
{{ $type := partial "inline/type-definition.html" (dict "key" $key "val" $v "args" $args "arguments" $arguments "types" $types "structure" $structure) }}
{{ $errmsg = $errmsg | append $type.errmsg }}
{{ if and $type $type.definition $type.definition.type }}
{{ $definitions = merge $definitions (dict $key $type.definition) }}
{{ with $type.udt }}
{{ $udt = merge $udt . }}
{{ end }}
{{ else }}
{{- $errmsg = $errmsg | append (printf "partial [utilities/InitTypes.html] - Missing type for '%s' in '%s'" $key (or $structure $bookshop) ) -}}
{{ $error = true }}
{{ end }}
{{ end }}
{{ end }}
{{ $params = merge $params (dict "types" $definitions "udt" $udt "err" $error "errmsg" $errmsg "warnmsg" $warnmsg) }}
{{ return $params }}