github.com/galdor/go-ejson@v0.0.0-20231201100034-d335379f26b0/values.go (about)

     1  package ejson
     2  
     3  import "fmt"
     4  
     5  type InvalidValueError struct {
     6  	Value interface{}
     7  }
     8  
     9  func (err *InvalidValueError) Error() string {
    10  	return fmt.Sprintf("%#v (%T) is not a valid json value",
    11  		err.Value, err.Value)
    12  }
    13  
    14  func IsNull(v interface{}) bool {
    15  	return v == nil
    16  }
    17  
    18  func IsNumber(v interface{}) bool {
    19  	_, ok := v.(float64)
    20  	return ok
    21  }
    22  
    23  func IsString(v interface{}) bool {
    24  	_, ok := v.(string)
    25  	return ok
    26  }
    27  
    28  func IsBoolean(v interface{}) bool {
    29  	_, ok := v.(bool)
    30  	return ok
    31  }
    32  
    33  func IsArray(v interface{}) bool {
    34  	_, ok := v.([]interface{})
    35  	return ok
    36  }
    37  
    38  func IsObject(v interface{}) bool {
    39  	_, ok := v.(map[string]interface{})
    40  	return ok
    41  }
    42  
    43  func AsNumber(v interface{}) float64 {
    44  	return v.(float64)
    45  }
    46  
    47  func AsString(v interface{}) string {
    48  	return v.(string)
    49  }
    50  
    51  func AsBoolean(v interface{}) bool {
    52  	return v.(bool)
    53  }
    54  
    55  func AsArray(v interface{}) []interface{} {
    56  	return v.([]interface{})
    57  }
    58  
    59  func AsObject(v interface{}) map[string]interface{} {
    60  	return v.(map[string]interface{})
    61  }
    62  
    63  func Equal(v1, v2 interface{}) bool {
    64  	switch {
    65  	case IsNull(v1) && IsNull(v2):
    66  		return true
    67  
    68  	case IsNumber(v1) && IsNumber(v2):
    69  		return AsNumber(v1) == AsNumber(v2)
    70  
    71  	case IsString(v1) && IsString(v2):
    72  		return AsString(v1) == AsString(v2)
    73  
    74  	case IsBoolean(v1) && IsBoolean(v2):
    75  		return AsBoolean(v1) == AsBoolean(v2)
    76  
    77  	case IsArray(v1) && IsArray(v2):
    78  		a1 := AsArray(v1)
    79  		a2 := AsArray(v2)
    80  
    81  		if len(a1) != len(a2) {
    82  			return false
    83  		}
    84  
    85  		for i := 0; i < len(a1); i++ {
    86  			if !Equal(a1[i], a2[i]) {
    87  				return false
    88  			}
    89  		}
    90  
    91  		return true
    92  
    93  	case IsObject(v1) && IsObject(v2):
    94  		obj1 := AsObject(v1)
    95  		obj2 := AsObject(v2)
    96  
    97  		for key, value1 := range obj1 {
    98  			value2, found := obj2[key]
    99  			if !found || !Equal(value1, value2) {
   100  				return false
   101  			}
   102  		}
   103  
   104  		for key, value2 := range obj2 {
   105  			value1, found := obj1[key]
   106  			if !found || !Equal(value1, value2) {
   107  				return false
   108  			}
   109  		}
   110  
   111  		return true
   112  	}
   113  
   114  	return false
   115  }
   116  
   117  func ObjectKeys(v interface{}) []string {
   118  	obj := AsObject(v)
   119  
   120  	keys := make([]string, len(obj))
   121  
   122  	i := 0
   123  	for key := range obj {
   124  		keys[i] = key
   125  		i++
   126  	}
   127  
   128  	return keys
   129  }
   130  
   131  func ObjectValues(v interface{}) []interface{} {
   132  	obj := AsObject(v)
   133  
   134  	values := make([]interface{}, len(obj))
   135  
   136  	i := 0
   137  	for _, value := range obj {
   138  		values[i] = value
   139  		i++
   140  	}
   141  
   142  	return values
   143  }