github.com/mavryk-network/mvgo@v1.19.9/internal/compose/file.go (about) 1 // Copyright (c) 2023 Blockwatch Data Inc. 2 // Author: alex@blockwatch.cc, abdul@blockwatch.cc 3 4 package compose 5 6 import ( 7 "encoding/json" 8 "fmt" 9 "os" 10 "strings" 11 12 "github.com/tidwall/gjson" 13 ) 14 15 func ReadJsonFile[T any](name string) (*T, error) { 16 name, jsonPath, hasPath := strings.Cut(name, "#") 17 var val *T 18 buf, err := os.ReadFile(name) 19 if err != nil { 20 return nil, err 21 } 22 if hasPath { 23 res := gjson.GetBytes(buf, jsonPath) 24 if !res.Exists() { 25 return nil, fmt.Errorf("missing path %q in file %s", jsonPath, name) 26 } 27 buf = []byte(res.Raw) 28 } 29 err = json.Unmarshal(buf, &val) 30 if err != nil { 31 return nil, err 32 } 33 return val, nil 34 }