Debian Codegraph @

resources/page/page_generate/generate_page_wrappers.go

raw
1 // Copyright 2019 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 page_generate
15
16 import (
17 "errors"
18 "fmt"
19 "os"
20 "path/filepath"
21 "reflect"
22 "strings"
23
24 "github.com/gohugoio/hugo/codegen"
25 "github.com/gohugoio/hugo/resources/page"
26 )
27
28 const header = `// Copyright 2019 The Hugo Authors. All rights reserved.
29 //
30 // Licensed under the Apache License, Version 2.0 (the "License");
31 // you may not use this file except in compliance with the License.
32 // You may obtain a copy of the License at
33 // http://www.apache.org/licenses/LICENSE-2.0
34 //
35 // Unless required by applicable law or agreed to in writing, software
36 // distributed under the License is distributed on an "AS IS" BASIS,
37 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38 // See the License for the specific language governing permissions and
39 // limitations under the License.
40
41 // This file is autogenerated.
42 `
43
44 var (
45 pageInterface = reflect.TypeFor[page.PageMetaProvider]()
46
47 packageDir = filepath.FromSlash("resources/page")
48 )
49
50 func Generate(c *codegen.Inspector) error {
51 if err := generateMarshalJSON(c); err != nil {
52 return fmt.Errorf("failed to generate JSON marshaler: %w", err)
53 }
54
55 return nil
56 }
57
58 func generateMarshalJSON(c *codegen.Inspector) error {
59 filename := filepath.Join(c.ProjectRootDir, packageDir, "page_marshaljson.autogen.go")
60 f, err := os.Create(filename)
61 if err != nil {
62 return err
63 }
64 defer f.Close()
65
66 includes := []reflect.Type{pageInterface}
67
68 excludes := []reflect.Type{}
69
70 methods := c.MethodsFromTypes(
71 includes,
72 excludes)
73
74 if len(methods) == 0 {
75 return errors.New("no methods found")
76 }
77
78 marshalJSON, pkgImports := methods.ToMarshalJSON(
79 "Page",
80 "github.com/gohugoio/hugo/resources/page",
81 // Exclusion regexps. Matches method names.
82 `\bPage\b`,
83 `\bIsNode\b`,
84 )
85
86 fmt.Fprintf(f, `%s
87
88 package page
89
90 %s
91
92
93 %s
94
95
96 `, header, importsString(pkgImports), marshalJSON)
97
98 return nil
99 }
100
101 func importsString(imps []string) string {
102 if len(imps) == 0 {
103 return ""
104 }
105
106 if len(imps) == 1 {
107 return fmt.Sprintf("import %q", imps[0])
108 }
109
110 var impsStr strings.Builder
111 impsStr.WriteString("import (\n")
112 for _, imp := range imps {
113 impsStr.WriteString(fmt.Sprintf("%q\n", imp))
114 }
115
116 return impsStr.String() + ")"
117 }