mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00

* Return Auth service Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Compose to run with SpiceDB and Auth svc Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update auth gRPC API Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove Users' policies Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Move Groups to internal Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use shared groups in Users Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove unused code Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use pkg Groups in Things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove Things groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Make imports consistent Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Groups networking Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove things groups-specific API Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Move Things Clients to the root Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Move Clients to Users root Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Temporarily remove tracing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix imports Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add buffer config for gRPC Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update auth type for Things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use Auth for login Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add temporary solution for refresh token Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Tokenizer interface Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Updade tokens issuing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix token issuing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update JWT validator and refactor Tokenizer Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Rename access timeout Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Rename login to authenticate Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Identify to use SubjectID Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add Auth to Groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use the Auth service for Groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update auth schema Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix Auth for Groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add auth for addons (#14) Signed-off-by: Arvindh <arvindh91@gmail.com> Speparate Login and Refresh tokens Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Merge authN and authZ requests for things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add connect and disconnect Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update sharing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix policies addition and removal Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update relation with roels Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add gRPC to Things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Assign and Unassign members to group and Listing of Group members (#15) * add auth for addons Signed-off-by: Arvindh <arvindh91@gmail.com> * add assign and unassign to group Signed-off-by: Arvindh <arvindh91@gmail.com> * add group incomplete repo implementation Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Move coap mqtt and ws policies to spicedb (#16) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Remove old policies Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> NOISSUE - Things authorize to return thingID (#18) This commit modifies the authorize endpoint to the grpc endpoint to return thingID. The authorize endpoint allows adapters to get the publisher of the message. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Add Groups to users service (#17) * add assign and unassign to group Signed-off-by: Arvindh <arvindh91@gmail.com> * add group incomplete repo implementation Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users stable 1 Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users stable 2 Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users & things Signed-off-by: Arvindh <arvindh91@gmail.com> * Amend signature Signed-off-by: Arvindh <arvindh91@gmail.com> * fix merge error Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Fix es code (#21) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Fix Bugs (#20) * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Test e2e (#19) * fix: connect method Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * fix: e2e Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * fix changes in sdk and e2e Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(docker): remove unnecessary port mapping Remove the port mapping for MQTT broker in the docker-compose.yml file. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Enable group listing Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(responses): update ChannelsPage struct The ChannelsPage struct in the responses.go file has been updated. The "Channels" field has been renamed to "Groups" to provide more accurate naming. This change ensures consistency and clarity in the codebase. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(things): add UpdateClientSecret method Add the UpdateClientSecret method to the things service. This method allows updating the client secret for a specific client identified by the provided token, id, and key parameters. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Use smaller buffers for gRPC Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Clean up tests (#22) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add Connect Disconnect endpoints (#23) * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> * fix list of things in a channel and Add connect disconnect endpoint Signed-off-by: Arvindh <arvindh91@gmail.com> * fix list of things in a channel and Add connect disconnect endpoint Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add: Things share with users (#25) * fix list of things in a channel and Add connect disconnect endpoint Signed-off-by: Arvindh <arvindh91@gmail.com> * add: things share with other users Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Rename gRPC Services (#24) * Rename things and users auth service Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * docs: add authorization docs for gRPC services Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Rename things and users grpc services Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Remove mainflux.env package Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add: Listing of things, channels, groups, users (#26) * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Clean Up Users (#27) * feat(groups): rename redis package to events - Renamed the `redis` package to `events` in the `internal/groups` directory. - Updated the file paths and names accordingly. - This change reflects the more accurate purpose of the package and improves code organization. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(auth): Modify identity method Change request and response of identity method Add accessToken and refreshToken to Token response Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * clean up users, remove dead code Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(users): add unit tests for user service This commit adds unit tests for the user service in the `users` package. The tests cover various scenarios and ensure the correct behavior of the service. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add: List of user groups & removed repeating code in groups (#29) * removed repeating code in list groups Signed-off-by: Arvindh <arvindh91@gmail.com> * add: list of user group Signed-off-by: Arvindh <arvindh91@gmail.com> * fix: otel handler operator name for endpoints Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Clean Up Things Service (#28) * Rework things service Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * add tests Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Clean Up Auth Service (#30) * clean up auth service Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(auth): remove unused import Remove the unused import of `emptypb` in `auth.pb.go`. This import is not being used in the codebase and can be safely removed. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Update API docs (#31) Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Remove TODO comments and cleanup the code Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Update dependenices Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Co-authored-by: b1ackd0t <28790446+rodneyosodo@users.noreply.github.com> Co-authored-by: Arvindh <30824765+arvindh123@users.noreply.github.com>
541 lines
16 KiB
Go
541 lines
16 KiB
Go
package pongo2
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Value struct {
|
|
val reflect.Value
|
|
safe bool // used to indicate whether a Value needs explicit escaping in the template
|
|
}
|
|
|
|
// AsValue converts any given value to a pongo2.Value
|
|
// Usually being used within own functions passed to a template
|
|
// through a Context or within filter functions.
|
|
//
|
|
// Example:
|
|
// AsValue("my string")
|
|
func AsValue(i interface{}) *Value {
|
|
return &Value{
|
|
val: reflect.ValueOf(i),
|
|
}
|
|
}
|
|
|
|
// AsSafeValue works like AsValue, but does not apply the 'escape' filter.
|
|
func AsSafeValue(i interface{}) *Value {
|
|
return &Value{
|
|
val: reflect.ValueOf(i),
|
|
safe: true,
|
|
}
|
|
}
|
|
|
|
func (v *Value) getResolvedValue() reflect.Value {
|
|
if v.val.IsValid() && v.val.Kind() == reflect.Ptr {
|
|
return v.val.Elem()
|
|
}
|
|
return v.val
|
|
}
|
|
|
|
// IsString checks whether the underlying value is a string
|
|
func (v *Value) IsString() bool {
|
|
return v.getResolvedValue().Kind() == reflect.String
|
|
}
|
|
|
|
// IsBool checks whether the underlying value is a bool
|
|
func (v *Value) IsBool() bool {
|
|
return v.getResolvedValue().Kind() == reflect.Bool
|
|
}
|
|
|
|
// IsFloat checks whether the underlying value is a float
|
|
func (v *Value) IsFloat() bool {
|
|
return v.getResolvedValue().Kind() == reflect.Float32 ||
|
|
v.getResolvedValue().Kind() == reflect.Float64
|
|
}
|
|
|
|
// IsInteger checks whether the underlying value is an integer
|
|
func (v *Value) IsInteger() bool {
|
|
return v.getResolvedValue().Kind() == reflect.Int ||
|
|
v.getResolvedValue().Kind() == reflect.Int8 ||
|
|
v.getResolvedValue().Kind() == reflect.Int16 ||
|
|
v.getResolvedValue().Kind() == reflect.Int32 ||
|
|
v.getResolvedValue().Kind() == reflect.Int64 ||
|
|
v.getResolvedValue().Kind() == reflect.Uint ||
|
|
v.getResolvedValue().Kind() == reflect.Uint8 ||
|
|
v.getResolvedValue().Kind() == reflect.Uint16 ||
|
|
v.getResolvedValue().Kind() == reflect.Uint32 ||
|
|
v.getResolvedValue().Kind() == reflect.Uint64
|
|
}
|
|
|
|
// IsNumber checks whether the underlying value is either an integer
|
|
// or a float.
|
|
func (v *Value) IsNumber() bool {
|
|
return v.IsInteger() || v.IsFloat()
|
|
}
|
|
|
|
// IsTime checks whether the underlying value is a time.Time.
|
|
func (v *Value) IsTime() bool {
|
|
_, ok := v.Interface().(time.Time)
|
|
return ok
|
|
}
|
|
|
|
// IsNil checks whether the underlying value is NIL
|
|
func (v *Value) IsNil() bool {
|
|
//fmt.Printf("%+v\n", v.getResolvedValue().Type().String())
|
|
return !v.getResolvedValue().IsValid()
|
|
}
|
|
|
|
// String returns a string for the underlying value. If this value is not
|
|
// of type string, pongo2 tries to convert it. Currently the following
|
|
// types for underlying values are supported:
|
|
//
|
|
// 1. string
|
|
// 2. int/uint (any size)
|
|
// 3. float (any precision)
|
|
// 4. bool
|
|
// 5. time.Time
|
|
// 6. String() will be called on the underlying value if provided
|
|
//
|
|
// NIL values will lead to an empty string. Unsupported types are leading
|
|
// to their respective type name.
|
|
func (v *Value) String() string {
|
|
if v.IsNil() {
|
|
return ""
|
|
}
|
|
|
|
switch v.getResolvedValue().Kind() {
|
|
case reflect.String:
|
|
return v.getResolvedValue().String()
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
return strconv.FormatInt(v.getResolvedValue().Int(), 10)
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
return strconv.FormatUint(v.getResolvedValue().Uint(), 10)
|
|
case reflect.Float32, reflect.Float64:
|
|
return fmt.Sprintf("%f", v.getResolvedValue().Float())
|
|
case reflect.Bool:
|
|
if v.Bool() {
|
|
return "True"
|
|
}
|
|
return "False"
|
|
case reflect.Struct:
|
|
if t, ok := v.Interface().(fmt.Stringer); ok {
|
|
return t.String()
|
|
}
|
|
}
|
|
|
|
logf("Value.String() not implemented for type: %s\n", v.getResolvedValue().Kind().String())
|
|
return v.getResolvedValue().String()
|
|
}
|
|
|
|
// Integer returns the underlying value as an integer (converts the underlying
|
|
// value, if necessary). If it's not possible to convert the underlying value,
|
|
// it will return 0.
|
|
func (v *Value) Integer() int {
|
|
switch v.getResolvedValue().Kind() {
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
return int(v.getResolvedValue().Int())
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
return int(v.getResolvedValue().Uint())
|
|
case reflect.Float32, reflect.Float64:
|
|
return int(v.getResolvedValue().Float())
|
|
case reflect.String:
|
|
// Try to convert from string to int (base 10)
|
|
f, err := strconv.ParseFloat(v.getResolvedValue().String(), 64)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return int(f)
|
|
default:
|
|
logf("Value.Integer() not available for type: %s\n", v.getResolvedValue().Kind().String())
|
|
return 0
|
|
}
|
|
}
|
|
|
|
// Float returns the underlying value as a float (converts the underlying
|
|
// value, if necessary). If it's not possible to convert the underlying value,
|
|
// it will return 0.0.
|
|
func (v *Value) Float() float64 {
|
|
switch v.getResolvedValue().Kind() {
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
return float64(v.getResolvedValue().Int())
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
return float64(v.getResolvedValue().Uint())
|
|
case reflect.Float32, reflect.Float64:
|
|
return v.getResolvedValue().Float()
|
|
case reflect.String:
|
|
// Try to convert from string to float64 (base 10)
|
|
f, err := strconv.ParseFloat(v.getResolvedValue().String(), 64)
|
|
if err != nil {
|
|
return 0.0
|
|
}
|
|
return f
|
|
default:
|
|
logf("Value.Float() not available for type: %s\n", v.getResolvedValue().Kind().String())
|
|
return 0.0
|
|
}
|
|
}
|
|
|
|
// Bool returns the underlying value as bool. If the value is not bool, false
|
|
// will always be returned. If you're looking for true/false-evaluation of the
|
|
// underlying value, have a look on the IsTrue()-function.
|
|
func (v *Value) Bool() bool {
|
|
switch v.getResolvedValue().Kind() {
|
|
case reflect.Bool:
|
|
return v.getResolvedValue().Bool()
|
|
default:
|
|
logf("Value.Bool() not available for type: %s\n", v.getResolvedValue().Kind().String())
|
|
return false
|
|
}
|
|
}
|
|
|
|
// Time returns the underlying value as time.Time.
|
|
// If the underlying value is not a time.Time, it returns the zero value of time.Time.
|
|
func (v *Value) Time() time.Time {
|
|
tm, ok := v.Interface().(time.Time)
|
|
if ok {
|
|
return tm
|
|
}
|
|
return time.Time{}
|
|
}
|
|
|
|
// IsTrue tries to evaluate the underlying value the Pythonic-way:
|
|
//
|
|
// Returns TRUE in one the following cases:
|
|
//
|
|
// * int != 0
|
|
// * uint != 0
|
|
// * float != 0.0
|
|
// * len(array/chan/map/slice/string) > 0
|
|
// * bool == true
|
|
// * underlying value is a struct
|
|
//
|
|
// Otherwise returns always FALSE.
|
|
func (v *Value) IsTrue() bool {
|
|
switch v.getResolvedValue().Kind() {
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
return v.getResolvedValue().Int() != 0
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
return v.getResolvedValue().Uint() != 0
|
|
case reflect.Float32, reflect.Float64:
|
|
return v.getResolvedValue().Float() != 0
|
|
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
|
|
return v.getResolvedValue().Len() > 0
|
|
case reflect.Bool:
|
|
return v.getResolvedValue().Bool()
|
|
case reflect.Struct:
|
|
return true // struct instance is always true
|
|
default:
|
|
logf("Value.IsTrue() not available for type: %s\n", v.getResolvedValue().Kind().String())
|
|
return false
|
|
}
|
|
}
|
|
|
|
// Negate tries to negate the underlying value. It's mainly used for
|
|
// the NOT-operator and in conjunction with a call to
|
|
// return_value.IsTrue() afterwards.
|
|
//
|
|
// Example:
|
|
// AsValue(1).Negate().IsTrue() == false
|
|
func (v *Value) Negate() *Value {
|
|
switch v.getResolvedValue().Kind() {
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
|
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
if v.Integer() != 0 {
|
|
return AsValue(0)
|
|
}
|
|
return AsValue(1)
|
|
case reflect.Float32, reflect.Float64:
|
|
if v.Float() != 0.0 {
|
|
return AsValue(float64(0.0))
|
|
}
|
|
return AsValue(float64(1.1))
|
|
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
|
|
return AsValue(v.getResolvedValue().Len() == 0)
|
|
case reflect.Bool:
|
|
return AsValue(!v.getResolvedValue().Bool())
|
|
case reflect.Struct:
|
|
return AsValue(false)
|
|
default:
|
|
logf("Value.IsTrue() not available for type: %s\n", v.getResolvedValue().Kind().String())
|
|
return AsValue(true)
|
|
}
|
|
}
|
|
|
|
// Len returns the length for an array, chan, map, slice or string.
|
|
// Otherwise it will return 0.
|
|
func (v *Value) Len() int {
|
|
switch v.getResolvedValue().Kind() {
|
|
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
|
return v.getResolvedValue().Len()
|
|
case reflect.String:
|
|
runes := []rune(v.getResolvedValue().String())
|
|
return len(runes)
|
|
default:
|
|
logf("Value.Len() not available for type: %s\n", v.getResolvedValue().Kind().String())
|
|
return 0
|
|
}
|
|
}
|
|
|
|
// Slice slices an array, slice or string. Otherwise it will
|
|
// return an empty []int.
|
|
func (v *Value) Slice(i, j int) *Value {
|
|
switch v.getResolvedValue().Kind() {
|
|
case reflect.Array, reflect.Slice:
|
|
return AsValue(v.getResolvedValue().Slice(i, j).Interface())
|
|
case reflect.String:
|
|
runes := []rune(v.getResolvedValue().String())
|
|
return AsValue(string(runes[i:j]))
|
|
default:
|
|
logf("Value.Slice() not available for type: %s\n", v.getResolvedValue().Kind().String())
|
|
return AsValue([]int{})
|
|
}
|
|
}
|
|
|
|
// Index gets the i-th item of an array, slice or string. Otherwise
|
|
// it will return NIL.
|
|
func (v *Value) Index(i int) *Value {
|
|
switch v.getResolvedValue().Kind() {
|
|
case reflect.Array, reflect.Slice:
|
|
if i >= v.Len() {
|
|
return AsValue(nil)
|
|
}
|
|
return AsValue(v.getResolvedValue().Index(i).Interface())
|
|
case reflect.String:
|
|
//return AsValue(v.getResolvedValue().Slice(i, i+1).Interface())
|
|
s := v.getResolvedValue().String()
|
|
runes := []rune(s)
|
|
if i < len(runes) {
|
|
return AsValue(string(runes[i]))
|
|
}
|
|
return AsValue("")
|
|
default:
|
|
logf("Value.Slice() not available for type: %s\n", v.getResolvedValue().Kind().String())
|
|
return AsValue([]int{})
|
|
}
|
|
}
|
|
|
|
// Contains checks whether the underlying value (which must be of type struct, map,
|
|
// string, array or slice) contains of another Value (e. g. used to check
|
|
// whether a struct contains of a specific field or a map contains a specific key).
|
|
//
|
|
// Example:
|
|
// AsValue("Hello, World!").Contains(AsValue("World")) == true
|
|
func (v *Value) Contains(other *Value) bool {
|
|
switch v.getResolvedValue().Kind() {
|
|
case reflect.Struct:
|
|
fieldValue := v.getResolvedValue().FieldByName(other.String())
|
|
return fieldValue.IsValid()
|
|
case reflect.Map:
|
|
var mapValue reflect.Value
|
|
switch other.Interface().(type) {
|
|
case int:
|
|
mapValue = v.getResolvedValue().MapIndex(other.getResolvedValue())
|
|
case string:
|
|
mapValue = v.getResolvedValue().MapIndex(other.getResolvedValue())
|
|
default:
|
|
logf("Value.Contains() does not support lookup type '%s'\n", other.getResolvedValue().Kind().String())
|
|
return false
|
|
}
|
|
|
|
return mapValue.IsValid()
|
|
case reflect.String:
|
|
return strings.Contains(v.getResolvedValue().String(), other.String())
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
for i := 0; i < v.getResolvedValue().Len(); i++ {
|
|
item := v.getResolvedValue().Index(i)
|
|
if other.EqualValueTo(AsValue(item.Interface())) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
|
|
default:
|
|
logf("Value.Contains() not available for type: %s\n", v.getResolvedValue().Kind().String())
|
|
return false
|
|
}
|
|
}
|
|
|
|
// CanSlice checks whether the underlying value is of type array, slice or string.
|
|
// You normally would use CanSlice() before using the Slice() operation.
|
|
func (v *Value) CanSlice() bool {
|
|
switch v.getResolvedValue().Kind() {
|
|
case reflect.Array, reflect.Slice, reflect.String:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Iterate iterates over a map, array, slice or a string. It calls the
|
|
// function's first argument for every value with the following arguments:
|
|
//
|
|
// idx current 0-index
|
|
// count total amount of items
|
|
// key *Value for the key or item
|
|
// value *Value (only for maps, the respective value for a specific key)
|
|
//
|
|
// If the underlying value has no items or is not one of the types above,
|
|
// the empty function (function's second argument) will be called.
|
|
func (v *Value) Iterate(fn func(idx, count int, key, value *Value) bool, empty func()) {
|
|
v.IterateOrder(fn, empty, false, false)
|
|
}
|
|
|
|
// IterateOrder behaves like Value.Iterate, but can iterate through an array/slice/string in reverse. Does
|
|
// not affect the iteration through a map because maps don't have any particular order.
|
|
// However, you can force an order using the `sorted` keyword (and even use `reversed sorted`).
|
|
func (v *Value) IterateOrder(fn func(idx, count int, key, value *Value) bool, empty func(), reverse bool, sorted bool) {
|
|
switch v.getResolvedValue().Kind() {
|
|
case reflect.Map:
|
|
keys := sortedKeys(v.getResolvedValue().MapKeys())
|
|
if sorted {
|
|
if reverse {
|
|
sort.Sort(sort.Reverse(keys))
|
|
} else {
|
|
sort.Sort(keys)
|
|
}
|
|
}
|
|
keyLen := len(keys)
|
|
for idx, key := range keys {
|
|
value := v.getResolvedValue().MapIndex(key)
|
|
if !fn(idx, keyLen, &Value{val: key}, &Value{val: value}) {
|
|
return
|
|
}
|
|
}
|
|
if keyLen == 0 {
|
|
empty()
|
|
}
|
|
return // done
|
|
case reflect.Array, reflect.Slice:
|
|
var items valuesList
|
|
|
|
itemCount := v.getResolvedValue().Len()
|
|
for i := 0; i < itemCount; i++ {
|
|
items = append(items, &Value{val: v.getResolvedValue().Index(i)})
|
|
}
|
|
|
|
if sorted {
|
|
if reverse {
|
|
sort.Sort(sort.Reverse(items))
|
|
} else {
|
|
sort.Sort(items)
|
|
}
|
|
} else {
|
|
if reverse {
|
|
for i := 0; i < itemCount/2; i++ {
|
|
items[i], items[itemCount-1-i] = items[itemCount-1-i], items[i]
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(items) > 0 {
|
|
for idx, item := range items {
|
|
if !fn(idx, itemCount, item, nil) {
|
|
return
|
|
}
|
|
}
|
|
} else {
|
|
empty()
|
|
}
|
|
return // done
|
|
case reflect.String:
|
|
if sorted {
|
|
// TODO(flosch): Handle sorted
|
|
panic("TODO: handle sort for type string")
|
|
}
|
|
|
|
// TODO(flosch): Not utf8-compatible (utf8-decoding necessary)
|
|
charCount := v.getResolvedValue().Len()
|
|
if charCount > 0 {
|
|
if reverse {
|
|
for i := charCount - 1; i >= 0; i-- {
|
|
if !fn(i, charCount, &Value{val: v.getResolvedValue().Slice(i, i+1)}, nil) {
|
|
return
|
|
}
|
|
}
|
|
} else {
|
|
for i := 0; i < charCount; i++ {
|
|
if !fn(i, charCount, &Value{val: v.getResolvedValue().Slice(i, i+1)}, nil) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
empty()
|
|
}
|
|
return // done
|
|
default:
|
|
logf("Value.Iterate() not available for type: %s\n", v.getResolvedValue().Kind().String())
|
|
}
|
|
empty()
|
|
}
|
|
|
|
// Interface gives you access to the underlying value.
|
|
func (v *Value) Interface() interface{} {
|
|
if v.val.IsValid() {
|
|
return v.val.Interface()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EqualValueTo checks whether two values are containing the same value or object.
|
|
func (v *Value) EqualValueTo(other *Value) bool {
|
|
// comparison of uint with int fails using .Interface()-comparison (see issue #64)
|
|
if v.IsInteger() && other.IsInteger() {
|
|
return v.Integer() == other.Integer()
|
|
}
|
|
if v.IsTime() && other.IsTime() {
|
|
return v.Time().Equal(other.Time())
|
|
}
|
|
return v.Interface() == other.Interface()
|
|
}
|
|
|
|
type sortedKeys []reflect.Value
|
|
|
|
func (sk sortedKeys) Len() int {
|
|
return len(sk)
|
|
}
|
|
|
|
func (sk sortedKeys) Less(i, j int) bool {
|
|
vi := &Value{val: sk[i]}
|
|
vj := &Value{val: sk[j]}
|
|
switch {
|
|
case vi.IsInteger() && vj.IsInteger():
|
|
return vi.Integer() < vj.Integer()
|
|
case vi.IsFloat() && vj.IsFloat():
|
|
return vi.Float() < vj.Float()
|
|
default:
|
|
return vi.String() < vj.String()
|
|
}
|
|
}
|
|
|
|
func (sk sortedKeys) Swap(i, j int) {
|
|
sk[i], sk[j] = sk[j], sk[i]
|
|
}
|
|
|
|
type valuesList []*Value
|
|
|
|
func (vl valuesList) Len() int {
|
|
return len(vl)
|
|
}
|
|
|
|
func (vl valuesList) Less(i, j int) bool {
|
|
vi := vl[i]
|
|
vj := vl[j]
|
|
switch {
|
|
case vi.IsInteger() && vj.IsInteger():
|
|
return vi.Integer() < vj.Integer()
|
|
case vi.IsFloat() && vj.IsFloat():
|
|
return vi.Float() < vj.Float()
|
|
default:
|
|
return vi.String() < vj.String()
|
|
}
|
|
}
|
|
|
|
func (vl valuesList) Swap(i, j int) {
|
|
vl[i], vl[j] = vl[j], vl[i]
|
|
}
|