Decode will decode the nbt file into v and return the root name of the file
(v any)
| 121 | |
| 122 | // Decode will decode the nbt file into v and return the root name of the file |
| 123 | func (d *Decoder) Decode(v any) (rootName string, err error) { |
| 124 | val := reflect.ValueOf(v) |
| 125 | if val.Kind() != reflect.Pointer { |
| 126 | return "", fmt.Errorf("Decode expects a pointer") |
| 127 | } |
| 128 | val = val.Elem() |
| 129 | |
| 130 | typeId, err := d.readByte() |
| 131 | if err != nil { |
| 132 | return "", err |
| 133 | } |
| 134 | if typeId != Compound { |
| 135 | return "", fmt.Errorf("expected a compound first element") |
| 136 | } |
| 137 | |
| 138 | if !d.dontReadRootCompoundName { |
| 139 | rootName, err = d.readString() |
| 140 | if err != nil { |
| 141 | return |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | switch val.Kind() { |
| 146 | case reflect.Struct: |
| 147 | m := generateMap(val) |
| 148 | defer func() { |
| 149 | clear(m) |
| 150 | valueMap.Put(m) |
| 151 | }() |
| 152 | |
| 153 | if err := d.decodeCompoundStruct(m); err != nil { |
| 154 | return "", err |
| 155 | } |
| 156 | case reflect.Map: |
| 157 | if val.IsNil() { |
| 158 | val.Set(reflect.MakeMap(val.Type())) |
| 159 | } |
| 160 | if err := d.decodeCompoundMap(val); err != nil { |
| 161 | return rootName, err |
| 162 | } |
| 163 | default: |
| 164 | return rootName, fmt.Errorf("Decode expects a pointer of struct/map, not %s", val.Type()) |
| 165 | } |
| 166 | |
| 167 | return |
| 168 | } |
| 169 | |
| 170 | func (d *Decoder) decodeCompoundMap(_map reflect.Value) error { |
| 171 | var typeId [1]byte |
no test coverage detected