-
Notifications
You must be signed in to change notification settings - Fork 18
/
format.go
46 lines (41 loc) · 985 Bytes
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package errors
type ErrorFormatter interface {
Format(kind string, elem *string, ctxkind string, ctx string) string
}
type defaultFormatter struct {
verb string
msg string
preposition string
}
func NewDefaultFormatter(verb, msg, preposition string) ErrorFormatter {
if verb != "" {
verb += " "
}
return &defaultFormatter{
verb: verb,
msg: msg,
preposition: preposition,
}
}
func (f *defaultFormatter) Format(kind string, elem *string, ctxkind string, ctx string) string {
if ctx != "" {
if ctxkind != "" {
ctx = ctxkind + " " + ctx
}
ctx = " " + f.preposition + " " + ctx
}
elems := ""
if elem != nil {
elems = "\"" + *elem + "\" "
}
if kind != "" {
kind += " "
}
if kind == "" && elems == "" {
return f.msg + ctx
}
return kind + elems + f.verb + f.msg + ctx
}