Skip to content

Commit

Permalink
let the json package figure out how to decode the elements of the inc…
Browse files Browse the repository at this point in the history
…oming map (#380)
  • Loading branch information
barnjamin authored Aug 31, 2022
1 parent aa22fdf commit 7297d6e
Showing 1 changed file with 10 additions and 36 deletions.
46 changes: 10 additions & 36 deletions logic/source_map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logic

import (
"encoding/json"
"fmt"
"strings"
)
Expand All @@ -19,47 +20,20 @@ type SourceMap struct {
}

func DecodeSourceMap(ism map[string]interface{}) (SourceMap, error) {
sm := SourceMap{}

if v, ok := ism["version"]; ok {
switch t := v.(type) {
case float64:
sm.Version = int(t)
case uint64:
sm.Version = int(t)
}
}

if sm.Version != 3 {
return sm, fmt.Errorf("only version 3 is supported")
}

if f, ok := ism["file"]; ok {
sm.File = f.(string)
}
var sm SourceMap

if sr, ok := ism["sourceRoot"]; ok {
sm.SourceRoot = sr.(string)
buff, err := json.Marshal(ism)
if err != nil {
return sm, err
}

if srcs, ok := ism["sources"]; ok {
srcSlice := srcs.([]interface{})
sm.Sources = make([]string, len(srcSlice))
for idx, s := range srcSlice {
sm.Sources[idx] = s.(string)
}
err = json.Unmarshal(buff, &sm)
if err != nil {
return sm, err
}

if names, ok := ism["names"]; ok {
nameSlice := names.([]interface{})
sm.Names = make([]string, len(nameSlice))
for idx, n := range nameSlice {
sm.Names[idx] = n.(string)
}
}

if m, ok := ism["mappings"]; ok {
sm.Mappings = m.(string)
if sm.Version != 3 {
return sm, fmt.Errorf("only version 3 is supported")
}

if sm.Mappings == "" {
Expand Down

0 comments on commit 7297d6e

Please sign in to comment.