Skip to content

Commit

Permalink
added methods for typed value fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
sinhashubham95 committed Jan 9, 2021
1 parent adc9995 commit 0dbf8b0
Show file tree
Hide file tree
Showing 2 changed files with 286 additions and 0 deletions.
1 change: 1 addition & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ var (
ErrIndexNotFound = errors.New("expected index for json array but found something else")
ErrIndexOutOfBound = errors.New("index out of bounds of the json array")
ErrNoDataFound = errors.New("no tree satisfies the path elements provided")
ErrInvalidType = errors.New("data at the specified path does not match the expected type")
)
285 changes: 285 additions & 0 deletions jsonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,291 @@ func (j *Jsonic) Child(path string) (*Jsonic, error) {
return j.child(strings.Split(path, dot))
}

// Get is used to get the data at the path specified.
func (j *Jsonic) Get(path string) (interface{}, error) {
child, err := j.Child(path)
if err != nil {
return nil, err
}
return child.data, nil
}

// GetInt is used to get the integer at the path specified.
func (j *Jsonic) GetInt(path string) (int, error) {
val, err := j.Get(path)
if err != nil {
return 0, err
}
if i, ok := val.(int); ok {
return i, nil
}
return 0, ErrInvalidType
}

// GetInt64 is used to get the integer at the path specified.
func (j *Jsonic) GetInt64(path string) (int64, error) {
val, err := j.Get(path)
if err != nil {
return 0, err
}
if i, ok := val.(int64); ok {
return i, nil
}
return 0, ErrInvalidType
}

// GetFloat is used to get the floating point number at the path specified.
func (j *Jsonic) GetFloat(path string) (float32, error) {
val, err := j.Get(path)
if err != nil {
return 0, err
}
if f, ok := val.(float32); ok {
return f, nil
}
return 0, ErrInvalidType
}

// GetFloat64 is used to get the floating point number at the path specified.
func (j *Jsonic) GetFloat64(path string) (float64, error) {
val, err := j.Get(path)
if err != nil {
return 0, err
}
if f, ok := val.(float64); ok {
return f, nil
}
return 0, ErrInvalidType
}

// GetBool is used to get the integer at the path specified.
func (j *Jsonic) GetBool(path string) (bool, error) {
val, err := j.Get(path)
if err != nil {
return false, err
}
if b, ok := val.(bool); ok {
return b, nil
}
return false, ErrInvalidType
}

// GetString is used to get the string at the path specified.
func (j *Jsonic) GetString(path string) (string, error) {
val, err := j.Get(path)
if err != nil {
return "", err
}
if s, ok := val.(string); ok {
return s, nil
}
return "", ErrInvalidType
}

// GetArray is used to get the data array at the path specified.
func (j *Jsonic) GetArray(path string) ([]interface{}, error) {
val, err := j.Get(path)
if err != nil {
return nil, err
}
if a, ok := val.([]interface{}); ok {
return a, nil
}
return nil, ErrInvalidType
}

// GetIntArray is used to get the integer array at the path specified.
func (j *Jsonic) GetIntArray(path string) ([]int, error) {
val, err := j.GetArray(path)
if err != nil {
return nil, err
}
iArr := make([]int, len(val))
for index, v := range val {
if i, ok := v.(int); ok {
iArr[index] = i
}
}
return iArr, nil
}

// GetInt64Array is used to get the 64-bit integer array at the path specified.
func (j *Jsonic) GetInt64Array(path string) ([]int64, error) {
val, err := j.GetArray(path)
if err != nil {
return nil, err
}
iArr := make([]int64, len(val))
for index, v := range val {
if i, ok := v.(int64); ok {
iArr[index] = i
}
}
return iArr, nil
}

// GetFloatArray is used to get the floating point number array at the path specified.
func (j *Jsonic) GetFloatArray(path string) ([]float32, error) {
val, err := j.GetArray(path)
if err != nil {
return nil, err
}
fArr := make([]float32, len(val))
for index, v := range val {
if f, ok := v.(float32); ok {
fArr[index] = f
}
}
return fArr, nil
}

