|
1 | 1 | package external |
2 | 2 |
|
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + |
| 7 | + "github.com/99designs/gqlgen/graphql" |
| 8 | +) |
| 9 | + |
3 | 10 | type ( |
4 | | - ObjectID int |
5 | | - Manufacturer string // remote named string |
6 | | - Count uint32 // remote named uint32 |
| 11 | + ObjectID int |
| 12 | + Manufacturer string // remote named string |
| 13 | + Count uint8 // remote named uint8 |
| 14 | + ExternalBytes []byte |
| 15 | + ExternalRunes []rune |
7 | 16 | ) |
8 | 17 |
|
9 | 18 | const ( |
10 | 19 | ManufacturerTesla Manufacturer = "TESLA" |
11 | 20 | ManufacturerHonda Manufacturer = "HONDA" |
12 | 21 | ManufacturerToyota Manufacturer = "TOYOTA" |
13 | 22 | ) |
| 23 | + |
| 24 | +func MarshalBytes(b ExternalBytes) graphql.Marshaler { |
| 25 | + return graphql.WriterFunc(func(w io.Writer) { |
| 26 | + _, _ = fmt.Fprintf(w, "%q", string(b)) |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +func UnmarshalBytes(v interface{}) (ExternalBytes, error) { |
| 31 | + switch v := v.(type) { |
| 32 | + case string: |
| 33 | + return ExternalBytes(v), nil |
| 34 | + case *string: |
| 35 | + return ExternalBytes(*v), nil |
| 36 | + case ExternalBytes: |
| 37 | + return v, nil |
| 38 | + default: |
| 39 | + return nil, fmt.Errorf("%T is not ExternalBytes", v) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func MarshalRunes(r ExternalRunes) graphql.Marshaler { |
| 44 | + return graphql.WriterFunc(func(w io.Writer) { |
| 45 | + _, _ = fmt.Fprintf(w, "%q", string(r)) |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +func UnmarshalRunes(v interface{}) (ExternalRunes, error) { |
| 50 | + switch v := v.(type) { |
| 51 | + case string: |
| 52 | + return ExternalRunes(v), nil |
| 53 | + case *string: |
| 54 | + return ExternalRunes(*v), nil |
| 55 | + case ExternalRunes: |
| 56 | + return v, nil |
| 57 | + default: |
| 58 | + return nil, fmt.Errorf("%T is not ExternalRunes", v) |
| 59 | + } |
| 60 | +} |
0 commit comments