Debian Codegraph @

resources/postpub/fields.go

raw
1 // Copyright 2020 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package postpub
15
16 import (
17 "reflect"
18 )
19
20 const (
21 FieldNotSupported = "__field_not_supported"
22 )
23
24 func structToMapWithPlaceholders(root string, in any, createPlaceholder func(s string) string) map[string]any {
25 m := structToMap(in)
26 insertFieldPlaceholders(root, m, createPlaceholder)
27 return m
28 }
29
30 func structToMap(s any) map[string]any {
31 m := make(map[string]any)
32 t := reflect.TypeOf(s)
33
34 for method := range t.Methods() {
35
36 if method.PkgPath != "" {
37 continue
38 }
39 if method.Type.NumIn() == 1 {
40 m[method.Name] = ""
41 }
42 }
43
44 for field := range t.Fields() {
45 if field.PkgPath != "" {
46 continue
47 }
48 m[field.Name] = ""
49 }
50 return m
51 }
52
53 // insert placeholder for the templates. Do it very shallow for now.
54 func insertFieldPlaceholders(root string, m map[string]any, createPlaceholder func(s string) string) {
55 for k := range m {
56 m[k] = createPlaceholder(root + "." + k)
57 }
58 }