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