mirror of
https://github.com/gethinode/hinode.git
synced 2025-10-07 18:14:28 +00:00
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
6761bedd51 |
238
layouts/_partials/utilities/InitArgs.html~~~
Normal file
238
layouts/_partials/utilities/InitArgs.html~~~
Normal 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 }}
|
144
layouts/_partials/utilities/InitTypes.html~
Normal file
144
layouts/_partials/utilities/InitTypes.html~
Normal 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 }}
|
Reference in New Issue
Block a user