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 )
84
85 fmt.Fprintf(f, `%s
86
87 package page
88
89 %s
90
91
92 %s
93
94
95 `, header, importsString(pkgImports), marshalJSON)
96
97 return nil
98 }
99
100 func importsString(imps []string) string {
101 if len(imps) == 0 {
102 return ""
103 }
104
105 if len(imps) == 1 {
106 return fmt.Sprintf("import %q", imps[0])
107 }
108
109 var impsStr strings.Builder
110 impsStr.WriteString("import (\n")
111 for _, imp := range imps {
112 impsStr.WriteString(fmt.Sprintf("%q\n", imp))
113 }
114
115 return impsStr.String() + ")"
116 }