// GetFloat64Array is used to get the 64-bit floating point number array at the path specified.
func (j *Jsonic) GetFloat64Array(path string) ([]float64, error) {
val, err := j.GetArray(path)
if err != nil {
return nil, err
}
fArr := make([]float64, len(val))
for index, v := range val {
if f, ok := v.(float64); ok {
fArr[index] = f
}
}
return fArr, nil
}

// GetBoolArray is used to get the boolean array at the path specified.
func (j *Jsonic) GetBoolArray(path string) ([]bool, error) {
val, err := j.GetArray(path)
if err != nil {
return nil, err
}
bArr := make([]bool, len(val))
for index, v := range val {
if b, ok := v.(bool); ok {
bArr[index] = b
}
}
return bArr, nil
}

// GetStringArray is used to get the string array at the path specified.
func (j *Jsonic) GetStringArray(path string) ([]string, error) {
val, err := j.GetArray(path)
if err != nil {
return nil, err
}
sArr := make([]string, len(val))
for index, v := range val {
if s, ok := v.(string); ok {
sArr[index] = s
}
}
return sArr, nil
}

// GetMap is used to get the data map at the path specified.
func (j *Jsonic) GetMap(path string) (map[string]interface{}, error) {
val, err := j.Get(path)
if err != nil {
return nil, err
}
if m, ok := val.(map[string]interface{}); ok {
return m, nil
}
return nil, ErrInvalidType
}

// GetIntMap is used to get the integer map at the path specified.
func (j *Jsonic) GetIntMap(path string) (map[string]int, error) {
val, err := j.GetMap(path)
if err != nil {
return nil, err
}
iMap := make(map[string]int)
for k, v := range val {
if i, ok := v.(int); ok {
iMap[k] = i
}
}
return iMap, nil
}

// GetInt64Map is used to get the 64-bit integer map at the path specified.
func (j *Jsonic) GetInt64Map(path string) (map[string]int64, error) {
val, err := j.GetMap(path)
if err != nil {
return nil, err
}
iMap := make(map[string]int64)
for k, v := range val {
if i, ok := v.(int64); ok {
iMap[k] = i
}
}
return iMap, nil
}

// GetFloatMap is used to get the floating point number map at the path specified.
func (j *Jsonic) GetFloatMap(path string) (map[string]float32, error) {
val, err := j.GetMap(path)
if err != nil {
return nil, err
}
fMap := make(map[string]float32)
for k, v := range val {
if f, ok := v.(float32); ok {
fMap[k] = f
}
}
return fMap, nil
}

// GetFloat64Map is used to get the 64-bit floating point number map at the path specified.
func (j *Jsonic) GetFloat64Map(path string) (map[string]float64, error) {
val, err := j.GetMap(path)
if err != nil {
return nil, err
}
fMap := make(map[string]float64)
for k, v := range val {
if f, ok := v.(float64); ok {
fMap[k] = f
}
}
return fMap, nil
}

// GetBoolMap is used to get the boolean map at the path specified.
func (j *Jsonic) GetBoolMap(path string) (map[string]bool, error) {
val, err := j.GetMap(path)
if err != nil {
return nil, err
}
bMap := make(map[string]bool)
for k, v := range val {
if b, ok := v.(bool); ok {
bMap[k] = b
}
}
return bMap, nil
}

// GetStringMap is used to get the string map at the path specified.
func (j *Jsonic) GetStringMap(path string) (map[string]string, error) {
val, err := j.GetMap(path)
if err != nil {
return nil, err
}
sMap := make(map[string]string)
for k, v := range val {
if s, ok := v.(string); ok {
sMap[k] = s
}
}
return sMap, nil
}

func new(data interface{}) *Jsonic {
return &Jsonic{
data: data,
Expand Down

0 comments on commit 0dbf8b0

Please sign in to comment.