|
7 | 7 | "fmt"
|
8 | 8 | "strings"
|
9 | 9 |
|
10 |
| - "go4.org/bytereplacer" |
11 |
| - |
12 | 10 | "github.com/operator-framework/operator-registry/alpha/property"
|
13 | 11 | )
|
14 | 12 |
|
@@ -100,25 +98,43 @@ func (m Meta) MarshalJSON() ([]byte, error) {
|
100 | 98 | }
|
101 | 99 |
|
102 | 100 | func (m *Meta) UnmarshalJSON(blob []byte) error {
|
103 |
| - blob = bytereplacer.New(`\u003c`, "<", `\u003e`, ">", `\u0026`, "&").Replace(blob) |
| 101 | + blobMap := map[string]interface{}{} |
| 102 | + if err := json.Unmarshal(blob, &blobMap); err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + for key, ptr := range map[string]*string{"schema": &m.Schema, "package": &m.Package, "name": &m.Name} { |
| 106 | + if _, ok := blobMap[key]; !ok { |
| 107 | + continue |
| 108 | + } |
| 109 | + v, ok := blobMap[key].(string) |
| 110 | + if !ok { |
| 111 | + return fmt.Errorf("expected value for key %q to be a string, got %t: %v", key, blobMap[key], blobMap[key]) |
| 112 | + } |
| 113 | + *ptr = v |
| 114 | + } |
104 | 115 |
|
105 |
| - type tmp struct { |
106 |
| - Schema string `json:"schema"` |
107 |
| - Package string `json:"package,omitempty"` |
108 |
| - Name string `json:"name,omitempty"` |
109 |
| - Properties []property.Property `json:"properties,omitempty"` |
| 116 | + // TODO: this ensures we can continue parsing FBC templates whose FBC fields |
| 117 | + // begin with a capital letter. We should decide if we want to continue |
| 118 | + // supporting this or not. Then we either remove this comment or remove |
| 119 | + // this code (and the comment). |
| 120 | + for key, ptr := range map[string]*string{"Schema": &m.Schema, "Package": &m.Package, "Name": &m.Name} { |
| 121 | + if _, ok := blobMap[key]; !ok || *ptr != "" { |
| 122 | + continue |
| 123 | + } |
| 124 | + v, ok := blobMap[key].(string) |
| 125 | + if !ok { |
| 126 | + return fmt.Errorf("expected value for key %q to be a string, got %t: %v", key, blobMap[key], blobMap[key]) |
| 127 | + } |
| 128 | + *ptr = v |
110 | 129 | }
|
111 |
| - var t tmp |
112 |
| - if err := json.Unmarshal(blob, &t); err != nil { |
113 |
| - // TODO: return an error that includes the the full JSON message, |
114 |
| - // the offset of the error, and the error message. Let callers |
115 |
| - // decide how to format it. |
116 |
| - return errors.New(resolveUnmarshalErr(blob, err)) |
| 130 | + |
| 131 | + buf := bytes.Buffer{} |
| 132 | + enc := json.NewEncoder(&buf) |
| 133 | + enc.SetEscapeHTML(false) |
| 134 | + if err := enc.Encode(blobMap); err != nil { |
| 135 | + return err |
117 | 136 | }
|
118 |
| - m.Schema = t.Schema |
119 |
| - m.Package = t.Package |
120 |
| - m.Name = t.Name |
121 |
| - m.Blob = blob |
| 137 | + m.Blob = buf.Bytes() |
122 | 138 | return nil
|
123 | 139 | }
|
124 | 140 |
|
|
0 commit comments