| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package templateproto | 5 package templateproto |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 | 9 |
| 10 "golang.org/x/net/context" | |
| 11 | |
| 12 "github.com/luci/luci-go/common/config" | |
| 13 "github.com/luci/luci-go/common/proto" | 10 "github.com/luci/luci-go/common/proto" |
| 14 ) | 11 ) |
| 15 | 12 |
| 16 // LoadFile loads a File from config service by configSet and path. | 13 // LoadFile loads a File from a string containing the template text protobuf. |
| 17 // | 14 // |
| 18 // Expects config.Interface to be in the context already. | 15 // Expects config.Interface to be in the context already. |
| 19 func LoadFile(c context.Context, configSet, path string) (file *File, vers strin
g, err error) { | 16 func LoadFile(data string) (file *File, err error) { |
| 20 » templateData, err := config.GetConfig(c, configSet, path, false) | |
| 21 » if err != nil { | |
| 22 » » return | |
| 23 » } | |
| 24 file = &File{} | 17 file = &File{} |
| 25 » vers = templateData.ContentHash | 18 » if err = proto.UnmarshalTextML(data, file); err != nil { |
| 26 » if err = proto.UnmarshalTextML(templateData.Content, file); err != nil { | |
| 27 return | 19 return |
| 28 } | 20 } |
| 29 err = file.Normalize() | 21 err = file.Normalize() |
| 30 return | 22 return |
| 31 } | 23 } |
| 32 | 24 |
| 33 // Render renders the specified template with the given parameters. | 25 // Render renders the specified template with the given parameters. |
| 34 func (f *File) Render(spec *Specifier) (string, error) { | 26 func (f *File) Render(spec *Specifier) (string, error) { |
| 35 ret, err := f.Template[spec.TemplateName].Render(spec.Params) | 27 ret, err := f.Template[spec.TemplateName].Render(spec.Params) |
| 36 if err != nil { | 28 if err != nil { |
| 37 err = fmt.Errorf("rendering %q: %s", spec.TemplateName, err) | 29 err = fmt.Errorf("rendering %q: %s", spec.TemplateName, err) |
| 38 } | 30 } |
| 39 return ret, err | 31 return ret, err |
| 40 } | 32 } |
| 41 | 33 |
| 42 // RenderL renders a specified template with go literal arguments. | 34 // RenderL renders a specified template with go literal arguments. |
| 43 func (f *File) RenderL(templName string, params LiteralMap) (ret string, err err
or) { | 35 func (f *File) RenderL(templName string, params LiteralMap) (ret string, err err
or) { |
| 44 spec := &Specifier{TemplateName: templName} | 36 spec := &Specifier{TemplateName: templName} |
| 45 spec.Params, err = params.Convert() | 37 spec.Params, err = params.Convert() |
| 46 if err != nil { | 38 if err != nil { |
| 47 return | 39 return |
| 48 } | 40 } |
| 49 return f.Render(spec) | 41 return f.Render(spec) |
| 50 } | 42 } |
| OLD | NEW |