/
string.go
51 lines (43 loc) · 1.09 KB
/
string.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
47
48
49
50
51
package odata
import (
"encoding/json"
"fmt"
"github.com/go-errors/errors"
"reflect"
"strings"
)
// Type for Edm.String
type String string
// Maps String to the graphql scalar type in the schema.
func (String) ImplementsGraphQLType(name string) bool {
return name == "String"
}
// A custom unmarshaler for String type
func (t *String) UnmarshalGraphQL(input interface{}) error {
switch input := input.(type) {
case string:
*t = String(input)
return nil
default:
return errors.Errorf(convertErrorFormat, reflect.TypeOf(input), reflect.TypeOf(*t))
}
}
// A custom json/graphql marshaller for String type
func (t String) MarshalJSON() ([]byte, error) {
return json.Marshal(string(t))
}
// A custom json unmarshaller for String type
func (t *String) UnmarshalJSON(b []byte) error {
s := ""
err := json.Unmarshal(b, &s)
*t = String(s)
return err
}
// Escape quotes
func (t String) Escape() string {
return strings.Replace(string(t), "'", "''", -1)
}
// A custom marshaller to uri query format for String type
func (t String) AsParameter() string {
return fmt.Sprintf("'%s'", t.Escape())
}