From fd552fda98900fc665c346d6c941990adbccdc64 Mon Sep 17 00:00:00 2001 From: Carlos Salguero Date: Tue, 27 Aug 2019 10:25:18 -0300 Subject: [PATCH 1/5] PMM-4534 Added TLS field in PostgreSQL tests --- Gopkg.lock | 6 +++--- Gopkg.toml | 2 +- inventory/agents_test.go | 2 ++ .../agents/add_postgres_exporter_responses.go | 12 +++++++++++ .../change_postgres_exporter_responses.go | 6 ++++++ .../json/client/agents/get_agent_responses.go | 6 ++++++ .../client/agents/list_agents_responses.go | 6 ++++++ .../postgre_sql/add_postgre_sql_responses.go | 14 +++++++++++++ .../server/change_settings_responses.go | 20 +++++++++---------- .../client/server/get_settings_responses.go | 12 +++++------ 10 files changed, 66 insertions(+), 20 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index b0a6732c..32db85ae 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -182,8 +182,8 @@ version = "v1.1.2" [[projects]] - branch = "PMM-2.0" - digest = "1:794fda421b5b776a7d6b344f5b530f745b4f87297e44e38a65d80acccebc0d81" + branch = "PMM-4534" + digest = "1:62ecb6a6f7ad53b36fd3267dbc00815ed45ac737ba0bb5e9f3846d107ef6a779" name = "github.com/percona/pmm" packages = [ "api/inventorypb/json/client", @@ -203,7 +203,7 @@ "utils/tlsconfig", ] pruneopts = "NUT" - revision = "2d8fdd46ebc027d15ffc4858fe0ae55b93fdbad0" + revision = "671366ddfbfc95f31d6d53979cdc0107a3e1be61" [[projects]] digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe" diff --git a/Gopkg.toml b/Gopkg.toml index a1394698..375fc744 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -11,4 +11,4 @@ required = [ # TODO switch to tags before PMM 2.0 release [[constraint]] name = "github.com/percona/pmm" - branch = "PMM-2.0" + branch = "PMM-4534" diff --git a/inventory/agents_test.go b/inventory/agents_test.go index 5abd46a1..3bebc664 100644 --- a/inventory/agents_test.go +++ b/inventory/agents_test.go @@ -1248,6 +1248,8 @@ func TestPostgresExporter(t *testing.T) { }, SkipConnectionCheck: true, + TLS: true, + TLSSkipVerify: false, }) agentID := PostgresExporter.PostgresExporter.AgentID defer pmmapitests.RemoveAgents(t, agentID) diff --git a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/add_postgres_exporter_responses.go b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/add_postgres_exporter_responses.go index 6c51eb0f..bad843a2 100644 --- a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/add_postgres_exporter_responses.go +++ b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/add_postgres_exporter_responses.go @@ -139,6 +139,12 @@ type AddPostgresExporterBody struct { // Skip connection check. SkipConnectionCheck bool `json:"skip_connection_check,omitempty"` + // Use TLS for database connections. + TLS bool `json:"tls,omitempty"` + + // Skip TLS certificate and hostname validation. Uses sslmode=required instead of verify-full. + TLSSkipVerify bool `json:"tls_skip_verify,omitempty"` + // PostgreSQL username for scraping metrics. Username string `json:"username,omitempty"` } @@ -293,6 +299,12 @@ type AddPostgresExporterOKBodyPostgresExporter struct { // Enum: [AGENT_STATUS_INVALID STARTING RUNNING WAITING STOPPING DONE] Status *string `json:"status,omitempty"` + // Use TLS for database connections. + TLS bool `json:"tls,omitempty"` + + // Skip TLS certificate and hostname validation. Uses sslmode=required instead of verify-full. + TLSSkipVerify bool `json:"tls_skip_verify,omitempty"` + // PostgreSQL username for scraping metrics. Username string `json:"username,omitempty"` } diff --git a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/change_postgres_exporter_responses.go b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/change_postgres_exporter_responses.go index 1efe89bf..6c9c2c67 100644 --- a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/change_postgres_exporter_responses.go +++ b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/change_postgres_exporter_responses.go @@ -308,6 +308,12 @@ type ChangePostgresExporterOKBodyPostgresExporter struct { // Enum: [AGENT_STATUS_INVALID STARTING RUNNING WAITING STOPPING DONE] Status *string `json:"status,omitempty"` + // Use TLS for database connections. + TLS bool `json:"tls,omitempty"` + + // Skip TLS certificate and hostname validation. Uses sslmode=required instead of verify-full. + TLSSkipVerify bool `json:"tls_skip_verify,omitempty"` + // PostgreSQL username for scraping metrics. Username string `json:"username,omitempty"` } diff --git a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/get_agent_responses.go b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/get_agent_responses.go index ffdc0693..3b35bc2c 100644 --- a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/get_agent_responses.go +++ b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/get_agent_responses.go @@ -989,6 +989,12 @@ type GetAgentOKBodyPostgresExporter struct { // Enum: [AGENT_STATUS_INVALID STARTING RUNNING WAITING STOPPING DONE] Status *string `json:"status,omitempty"` + // Use TLS for database connections. + TLS bool `json:"tls,omitempty"` + + // Skip TLS certificate and hostname validation. Uses sslmode=required instead of verify-full. + TLSSkipVerify bool `json:"tls_skip_verify,omitempty"` + // PostgreSQL username for scraping metrics. Username string `json:"username,omitempty"` } diff --git a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/list_agents_responses.go b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/list_agents_responses.go index 45ee9b1b..f03640aa 100644 --- a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/list_agents_responses.go +++ b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/list_agents_responses.go @@ -1080,6 +1080,12 @@ type PostgresExporterItems0 struct { // Enum: [AGENT_STATUS_INVALID STARTING RUNNING WAITING STOPPING DONE] Status *string `json:"status,omitempty"` + // Use TLS for database connections. + TLS bool `json:"tls,omitempty"` + + // Skip TLS certificate and hostname validation. Uses sslmode=required instead of verify-full. + TLSSkipVerify bool `json:"tls_skip_verify,omitempty"` + // PostgreSQL username for scraping metrics. Username string `json:"username,omitempty"` } diff --git a/vendor/github.com/percona/pmm/api/managementpb/json/client/postgre_sql/add_postgre_sql_responses.go b/vendor/github.com/percona/pmm/api/managementpb/json/client/postgre_sql/add_postgre_sql_responses.go index 7ee955d0..bbc25122 100644 --- a/vendor/github.com/percona/pmm/api/managementpb/json/client/postgre_sql/add_postgre_sql_responses.go +++ b/vendor/github.com/percona/pmm/api/managementpb/json/client/postgre_sql/add_postgre_sql_responses.go @@ -160,6 +160,14 @@ type AddPostgreSQLBody struct { // Skip connection check. SkipConnectionCheck bool `json:"skip_connection_check,omitempty"` + // // Use SSL for PostgreSQL connection + TLS bool `json:"tls,omitempty"` + + // // Skip SSL certificates validation. + // // true : ssl-mode=require + // // false: ssl-mode=verify-full + TLSSkipVerify bool `json:"tls_skip_verify,omitempty"` + // PostgreSQL username for scraping metrics. Username string `json:"username,omitempty"` } @@ -364,6 +372,12 @@ type AddPostgreSQLOKBodyPostgresExporter struct { // Enum: [AGENT_STATUS_INVALID STARTING RUNNING WAITING STOPPING DONE] Status *string `json:"status,omitempty"` + // Use TLS for database connections. + TLS bool `json:"tls,omitempty"` + + // Skip TLS certificate and hostname validation. Uses sslmode=required instead of verify-full. + TLSSkipVerify bool `json:"tls_skip_verify,omitempty"` + // PostgreSQL username for scraping metrics. Username string `json:"username,omitempty"` } diff --git a/vendor/github.com/percona/pmm/api/serverpb/json/client/server/change_settings_responses.go b/vendor/github.com/percona/pmm/api/serverpb/json/client/server/change_settings_responses.go index d61bd9ab..5b75c558 100644 --- a/vendor/github.com/percona/pmm/api/serverpb/json/client/server/change_settings_responses.go +++ b/vendor/github.com/percona/pmm/api/serverpb/json/client/server/change_settings_responses.go @@ -315,8 +315,8 @@ type ChangeSettingsOKBodySettings struct { // qan QAN *ChangeSettingsOKBodySettingsQAN `json:"qan,omitempty"` - // telemetry - Telemetry bool `json:"telemetry,omitempty"` + // telemetry enabled + TelemetryEnabled bool `json:"telemetry_enabled,omitempty"` // updates disabled UpdatesDisabled bool `json:"updates_disabled,omitempty"` @@ -399,13 +399,13 @@ swagger:model ChangeSettingsOKBodySettingsMetricsResolutions */ type ChangeSettingsOKBodySettingsMetricsResolutions struct { - // High resolution. Suffix 's' is required in JSON: 1s, 60s, 300s, etc. + // High resolution. Should have a suffix in JSON: 1s, 1m, 1h. Hr string `json:"hr,omitempty"` - // Low resolution. Suffix 's' is required in JSON: 1s, 60s, 300s, etc. + // Low resolution. Should have a suffix in JSON: 1s, 1m, 1h. Lr string `json:"lr,omitempty"` - // Medium resolution. Suffix 's' is required in JSON: 1s, 60s, 300s, etc. + // Medium resolution. Should have a suffix in JSON: 1s, 1m, 1h. Mr string `json:"mr,omitempty"` } @@ -437,7 +437,7 @@ swagger:model ChangeSettingsOKBodySettingsQAN */ type ChangeSettingsOKBodySettingsQAN struct { - // A number of full days of data retention. Suffix 's' is required in JSON: 86400s, 1209600s, 5184000s, etc. + // A number of full days of data retention. Should have a suffix in JSON: 24h, 720h, etc. DataRetention string `json:"data_retention,omitempty"` } @@ -469,13 +469,13 @@ swagger:model ChangeSettingsParamsBodyMetricsResolutions */ type ChangeSettingsParamsBodyMetricsResolutions struct { - // High resolution. Suffix 's' is required in JSON: 1s, 60s, 300s, etc. + // High resolution. Should have a suffix in JSON: 1s, 1m, 1h. Hr string `json:"hr,omitempty"` - // Low resolution. Suffix 's' is required in JSON: 1s, 60s, 300s, etc. + // Low resolution. Should have a suffix in JSON: 1s, 1m, 1h. Lr string `json:"lr,omitempty"` - // Medium resolution. Suffix 's' is required in JSON: 1s, 60s, 300s, etc. + // Medium resolution. Should have a suffix in JSON: 1s, 1m, 1h. Mr string `json:"mr,omitempty"` } @@ -507,7 +507,7 @@ swagger:model ChangeSettingsParamsBodyQAN */ type ChangeSettingsParamsBodyQAN struct { - // A number of full days of data retention. Suffix 's' is required in JSON: 86400s, 1209600s, 5184000s, etc. + // A number of full days of data retention. Should have a suffix in JSON: 24h, 720h, etc. DataRetention string `json:"data_retention,omitempty"` } diff --git a/vendor/github.com/percona/pmm/api/serverpb/json/client/server/get_settings_responses.go b/vendor/github.com/percona/pmm/api/serverpb/json/client/server/get_settings_responses.go index 9c7ccf5d..0fcd9e4c 100644 --- a/vendor/github.com/percona/pmm/api/serverpb/json/client/server/get_settings_responses.go +++ b/vendor/github.com/percona/pmm/api/serverpb/json/client/server/get_settings_responses.go @@ -225,8 +225,8 @@ type GetSettingsOKBodySettings struct { // qan QAN *GetSettingsOKBodySettingsQAN `json:"qan,omitempty"` - // telemetry - Telemetry bool `json:"telemetry,omitempty"` + // telemetry enabled + TelemetryEnabled bool `json:"telemetry_enabled,omitempty"` // updates disabled UpdatesDisabled bool `json:"updates_disabled,omitempty"` @@ -309,13 +309,13 @@ swagger:model GetSettingsOKBodySettingsMetricsResolutions */ type GetSettingsOKBodySettingsMetricsResolutions struct { - // High resolution. Suffix 's' is required in JSON: 1s, 60s, 300s, etc. + // High resolution. Should have a suffix in JSON: 1s, 1m, 1h. Hr string `json:"hr,omitempty"` - // Low resolution. Suffix 's' is required in JSON: 1s, 60s, 300s, etc. + // Low resolution. Should have a suffix in JSON: 1s, 1m, 1h. Lr string `json:"lr,omitempty"` - // Medium resolution. Suffix 's' is required in JSON: 1s, 60s, 300s, etc. + // Medium resolution. Should have a suffix in JSON: 1s, 1m, 1h. Mr string `json:"mr,omitempty"` } @@ -347,7 +347,7 @@ swagger:model GetSettingsOKBodySettingsQAN */ type GetSettingsOKBodySettingsQAN struct { - // A number of full days of data retention. Suffix 's' is required in JSON: 86400s, 1209600s, 5184000s, etc. + // A number of full days of data retention. Should have a suffix in JSON: 24h, 720h, etc. DataRetention string `json:"data_retention,omitempty"` } From 9bea191e16698a900773c5610eb90afd1067154d Mon Sep 17 00:00:00 2001 From: Carlos Salguero Date: Thu, 29 Aug 2019 08:52:11 -0300 Subject: [PATCH 2/5] PMM-4534 Set pmm branch to pmm-2.0 --- Gopkg.lock | 6 +- Gopkg.toml | 2 +- .../client/my_sql/add_my_sql_responses.go | 159 +++++++++++++++++- 3 files changed, 162 insertions(+), 5 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 32db85ae..905f54ad 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -182,8 +182,8 @@ version = "v1.1.2" [[projects]] - branch = "PMM-4534" - digest = "1:62ecb6a6f7ad53b36fd3267dbc00815ed45ac737ba0bb5e9f3846d107ef6a779" + branch = "PMM-2.0" + digest = "1:f06ca107ca5f8849f57445c7d5f7903f99538d468690c3f50152de9c01a3d410" name = "github.com/percona/pmm" packages = [ "api/inventorypb/json/client", @@ -203,7 +203,7 @@ "utils/tlsconfig", ] pruneopts = "NUT" - revision = "671366ddfbfc95f31d6d53979cdc0107a3e1be61" + revision = "8f73fba53308b05f2898a01bed621f34d439b55a" [[projects]] digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe" diff --git a/Gopkg.toml b/Gopkg.toml index 375fc744..a1394698 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -11,4 +11,4 @@ required = [ # TODO switch to tags before PMM 2.0 release [[constraint]] name = "github.com/percona/pmm" - branch = "PMM-4534" + branch = "PMM-2.0" diff --git a/vendor/github.com/percona/pmm/api/managementpb/json/client/my_sql/add_my_sql_responses.go b/vendor/github.com/percona/pmm/api/managementpb/json/client/my_sql/add_my_sql_responses.go index c7bfe667..64b13abf 100644 --- a/vendor/github.com/percona/pmm/api/managementpb/json/client/my_sql/add_my_sql_responses.go +++ b/vendor/github.com/percona/pmm/api/managementpb/json/client/my_sql/add_my_sql_responses.go @@ -124,6 +124,9 @@ swagger:model AddMySQLBody */ type AddMySQLBody struct { + // add node + AddNode *AddMySQLParamsBodyAddNode `json:"add_node,omitempty"` + // Node and Service access address (DNS name or IP). Required. Address string `json:"address,omitempty"` @@ -136,9 +139,14 @@ type AddMySQLBody struct { // Environment name. Environment string `json:"environment,omitempty"` - // Node identifier on which a service is been running. Required. + // Node identifier on which a service is been running. + // Exactly one of these parameters should be present: node_id, node_name, add_node. NodeID string `json:"node_id,omitempty"` + // Node name on which a service is been running. + // Exactly one of these parameters should be present: node_id, node_name, add_node. + NodeName string `json:"node_name,omitempty"` + // MySQL password for scraping metrics. Password string `json:"password,omitempty"` @@ -169,6 +177,33 @@ type AddMySQLBody struct { // Validate validates this add my SQL body func (o *AddMySQLBody) Validate(formats strfmt.Registry) error { + var res []error + + if err := o.validateAddNode(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +func (o *AddMySQLBody) validateAddNode(formats strfmt.Registry) error { + + if swag.IsZero(o.AddNode) { // not required + return nil + } + + if o.AddNode != nil { + if err := o.AddNode.Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("body" + "." + "add_node") + } + return err + } + } + return nil } @@ -774,3 +809,125 @@ func (o *AddMySQLOKBodyService) UnmarshalBinary(b []byte) error { *o = res return nil } + +/*AddMySQLParamsBodyAddNode AddNodeParams is a params to add new node to inventory while adding new service. +swagger:model AddMySQLParamsBodyAddNode +*/ +type AddMySQLParamsBodyAddNode struct { + + // Node availability zone. + Az string `json:"az,omitempty"` + + // Container identifier. If specified, must be a unique Docker container identifier. + ContainerID string `json:"container_id,omitempty"` + + // Container name. + ContainerName string `json:"container_name,omitempty"` + + // Custom user-assigned labels. + CustomLabels map[string]string `json:"custom_labels,omitempty"` + + // Linux distribution name and version. + Distro string `json:"distro,omitempty"` + + // Linux machine-id. + // Must be unique across all Generic Nodes if specified. + MachineID string `json:"machine_id,omitempty"` + + // Node model. + NodeModel string `json:"node_model,omitempty"` + + // Unique across all Nodes user-defined name. Can't be changed. + NodeName string `json:"node_name,omitempty"` + + // NodeType describes supported Node types. + // Enum: [NODE_TYPE_INVALID GENERIC_NODE CONTAINER_NODE REMOTE_NODE REMOTE_AMAZON_RDS_NODE] + NodeType *string `json:"node_type,omitempty"` + + // Node region. + Region string `json:"region,omitempty"` +} + +// Validate validates this add my SQL params body add node +func (o *AddMySQLParamsBodyAddNode) Validate(formats strfmt.Registry) error { + var res []error + + if err := o.validateNodeType(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +var addMySqlParamsBodyAddNodeTypeNodeTypePropEnum []interface{} + +func init() { + var res []string + if err := json.Unmarshal([]byte(`["NODE_TYPE_INVALID","GENERIC_NODE","CONTAINER_NODE","REMOTE_NODE","REMOTE_AMAZON_RDS_NODE"]`), &res); err != nil { + panic(err) + } + for _, v := range res { + addMySqlParamsBodyAddNodeTypeNodeTypePropEnum = append(addMySqlParamsBodyAddNodeTypeNodeTypePropEnum, v) + } +} + +const ( + + // AddMySQLParamsBodyAddNodeNodeTypeNODETYPEINVALID captures enum value "NODE_TYPE_INVALID" + AddMySQLParamsBodyAddNodeNodeTypeNODETYPEINVALID string = "NODE_TYPE_INVALID" + + // AddMySQLParamsBodyAddNodeNodeTypeGENERICNODE captures enum value "GENERIC_NODE" + AddMySQLParamsBodyAddNodeNodeTypeGENERICNODE string = "GENERIC_NODE" + + // AddMySQLParamsBodyAddNodeNodeTypeCONTAINERNODE captures enum value "CONTAINER_NODE" + AddMySQLParamsBodyAddNodeNodeTypeCONTAINERNODE string = "CONTAINER_NODE" + + // AddMySQLParamsBodyAddNodeNodeTypeREMOTENODE captures enum value "REMOTE_NODE" + AddMySQLParamsBodyAddNodeNodeTypeREMOTENODE string = "REMOTE_NODE" + + // AddMySQLParamsBodyAddNodeNodeTypeREMOTEAMAZONRDSNODE captures enum value "REMOTE_AMAZON_RDS_NODE" + AddMySQLParamsBodyAddNodeNodeTypeREMOTEAMAZONRDSNODE string = "REMOTE_AMAZON_RDS_NODE" +) + +// prop value enum +func (o *AddMySQLParamsBodyAddNode) validateNodeTypeEnum(path, location string, value string) error { + if err := validate.Enum(path, location, value, addMySqlParamsBodyAddNodeTypeNodeTypePropEnum); err != nil { + return err + } + return nil +} + +func (o *AddMySQLParamsBodyAddNode) validateNodeType(formats strfmt.Registry) error { + + if swag.IsZero(o.NodeType) { // not required + return nil + } + + // value enum + if err := o.validateNodeTypeEnum("body"+"."+"add_node"+"."+"node_type", "body", *o.NodeType); err != nil { + return err + } + + return nil +} + +// MarshalBinary interface implementation +func (o *AddMySQLParamsBodyAddNode) MarshalBinary() ([]byte, error) { + if o == nil { + return nil, nil + } + return swag.WriteJSON(o) +} + +// UnmarshalBinary interface implementation +func (o *AddMySQLParamsBodyAddNode) UnmarshalBinary(b []byte) error { + var res AddMySQLParamsBodyAddNode + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *o = res + return nil +} From 428a5a0c65ca29ba5c54c92ed380e9a3af396f01 Mon Sep 17 00:00:00 2001 From: Carlos Salguero Date: Mon, 2 Sep 2019 14:02:18 -0300 Subject: [PATCH 3/5] PMM-4534 Added MongoDB explain action --- Gopkg.lock | 6 +- Gopkg.toml | 2 +- management/mongodb_test.go | 138 ++++++++++++++++++ .../json/client/actions/actions_client.go | 33 +++++ 4 files changed, 175 insertions(+), 4 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 0404f79d..a49deb38 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -182,8 +182,8 @@ version = "v1.1.2" [[projects]] - branch = "PMM-2.0" - digest = "1:c45b356b12434745618c0827f76173dcc9fef3eaffc23839d489e196bb77b34e" + branch = "PMM-4192" + digest = "1:26e1fb9c178c61497ce73731f6ec7fc402af1ff35014f25aeea4b05717ecb078" name = "github.com/percona/pmm" packages = [ "api/inventorypb/json/client", @@ -203,7 +203,7 @@ "utils/tlsconfig", ] pruneopts = "NUT" - revision = "093b70a3828bcabe57a0b024d3120fc8b04643e8" + revision = "c0493de4e3b46931b855f7eafcf874b08ff20a94" [[projects]] digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe" diff --git a/Gopkg.toml b/Gopkg.toml index a1394698..36c06292 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -11,4 +11,4 @@ required = [ # TODO switch to tags before PMM 2.0 release [[constraint]] name = "github.com/percona/pmm" - branch = "PMM-2.0" + branch = "PMM-4192" diff --git a/management/mongodb_test.go b/management/mongodb_test.go index e4273f71..82b3ea08 100644 --- a/management/mongodb_test.go +++ b/management/mongodb_test.go @@ -1,23 +1,161 @@ package management import ( + "fmt" "testing" + "time" "github.com/AlekSi/pointer" + "github.com/percona/percona-toolkit/src/go/mongolib/proto" inventoryClient "github.com/percona/pmm/api/inventorypb/json/client" "github.com/percona/pmm/api/inventorypb/json/client/agents" "github.com/percona/pmm/api/inventorypb/json/client/services" "github.com/percona/pmm/api/managementpb/json/client" + "github.com/percona/pmm/api/managementpb/json/client/actions" mongodb "github.com/percona/pmm/api/managementpb/json/client/mongo_db" "github.com/percona/pmm/api/managementpb/json/client/node" "github.com/percona/pmm/api/managementpb/json/client/service" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.mongodb.org/mongo-driver/bson" "google.golang.org/grpc/codes" pmmapitests "github.com/Percona-Lab/pmm-api-tests" ) +func TestMongoDBExplain(t *testing.T) { + t.Run("MongoDB explain", func(t *testing.T) { + nodeName := pmmapitests.TestString(t, "node-name-for-all-fields") + nodeID, pmmAgentID := registerGenericNode(t, node.RegisterBody{ + NodeName: nodeName, + NodeType: pointer.ToString(node.RegisterBodyNodeTypeGENERICNODE), + }) + defer pmmapitests.RemoveNodes(t, nodeID) + defer removePMMAgentWithSubAgents(t, pmmAgentID) + + serviceName := pmmapitests.TestString(t, "service-name-for-all-fields") + + params := &mongodb.AddMongoDBParams{ + Context: pmmapitests.Context, + Body: mongodb.AddMongoDBBody{ + NodeID: nodeID, + PMMAgentID: pmmAgentID, + ServiceName: serviceName, + Address: "10.10.10.10", + Port: 27017, + Username: "username", + Password: "password", + QANMongodbProfiler: true, + + SkipConnectionCheck: true, + }, + } + addMongoDBOK, err := client.Default.MongoDB.AddMongoDB(params) + require.NoError(t, err) + require.NotNil(t, addMongoDBOK) + require.NotNil(t, addMongoDBOK.Payload.Service) + serviceID := addMongoDBOK.Payload.Service.ServiceID + fmt.Printf("ServiceID: %s\n", serviceID) + defer pmmapitests.RemoveServices(t, serviceID) + + // Check that service is created and its fields. + serviceOK, err := inventoryClient.Default.Services.GetService(&services.GetServiceParams{ + Body: services.GetServiceBody{ + ServiceID: serviceID, + }, + Context: pmmapitests.Context, + }) + assert.NoError(t, err) + assert.NotNil(t, serviceOK) + assert.Equal(t, services.GetServiceOKBody{ + Mongodb: &services.GetServiceOKBodyMongodb{ + ServiceID: serviceID, + NodeID: nodeID, + ServiceName: serviceName, + Address: "10.10.10.10", + Port: 27017, + }, + }, *serviceOK.Payload) + + // -------------------------------------------------------------------------------------- + eq := proto.ExampleQuery{ + Ns: "test.coll", + Op: "query", + Query: proto.BsonD{ + { + Key: "k", + Value: proto.BsonD{ + { + Key: "$lte", + Value: int32(1), + }, + }, + }, + }, + Command: nil, + OriginatingCommand: nil, + UpdateObj: nil, + } + buf, _ := bson.MarshalExtJSON(eq, true, true) + explainActionOK, err := client.Default.Actions.StartMongoDBExplainAction(&actions.StartMongoDBExplainActionParams{ + Context: pmmapitests.Context, + Body: actions.StartMongoDBExplainActionBody{ + //PMMAgentID: "/agent_id/f235005b-9cca-4b73-bbbd-1251067c3138", + ServiceID: serviceID, + Database: "test", + Query: string(buf), + }, + }) + require.NoError(t, err) + require.NotEmpty(t, explainActionOK.Payload.ActionID) + + time.Sleep(2 * time.Second) + + actionOK, err := client.Default.Actions.GetAction(&actions.GetActionParams{ + Context: pmmapitests.Context, + Body: actions.GetActionBody{ + ActionID: explainActionOK.Payload.ActionID, + }, + }) + require.NoError(t, err) + require.Empty(t, actionOK.Payload.Error) + // -------------------------------------------------------------------------------------- + // Check that exporters are added. + listAgents, err := inventoryClient.Default.Agents.ListAgents(&agents.ListAgentsParams{ + Context: pmmapitests.Context, + Body: agents.ListAgentsBody{ + ServiceID: serviceID, + }, + }) + assert.NoError(t, err) + require.NotNil(t, listAgents) + defer removeAllAgentsInList(t, listAgents) + + require.Len(t, listAgents.Payload.MongodbExporter, 1) + require.Len(t, listAgents.Payload.QANMongodbProfilerAgent, 1) + assert.Equal(t, agents.ListAgentsOKBody{ + MongodbExporter: []*agents.MongodbExporterItems0{ + { + AgentID: listAgents.Payload.MongodbExporter[0].AgentID, + ServiceID: serviceID, + PMMAgentID: pmmAgentID, + Username: "username", + Password: "password", + }, + }, + QANMongodbProfilerAgent: []*agents.QANMongodbProfilerAgentItems0{ + { + AgentID: listAgents.Payload.QANMongodbProfilerAgent[0].AgentID, + ServiceID: serviceID, + PMMAgentID: pmmAgentID, + Username: "username", + Password: "password", + }, + }, + }, *listAgents.Payload) + }) +} + func TestAddMongoDB(t *testing.T) { t.Run("Basic", func(t *testing.T) { nodeName := pmmapitests.TestString(t, "node-for-basic-name") diff --git a/vendor/github.com/percona/pmm/api/managementpb/json/client/actions/actions_client.go b/vendor/github.com/percona/pmm/api/managementpb/json/client/actions/actions_client.go index c27fe256..593be2f1 100644 --- a/vendor/github.com/percona/pmm/api/managementpb/json/client/actions/actions_client.go +++ b/vendor/github.com/percona/pmm/api/managementpb/json/client/actions/actions_client.go @@ -90,6 +90,39 @@ func (a *Client) GetAction(params *GetActionParams) (*GetActionOK, error) { return nil, runtime.NewAPIError("unexpected success response: content available as default response in error", unexpectedSuccess, unexpectedSuccess.Code()) } +/* +StartMongoDBExplainAction starts mongo DB explain action starts postgre SQL e x p l a i n action +*/ +func (a *Client) StartMongoDBExplainAction(params *StartMongoDBExplainActionParams) (*StartMongoDBExplainActionOK, error) { + // TODO: Validate the params before sending + if params == nil { + params = NewStartMongoDBExplainActionParams() + } + + result, err := a.transport.Submit(&runtime.ClientOperation{ + ID: "StartMongoDBExplainAction", + Method: "POST", + PathPattern: "/v0/management/Actions/StartMongoDBExplain", + ProducesMediaTypes: []string{"application/json"}, + ConsumesMediaTypes: []string{"application/json"}, + Schemes: []string{"http", "https"}, + Params: params, + Reader: &StartMongoDBExplainActionReader{formats: a.formats}, + Context: params.Context, + Client: params.HTTPClient, + }) + if err != nil { + return nil, err + } + success, ok := result.(*StartMongoDBExplainActionOK) + if ok { + return success, nil + } + // unexpected success response + unexpectedSuccess := result.(*StartMongoDBExplainActionDefault) + return nil, runtime.NewAPIError("unexpected success response: content available as default response in error", unexpectedSuccess, unexpectedSuccess.Code()) +} + /* StartMySQLExplainAction starts my SQL explain action starts my SQL e x p l a i n action with traditional output */ From 635ee0581cbbca93f3558bb3f8bf4d36c58029b0 Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Tue, 10 Sep 2019 23:40:26 +0300 Subject: [PATCH 4/5] PMM-4534 Update API. --- Gopkg.lock | 27 +- Gopkg.toml | 2 +- .../percona/percona-toolkit/COPYING | 339 ++++ .../percona-toolkit/config/deb/copyright | 35 + .../sandbox/servers/5.6/sys/COPYING | 339 ++++ .../sandbox/servers/5.6/sys/LICENSE | 14 + .../sandbox/servers/5.7/sys/COPYING | 339 ++++ .../sandbox/servers/5.7/sys/LICENSE | 14 + .../src/go/mongolib/proto/asserts.go | 9 + .../go/mongolib/proto/backgroundflushing.go | 9 + .../src/go/mongolib/proto/balancer_stats.go | 9 + .../src/go/mongolib/proto/bson.go | 167 ++ .../src/go/mongolib/proto/buildinfo.go | 13 + .../src/go/mongolib/proto/chunks_count.go | 6 + .../src/go/mongolib/proto/cmdlineopts.go | 105 + .../src/go/mongolib/proto/collstats.go | 46 + .../src/go/mongolib/proto/connections.go | 7 + .../src/go/mongolib/proto/currentOp.go | 68 + .../src/go/mongolib/proto/cursors.go | 10 + .../src/go/mongolib/proto/databases.go | 13 + .../src/go/mongolib/proto/durability.go | 21 + .../src/go/mongolib/proto/extrainfo.go | 7 + .../src/go/mongolib/proto/get_shard_map.go | 6 + .../src/go/mongolib/proto/globallock.go | 19 + .../src/go/mongolib/proto/hostinfo.go | 39 + .../src/go/mongolib/proto/locks.go | 7 + .../src/go/mongolib/proto/master_doc.go | 7 + .../src/go/mongolib/proto/memory.go | 10 + .../src/go/mongolib/proto/metrics.go | 85 + .../src/go/mongolib/proto/network.go | 7 + .../src/go/mongolib/proto/opcounters.go | 10 + .../src/go/mongolib/proto/oplog.go | 76 + .../src/go/mongolib/proto/profiler_status.go | 13 + .../src/go/mongolib/proto/replconfig.go | 50 + .../src/go/mongolib/proto/replicas.go | 13 + .../src/go/mongolib/proto/replstatus.go | 48 + .../src/go/mongolib/proto/server_status.go | 196 ++ .../proto/sharding_changelog_stats.go | 15 + .../src/go/mongolib/proto/shards.go | 11 + .../src/go/mongolib/proto/system.profile.go | 286 +++ .../percona-toolkit/t/lib/bash/alt_cmds.t | 1 + .../percona-toolkit/t/lib/bash/collect.t | 1 + .../t/lib/bash/collect_mysql_info.t | 1 + .../t/lib/bash/collect_system_info.t | 1 + .../percona-toolkit/t/lib/bash/daemon.t | 1 + .../percona-toolkit/t/lib/bash/log_warn_die.t | 1 + .../t/lib/bash/mysql_options.t | 1 + .../t/lib/bash/parse_options.t | 1 + .../t/lib/bash/report_formatting.t | 1 + .../t/lib/bash/report_mysql_info.t | 1 + .../t/lib/bash/report_system_info.t | 1 + .../percona-toolkit/t/lib/bash/safeguards.t | 1 + .../t/lib/bash/summary_common.t | 1 + .../percona-toolkit/t/lib/bash/tmpdir.t | 1 + .../util/NaturalDocs/License.txt | 275 +++ ..._qan_my_sql_perf_schema_agent_responses.go | 3 + .../add_qan_my_sql_slowlog_agent_responses.go | 13 + ..._qan_my_sql_perf_schema_agent_responses.go | 3 + ...ange_qan_my_sql_slowlog_agent_responses.go | 6 + .../json/client/agents/get_agent_responses.go | 9 + .../client/agents/list_agents_responses.go | 9 + .../json/client/actions/actions_client.go | 33 - .../client/mongo_db/add_mongo_db_responses.go | 159 +- .../client/my_sql/add_my_sql_responses.go | 16 + .../postgre_sql/add_postgre_sql_responses.go | 159 +- .../proxy_sql/add_proxy_sql_responses.go | 159 +- vendor/gopkg.in/mgo.v2/LICENSE | 25 + vendor/gopkg.in/mgo.v2/bson/LICENSE | 25 + vendor/gopkg.in/mgo.v2/bson/bson.go | 734 +++++++ vendor/gopkg.in/mgo.v2/bson/decimal.go | 310 +++ vendor/gopkg.in/mgo.v2/bson/decode.go | 849 +++++++++ vendor/gopkg.in/mgo.v2/bson/encode.go | 514 +++++ vendor/gopkg.in/mgo.v2/bson/json.go | 380 ++++ vendor/gopkg.in/mgo.v2/internal/json/LICENSE | 27 + .../gopkg.in/mgo.v2/internal/json/decode.go | 1685 +++++++++++++++++ .../gopkg.in/mgo.v2/internal/json/encode.go | 1256 ++++++++++++ .../mgo.v2/internal/json/extension.go | 95 + vendor/gopkg.in/mgo.v2/internal/json/fold.go | 143 ++ .../gopkg.in/mgo.v2/internal/json/indent.go | 141 ++ .../gopkg.in/mgo.v2/internal/json/scanner.go | 697 +++++++ .../gopkg.in/mgo.v2/internal/json/stream.go | 510 +++++ vendor/gopkg.in/mgo.v2/internal/json/tags.go | 44 + 82 files changed, 10750 insertions(+), 40 deletions(-) create mode 100644 vendor/github.com/percona/percona-toolkit/COPYING create mode 100644 vendor/github.com/percona/percona-toolkit/config/deb/copyright create mode 100644 vendor/github.com/percona/percona-toolkit/sandbox/servers/5.6/sys/COPYING create mode 100644 vendor/github.com/percona/percona-toolkit/sandbox/servers/5.6/sys/LICENSE create mode 100644 vendor/github.com/percona/percona-toolkit/sandbox/servers/5.7/sys/COPYING create mode 100644 vendor/github.com/percona/percona-toolkit/sandbox/servers/5.7/sys/LICENSE create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/asserts.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/backgroundflushing.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/balancer_stats.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/bson.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/buildinfo.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/chunks_count.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/cmdlineopts.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/collstats.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/connections.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/currentOp.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/cursors.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/databases.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/durability.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/extrainfo.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/get_shard_map.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/globallock.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/hostinfo.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/locks.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/master_doc.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/memory.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/metrics.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/network.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/opcounters.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/oplog.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/profiler_status.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/replconfig.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/replicas.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/replstatus.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/server_status.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/sharding_changelog_stats.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/shards.go create mode 100644 vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/system.profile.go create mode 120000 vendor/github.com/percona/percona-toolkit/t/lib/bash/alt_cmds.t create mode 120000 vendor/github.com/percona/percona-toolkit/t/lib/bash/collect.t create mode 120000 vendor/github.com/percona/percona-toolkit/t/lib/bash/collect_mysql_info.t create mode 120000 vendor/github.com/percona/percona-toolkit/t/lib/bash/collect_system_info.t create mode 120000 vendor/github.com/percona/percona-toolkit/t/lib/bash/daemon.t create mode 120000 vendor/github.com/percona/percona-toolkit/t/lib/bash/log_warn_die.t create mode 120000 vendor/github.com/percona/percona-toolkit/t/lib/bash/mysql_options.t create mode 120000 vendor/github.com/percona/percona-toolkit/t/lib/bash/parse_options.t create mode 120000 vendor/github.com/percona/percona-toolkit/t/lib/bash/report_formatting.t create mode 120000 vendor/github.com/percona/percona-toolkit/t/lib/bash/report_mysql_info.t create mode 120000 vendor/github.com/percona/percona-toolkit/t/lib/bash/report_system_info.t create mode 120000 vendor/github.com/percona/percona-toolkit/t/lib/bash/safeguards.t create mode 120000 vendor/github.com/percona/percona-toolkit/t/lib/bash/summary_common.t create mode 120000 vendor/github.com/percona/percona-toolkit/t/lib/bash/tmpdir.t create mode 100644 vendor/github.com/percona/percona-toolkit/util/NaturalDocs/License.txt create mode 100644 vendor/gopkg.in/mgo.v2/LICENSE create mode 100644 vendor/gopkg.in/mgo.v2/bson/LICENSE create mode 100644 vendor/gopkg.in/mgo.v2/bson/bson.go create mode 100644 vendor/gopkg.in/mgo.v2/bson/decimal.go create mode 100644 vendor/gopkg.in/mgo.v2/bson/decode.go create mode 100644 vendor/gopkg.in/mgo.v2/bson/encode.go create mode 100644 vendor/gopkg.in/mgo.v2/bson/json.go create mode 100644 vendor/gopkg.in/mgo.v2/internal/json/LICENSE create mode 100644 vendor/gopkg.in/mgo.v2/internal/json/decode.go create mode 100644 vendor/gopkg.in/mgo.v2/internal/json/encode.go create mode 100644 vendor/gopkg.in/mgo.v2/internal/json/extension.go create mode 100644 vendor/gopkg.in/mgo.v2/internal/json/fold.go create mode 100644 vendor/gopkg.in/mgo.v2/internal/json/indent.go create mode 100644 vendor/gopkg.in/mgo.v2/internal/json/scanner.go create mode 100644 vendor/gopkg.in/mgo.v2/internal/json/stream.go create mode 100644 vendor/gopkg.in/mgo.v2/internal/json/tags.go diff --git a/Gopkg.lock b/Gopkg.lock index a49deb38..b46b6124 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -182,8 +182,16 @@ version = "v1.1.2" [[projects]] - branch = "PMM-4192" - digest = "1:26e1fb9c178c61497ce73731f6ec7fc402af1ff35014f25aeea4b05717ecb078" + digest = "1:6f436920f12a308c3727451524e6734cc9ffb20d4ae49c5064d12113189787c8" + name = "github.com/percona/percona-toolkit" + packages = ["src/go/mongolib/proto"] + pruneopts = "NUT" + revision = "e85ce15ef24bc4614b4d2f13792fa73583d68f8e" + version = "3.0.13" + +[[projects]] + branch = "PMM-2.0" + digest = "1:e665843242ab7329426030d94eb9aef41fd79b89922ab7a17594173dd6825ff2" name = "github.com/percona/pmm" packages = [ "api/inventorypb/json/client", @@ -203,7 +211,7 @@ "utils/tlsconfig", ] pruneopts = "NUT" - revision = "c0493de4e3b46931b855f7eafcf874b08ff20a94" + revision = "f8fed450fcb387e906b21fa1bf14a63366250204" [[projects]] digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe" @@ -297,6 +305,17 @@ revision = "6eaf6f47437a6b4e2153a190160ef39a92c7eceb" version = "v1.23.0" +[[projects]] + branch = "v2" + digest = "1:2642fd0b6900c77247d61d80cf2eb59a374ef4ffc2d25a1b95b87dc355b2894e" + name = "gopkg.in/mgo.v2" + packages = [ + "bson", + "internal/json", + ] + pruneopts = "NUT" + revision = "a6b53ec6cb22a3699387a57a161566f9402ee85b" + [[projects]] digest = "1:18108594151654e9e696b27b181b953f9a90b16bf14d253dd1b397b025a1487f" name = "gopkg.in/yaml.v2" @@ -313,6 +332,7 @@ "github.com/go-openapi/runtime", "github.com/go-openapi/runtime/client", "github.com/jstemmer/go-junit-report", + "github.com/percona/percona-toolkit/src/go/mongolib/proto", "github.com/percona/pmm/api/inventorypb/json/client", "github.com/percona/pmm/api/inventorypb/json/client/agents", "github.com/percona/pmm/api/inventorypb/json/client/nodes", @@ -331,6 +351,7 @@ "github.com/sirupsen/logrus", "github.com/stretchr/testify/assert", "github.com/stretchr/testify/require", + "go.mongodb.org/mongo-driver/bson", "golang.org/x/sys/unix", "google.golang.org/grpc/codes", ] diff --git a/Gopkg.toml b/Gopkg.toml index 36c06292..a1394698 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -11,4 +11,4 @@ required = [ # TODO switch to tags before PMM 2.0 release [[constraint]] name = "github.com/percona/pmm" - branch = "PMM-4192" + branch = "PMM-2.0" diff --git a/vendor/github.com/percona/percona-toolkit/COPYING b/vendor/github.com/percona/percona-toolkit/COPYING new file mode 100644 index 00000000..d159169d --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/vendor/github.com/percona/percona-toolkit/config/deb/copyright b/vendor/github.com/percona/percona-toolkit/config/deb/copyright new file mode 100644 index 00000000..362a0523 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/config/deb/copyright @@ -0,0 +1,35 @@ +This package was debianized by Percona Toolkit Developers + on Sun, 10 Jun 2007 22:30:36 -0500 + +It was downloaded from http://www.percona.com/downloads/ + +Upstream Author: + + Percona Toolkit Developers + +Copyright: + + Copyright 2013 Percona Ireland Ltd. + +License: + + This software is dual licensed, either GPL version 2 or Artistic License. + + This package is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, version 2; OR the Perl Artistic License. + + This package is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this package; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +On Debian systems, the complete text of the GNU General Public License version +2 can be found in `/usr/share/common-licenses/GPL-2'. + +On Debian systems, the complete text of the Artistic License can be found in +`/usr/share/common-licenses/Artistic'. diff --git a/vendor/github.com/percona/percona-toolkit/sandbox/servers/5.6/sys/COPYING b/vendor/github.com/percona/percona-toolkit/sandbox/servers/5.6/sys/COPYING new file mode 100644 index 00000000..d511905c --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/sandbox/servers/5.6/sys/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/vendor/github.com/percona/percona-toolkit/sandbox/servers/5.6/sys/LICENSE b/vendor/github.com/percona/percona-toolkit/sandbox/servers/5.6/sys/LICENSE new file mode 100644 index 00000000..8222fa6d --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/sandbox/servers/5.6/sys/LICENSE @@ -0,0 +1,14 @@ +Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; version 2 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA diff --git a/vendor/github.com/percona/percona-toolkit/sandbox/servers/5.7/sys/COPYING b/vendor/github.com/percona/percona-toolkit/sandbox/servers/5.7/sys/COPYING new file mode 100644 index 00000000..d511905c --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/sandbox/servers/5.7/sys/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/vendor/github.com/percona/percona-toolkit/sandbox/servers/5.7/sys/LICENSE b/vendor/github.com/percona/percona-toolkit/sandbox/servers/5.7/sys/LICENSE new file mode 100644 index 00000000..8222fa6d --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/sandbox/servers/5.7/sys/LICENSE @@ -0,0 +1,14 @@ +Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; version 2 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/asserts.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/asserts.go new file mode 100644 index 00000000..353c040f --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/asserts.go @@ -0,0 +1,9 @@ +package proto + +type Asserts struct { + User float64 `bson:"user"` + Warning float64 `bson:"warning"` + Msg float64 `bson:"msg"` + Regular float64 `bson:"regular"` + Rollovers float64 `bson:"rollovers"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/backgroundflushing.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/backgroundflushing.go new file mode 100644 index 00000000..a75219af --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/backgroundflushing.go @@ -0,0 +1,9 @@ +package proto + +type BackgroundFlushing struct { + AverageMs float64 `bson:"average_ms"` + Flushes float64 `bson:"flushes"` + LastFinished string `bson:"last_finished"` + LastMs float64 `bson:"last_ms"` + TotalMs float64 `bson:"total_ms"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/balancer_stats.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/balancer_stats.go new file mode 100644 index 00000000..14e76154 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/balancer_stats.go @@ -0,0 +1,9 @@ +package proto + +type BalancerStats struct { + Success int64 + Failed int64 + Splits int64 + Drops int64 + Settings map[string]interface{} +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/bson.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/bson.go new file mode 100644 index 00000000..b8664a72 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/bson.go @@ -0,0 +1,167 @@ +package proto + +import ( + "bytes" + "encoding/json" + "fmt" + "math" + + "gopkg.in/mgo.v2/bson" +) + +type BsonD bson.D + +func (d *BsonD) UnmarshalJSON(data []byte) error { + dec := json.NewDecoder(bytes.NewReader(data)) + + t, err := dec.Token() + if err != nil { + return err + } + if t != json.Delim('{') { + return fmt.Errorf("expected { but got %s", t) + } + for { + t, err := dec.Token() + if err != nil { + return err + } + + // Might be empty object + if t == json.Delim('}') { + return nil + } + + key, ok := t.(string) + if !ok { + return fmt.Errorf("expected key to be a string but got %s", t) + } + + de := bson.DocElem{} + de.Name = key + + if !dec.More() { + return fmt.Errorf("missing value for key %s", key) + } + + var raw json.RawMessage + err = dec.Decode(&raw) + if err != nil { + return err + } + + var v BsonD + err = bson.UnmarshalJSON(raw, &v) + if err != nil { + var v []BsonD + err = bson.UnmarshalJSON(raw, &v) + if err != nil { + var v interface{} + err = bson.UnmarshalJSON(raw, &v) + if err != nil { + return err + } else { + de.Value = v + } + } else { + de.Value = v + } + } else { + de.Value = v + } + + *d = append(*d, de) + if !dec.More() { + break + } + } + + t, err = dec.Token() + if err != nil { + return err + } + if t != json.Delim('}') { + return fmt.Errorf("expect delimeter %s but got %s", json.Delim('}'), t) + } + + return nil +} + +func (d BsonD) MarshalJSON() ([]byte, error) { + var b bytes.Buffer + + b.WriteByte('{') + + for i, v := range d { + if i > 0 { + b.WriteByte(',') + } + + // marshal key + key, err := bson.MarshalJSON(v.Name) + if err != nil { + return nil, err + } + b.Write(key) + b.WriteByte(':') + + var val []byte + if value, ok := v.Value.(float64); ok && math.IsInf(value, 0) { + if math.IsInf(value, 1) { + val = []byte("Infinity") + } else { + val = []byte("-Infinity") + } + + // below is wrong, but I'm later unable to Unmarshal Infinity, + // so we turn it into string for now + val = append([]byte(`"`), val...) + val = append(val, '"') + } else { + // marshal value + val, err = bson.MarshalJSON(v.Value) + if err != nil { + return nil, err + } + } + b.Write(val) + } + + b.WriteByte('}') + + return b.Bytes(), nil +} + +func (d BsonD) Len() int { + return len(d) +} + +// Map returns a map out of the ordered element name/value pairs in d. +func (d BsonD) Map() (m bson.M) { + m = make(bson.M, len(d)) + for _, item := range d { + switch v := item.Value.(type) { + case BsonD: + m[item.Name] = v.Map() + case []BsonD: + el := []bson.M{} + for i := range v { + el = append(el, v[i].Map()) + } + m[item.Name] = el + case []interface{}: + // mgo/bson doesn't expose UnmarshalBSON interface + // so we can't create custom bson.Unmarshal() + el := []bson.M{} + for i := range v { + if b, ok := v[i].(BsonD); ok { + el = append(el, b.Map()) + } + } + m[item.Name] = el + default: + m[item.Name] = item.Value + } + } + return m +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/buildinfo.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/buildinfo.go new file mode 100644 index 00000000..db4b4240 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/buildinfo.go @@ -0,0 +1,13 @@ +package proto + +// BuildInfo Struct to store results of calling session.BuildInfo() +type BuildInfo struct { + Version string + VersionArray []int32 + GitVersion string + OpenSSLVersion string + SysInfo string + Bits int32 + Debug bool + MaxObjectSize int64 +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/chunks_count.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/chunks_count.go new file mode 100644 index 00000000..795d5c9c --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/chunks_count.go @@ -0,0 +1,6 @@ +package proto + +type ChunksByCollection struct { + ID string `bson:"_id"` // Namespace + Count int `bson:"count"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/cmdlineopts.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/cmdlineopts.go new file mode 100644 index 00000000..802a3042 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/cmdlineopts.go @@ -0,0 +1,105 @@ +package proto + +type ProcessManagement struct { + Fork bool `bson:"fork"` +} + +type Replication struct { + ReplSet string `bson:"replSet"` +} + +type Sharding struct { + ClusterRole string `bson:"clusterRole"` +} + +type CloStorage struct { + DbPath string `bson:"dbPath"` + Engine string `bson:"engine"` +} + +type CloSystemLog struct { + Destination string `bson:"destination"` + Path string `bson:"path"` +} + +type Parsed struct { + Sharding Sharding `bson:"sharding"` + Storage CloStorage `bson:"storage"` + SystemLog CloSystemLog `bson:"systemLog"` + Net Net `bson:"net"` + ProcessManagement ProcessManagement `bson:"processManagement"` + Replication Replication `bson:"replication"` + Security Security `bson:"security"` +} + +// Security is a struct to hold security related configs +type Security struct { + KeyFile string `bson:"keyFile"` + ClusterAuthMode string `bson:"clusterAuthMode"` + Authorization string `bson:"authorization"` + JavascriptEnabled bool `bson:javascriptEnabled"` + Sasl struct { + HostName string `bson:"hostName"` + ServiceName string `bson:"serverName"` + SaslauthdSocketPath string `bson:"saslauthdSocketPath"` + } `bson:"sasl"` + EnableEncryption bool `bson:"enableEncryption"` + EncryptionCipherMode string `bson:"encryptionCipherMode"` + EncryptionKeyFile string `bson:"encryptionKeyFile"` + Kmip struct { + KeyIdentifier string `bson:"keyIdentifier"` + RotateMasterKey bool `bson:"rotateMasterKey"` + ServerName string `bson:"serverName"` + Port string `bson:"port"` + ClientCertificateFile string `bson:"clientCertificateFile"` + ClientCertificatePassword string `bson:"clientCertificatePassword"` + ServerCAFile string `bson:"serverCAFile"` + } `bson:"kmip"` +} + +// NET config options. See https://docs.mongodb.com/manual/reference/configuration-options/#net-options +type Net struct { + HTTP HTTP `bson:"http"` + SSL SSL `bson:"ssl"` + Port int64 `bson:"port"` + BindIP string `bson:"bindIp"` + MaxIncomingConnections int `bson:"maxIncomingConnections"` + WireObjectCheck bool `bson:"wireObjectCheck"` + IPv6 bool `bson:"ipv6"` + UnixDomainSocket struct { + Enabled bool `bson:"enabled"` + PathPrefix string `bson:"pathPrefix"` + FilePermissions int64 `bson:"filePermissions"` + } `bson:"unixDomainSocket"` +} + +type HTTP struct { + Enabled bool `bson:"enabled"` + Port float64 `bson:"port"` + JSONPEnabled bool `bson:"JSONPEnabled"` + RESTInterfaceEnabled bool `bson:"RESTInterfaceEnabled"` +} + +// SSL config options. See https://docs.mongodb.com/manual/reference/configuration-options/#net-ssl-options +type SSL struct { + SSLOnNormalPorts bool `bson:"sslOnNormalPorts"` // deprecated since 2.6 + Mode string `bson:"mode"` // disabled, allowSSL, preferSSL, requireSSL + PEMKeyFile string `bson:"PEMKeyFile"` + PEMKeyPassword string `bson:"PEMKeyPassword"` + ClusterFile string `bson:"clusterFile"` + ClusterPassword string `bson:"clusterPassword"` + CAFile string `bson:"CAFile"` + CRLFile string `bson:"CRLFile"` + AllowConnectionsWithoutCertificates bool `bson:"allowConnectionsWithoutCertificates"` + AllowInvalidCertificates bool `bson:"allowInvalidCertificates"` + AllowInvalidHostnames bool `bson:"allowInvalidHostnames"` + DisabledProtocols string `bson:"disabledProtocols"` + FIPSMode bool `bson:"FIPSMode"` +} + +type CommandLineOptions struct { + Argv []string `bson:"argv"` + Ok float64 `bson:"ok"` + Parsed Parsed `bson:"parsed"` + Security Security `bson:"security"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/collstats.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/collstats.go new file mode 100644 index 00000000..8fef4703 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/collstats.go @@ -0,0 +1,46 @@ +package proto + +type ShardStas struct { + Ns string `json:"ns"` + Count int64 `json:"count"` + Size int64 `json:"size"` + AvgObjSize int64 `json:"avgObjSize"` + NumExtents int64 `json:"numExtents"` + StorageSize int64 `json:"storageSize"` + LastExtentSize int64 `json:"lastExtentSize"` + PaddingFactor int64 `json:"paddingFactor"` + PaddingFactorNote string `json:"paddingFactorNote"` + UserFlags int64 `json:"userFlags"` + Capped bool `json:"capped"` + Nindexes int64 `json:"nindexes"` + IndexDetails struct { + } `json:"indexDetails"` + TotalIndexSize int64 `json:"totalIndexSize"` + IndexSizes struct { + ID int64 `json:"_id_"` + IDHashed int64 `json:"_id_hashed"` + } `json:"indexSizes"` + Ok int `json:"ok"` +} + +type CollStats struct { + Sharded bool `json:"sharded"` + PaddingFactorNote string `json:"paddingFactorNote"` + UserFlags int64 `json:"userFlags"` + Capped bool `json:"capped"` + Ns string `json:"ns"` + Count int64 `json:"count"` + NumExtents int64 `json:"numExtents"` + Size int64 `json:"size"` + StorageSize int64 `json:"storageSize"` + TotalIndexSize int64 `json:"totalIndexSize"` + IndexSizes struct { + ID int `json:"_id_"` + IDHashed int `json:"_id_hashed"` + } `json:"indexSizes"` + AvgObjSize int64 `json:"avgObjSize"` + Nindexes int64 `json:"nindexes"` + Nchunks int64 `json:"nchunks"` + Shards map[string]ShardStas `json:"shards"` + Ok int64 `json:"ok"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/connections.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/connections.go new file mode 100644 index 00000000..2ff17e39 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/connections.go @@ -0,0 +1,7 @@ +package proto + +type Connections struct { + Available float64 `bson:"available"` + Current float64 `bson:"current"` + TotalCreated float64 `bson:"totalCreated"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/currentOp.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/currentOp.go new file mode 100644 index 00000000..9ff8ce95 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/currentOp.go @@ -0,0 +1,68 @@ +package proto + +type Query struct { + CurrentOp float64 `bson:"currentOp"` +} + +type Progress struct { + Done float64 `bson:"done"` + Total float64 `bson:"total"` +} + +type AcquireCount struct { + Rr float64 `bson:"r"` + Ww float64 `bson:"w"` + R float64 `bson:"R"` + W float64 `bson:"W"` +} + +type LockInfo struct { + DeadlockCount AcquireCount `bson:"deadlockCount"` + AcquireCount AcquireCount `bson:"acquireCount"` + AcquireWaitCount AcquireCount `bson:"acquireWaitCount"` + TimeAcquiringMicros AcquireCount `bson:"timeAcquiringMicros"` +} + +type CurrentOpLockStats struct { + Global LockInfo `bson:"Global"` + MMAPV1Journal interface{} `bson:"MMAPV1Journal"` + Database interface{} `bson:"Database"` +} + +type Locks struct { + Global LockInfo `bson:"Global"` + MMAPV1Journal LockInfo `bson:"MMAPV1Journal"` + Database LockInfo `bson:"Database"` + Collection LockInfo `bson:"Collection"` + Metadata LockInfo `bson:"Metadata"` + Oplog LockInfo `bson:"oplog"` +} + +type Inprog struct { + Desc string `bson:"desc"` + ConnectionId float64 `bson:"connectionId"` + Opid float64 `bson:"opid"` + Msg string `bson:"msg"` + NumYields float64 `bson:"numYields"` + Locks Locks `bson:"locks"` + WaitingForLock float64 `bson:"waitingForLock"` + ThreadId string `bson:"threadId"` + Active float64 `bson:"active"` + MicrosecsRunning float64 `bson:"microsecs_running"` + SecsRunning float64 `bson:"secs_running"` + Op string `bson:"op"` + Ns string `bson:"ns"` + Insert interface{} `bson:"insert"` + PlanSummary string `bson:"planSummary"` + Client string `bson:"client"` + Query Query `bson:"query"` + Progress Progress `bson:"progress"` + KillPending float64 `bson:"killPending"` + LockStats CurrentOpLockStats `bson:"lockStats"` +} + +type CurrentOp struct { + Info string `bson:"info"` + Inprog []Inprog `bson:"inprog"` + FsyncLock float64 `bson:"fsyncLock"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/cursors.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/cursors.go new file mode 100644 index 00000000..37f4c8c1 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/cursors.go @@ -0,0 +1,10 @@ +package proto + +type Cursors struct { + ClientCursorsSize float64 `bson:"clientCursors_size"` + Note string `bson:"note"` + Pinned float64 `bson:"pinned"` + TimedOut float64 `bson:"timedOut"` + TotalNoTimeout float64 `bson:"totalNoTimeout"` + TotalOpen float64 `bson:"totalOpen"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/databases.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/databases.go new file mode 100644 index 00000000..5ead156a --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/databases.go @@ -0,0 +1,13 @@ +package proto + +// Database item plus struct to hold collections stats +type Database struct { + Name string `bson:"name"` + SizeOnDisk int64 `bson:"sizeOnDisk"` + Empty bool `bson:"empty"` +} + +// Database struct for listDatabases command +type Databases struct { + Databases []Database `bson:"databases"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/durability.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/durability.go new file mode 100644 index 00000000..f31c03e8 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/durability.go @@ -0,0 +1,21 @@ +package proto + +type TimeMs struct { + WriteToDataFiles float64 `bson:"writeToDataFiles"` + WriteToJournal float64 `bson:"writeToJournal"` + Commits float64 `bson:"commits"` + CommitsInWriteLock float64 `bson:"commitsInWriteLock"` + Dt float64 `bson:"dt"` + PrepLogBuffer float64 `bson:"prepLogBuffer"` + RemapPrivateView float64 `bson:"remapPrivateView"` +} + +type Dur struct { + TimeMs *TimeMs `bson:"timeMs"` + WriteToDataFilesMB float64 `bson:"writeToDataFilesMB"` + Commits float64 `bson:"commits"` + CommitsInWriteLock float64 `bson:"commitsInWriteLock"` + Compression float64 `bson:"compression"` + EarlyCommits float64 `bson:"earlyCommits"` + JournaledMB float64 `bson:"journaledMB"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/extrainfo.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/extrainfo.go new file mode 100644 index 00000000..03939cb9 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/extrainfo.go @@ -0,0 +1,7 @@ +package proto + +type ExtraInfo struct { + PageFaults float64 `bson:"page_faults"` + HeapUsageBytes float64 `bson:"heap_usage_bytes"` + Note string `bson:"note"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/get_shard_map.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/get_shard_map.go new file mode 100644 index 00000000..7bd500ef --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/get_shard_map.go @@ -0,0 +1,6 @@ +package proto + +type ShardsMap struct { + Map map[string]string `bson:"map"` + OK int `bson:"ok"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/globallock.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/globallock.go new file mode 100644 index 00000000..f4b10c25 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/globallock.go @@ -0,0 +1,19 @@ +package proto + +type GlobalLock struct { + ActiveClients *ActiveClients `bson:"activeClients"` + CurrentQueue *CurrentQueue `bson:"currentQueue"` + TotalTime int64 `bson:"totalTime"` +} + +type ActiveClients struct { + Readers int64 `bson:"readers"` + Total int64 `bson:"total"` + Writers int64 `bson:"writers"` +} + +type CurrentQueue struct { + Writers int64 `bson:"writers"` + Readers int64 `bson:"readers"` + Total int64 `bson:"total"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/hostinfo.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/hostinfo.go new file mode 100644 index 00000000..dc391ecc --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/hostinfo.go @@ -0,0 +1,39 @@ +package proto + +type Extra struct { + LibcVersion string `bson:"libcVersion"` + PageSize float64 `bson:"pageSize"` + VersionSignature string `bson:"versionSignature"` + NumPages float64 `bson:"numPages"` + VersionString string `bson:"versionString"` + CpuFeatures string `bson:"cpuFeatures"` + CpuFrequencyMHz string `bson:"cpuFrequencyMHz"` + KernelVersion string `bson:"kernelVersion"` + MaxOpenFiles float64 `bson:"maxOpenFiles"` +} + +type Os struct { + Type string `bson:"type"` + Version string `bson:"version"` + Name string `bson:"name"` +} + +type System struct { + CurrentTime string `bson:"currentTime"` + Hostname string `bson:"hostname"` + MemSizeMB float64 `bson:"memSizeMB"` + NumCores float64 `bson:"numCores"` + NumaEnabled bool `bson:"numaEnabled"` + CpuAddrSize float64 `bson:"cpuAddrSize"` + CpuArch string `bson:"cpuArch"` +} + +// HostInfo has exported field for the 'hostInfo' command plus some other +// fields like Database/Collections count. We are setting those fields into +// a separated function +type HostInfo struct { + Extra *Extra `bson:"extra"` + Os *Os `bson:"os"` + System *System `bson:"system"` + ID int +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/locks.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/locks.go new file mode 100644 index 00000000..36a68f22 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/locks.go @@ -0,0 +1,7 @@ +package proto + +type AcquiredLocks struct { + AcquireCount *AcquireCount `bson:"acquireCount"` + AcquireWaitCount float64 `bson:"acquireWaitCount.W"` + TimeAcquiringMicros float64 `bson:"timeAcquiringMicros.W"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/master_doc.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/master_doc.go new file mode 100644 index 00000000..796e10fc --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/master_doc.go @@ -0,0 +1,7 @@ +package proto + +type MasterDoc struct { + SetName interface{} `bson:"setName"` + Hosts interface{} `bson:"hosts"` + Msg string `bson:"msg"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/memory.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/memory.go new file mode 100644 index 00000000..53117920 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/memory.go @@ -0,0 +1,10 @@ +package proto + +type Mem struct { + Bits float64 `bson:"bits"` + Mapped float64 `bson:"mapped"` + MappedWithJournal float64 `bson:"mappedWithJournal"` + Resident float64 `bson:"resident"` + Supported bool `bson:"supported"` + Virtual float64 `bson:"virtual"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/metrics.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/metrics.go new file mode 100644 index 00000000..ce554ee3 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/metrics.go @@ -0,0 +1,85 @@ +package proto + +type Metrics struct { + Commands map[string]CommandStats `bson:"commands"` + Cursor *Cursor `bson:"cursor"` + Document *Document `bson:"document"` + GetLastError *GetLastError `bson:"getLastError"` + Moves float64 `bson:"record.moves"` + Operation *Operation `bson:"operation"` + QueryExecutor *QueryExecutor `bson:"queryExecutor"` + Repl *ReplMetrics `bson:"repl"` + Storage *Storage `bson:"storage"` + Ttl *Ttl `bson:"ttl"` +} + +type CommandStats struct { + Failed float64 `bson:"failed"` + Total float64 `bson:"total"` +} + +type Cursor struct { + NoTimeout float64 `bson:"open.noTimeout"` + Pinned float64 `bson:"open.pinned"` + TimedOut float64 `bson:"timedOut"` + Total float64 `bson:"open.total"` +} + +type Document struct { + Deleted float64 `bson:"deleted"` + Inserted float64 `bson:"inserted"` + Returned float64 `bson:"returned"` + Updated float64 `bson:"updated"` +} + +type GetLastError struct { + Wtimeouts float64 `bson:"wtimeouts"` + Num float64 `bson:"wtime.num"` + TotalMillis float64 `bson:"wtime.totalMillis"` +} + +type ReplMetrics struct { + Batches *MetricStats `bson:"apply.batches"` + BufferSizeBytes float64 `bson:"buffer.sizeBytes"` + BufferCount float64 `bson:"buffer.count"` + BufferMaxSizeBytes float64 `bson:"buffer.maxSizeBytes"` + Network *ReplNetwork `bson:"network"` + Ops float64 `bson:"apply.ops"` + PreloadDocs *MetricStats `bson:"preload.docs"` + PreloadIndexes *MetricStats `bson:"preload.indexes"` +} + +type Storage struct { + BucketExhausted float64 `bson:"freelist.search.bucketExhausted"` + Requests float64 `bson:"freelist.search.requests"` + Scanned float64 `bson:"freelist.search.scanned"` +} + +type MetricStats struct { + Num float64 `bson:"num"` + TotalMillis float64 `bson:"totalMillis"` +} + +type ReplNetwork struct { + Getmores *MetricStats `bson:"getmores"` + Ops float64 `bson:"ops"` + ReadersCreated float64 `bson:"readersCreated"` + Bytes float64 `bson:"bytes"` +} + +type Operation struct { + Fastmod float64 `bson:"fastmod"` + Idhack float64 `bson:"idhack"` + ScanAndOrder float64 `bson:"scanAndOrder"` + WriteConflicts float64 `bson:"writeConflicts"` +} + +type QueryExecutor struct { + Scanned float64 `bson:"scanned"` + ScannedObjects float64 `bson:"scannedObjects"` +} + +type Ttl struct { + DeletedDocuments float64 `bson:"deletedDocuments"` + Passes float64 `bson:"passes"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/network.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/network.go new file mode 100644 index 00000000..4053aa32 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/network.go @@ -0,0 +1,7 @@ +package proto + +type Network struct { + BytesIn float64 `bson:"bytesIn"` + BytesOut float64 `bson:"bytesOut"` + NumRequests float64 `bson:"numRequests"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/opcounters.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/opcounters.go new file mode 100644 index 00000000..f9be7fa7 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/opcounters.go @@ -0,0 +1,10 @@ +package proto + +type Opcounters struct { + Command float64 `bson:"command"` + Delete float64 `bson:"delete"` + Getmore float64 `bson:"getmore"` + Insert float64 `bson:"insert"` + Query float64 `bson:"query"` + Update float64 `bson:"update"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/oplog.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/oplog.go new file mode 100644 index 00000000..371f13cd --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/oplog.go @@ -0,0 +1,76 @@ +package proto + +import ( + "time" + + "gopkg.in/mgo.v2/bson" +) + +type OplogEntry struct { + Name string + Options struct { + Capped bool + Size int64 + AutoIndexId bool + } +} + +type OplogInfo struct { + Hostname string + Size int64 + UsedMB int64 + TimeDiff int64 + TimeDiffHours float64 + Running string // TimeDiffHours in human readable format + TFirst time.Time + TLast time.Time + Now time.Time + ElectionTime time.Time +} + +type OpLogs []OplogInfo + +func (s OpLogs) Len() int { + return len(s) +} +func (s OpLogs) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} +func (s OpLogs) Less(i, j int) bool { + return s[i].TimeDiffHours < s[j].TimeDiffHours +} + +type OplogRow struct { + Timestamp int64 `bson:"ts,omitempty"` + HistoryId int64 `bson:"h,omitempty"` + Version int64 `bson:"v,omitempty"` + Operation string `bson:"op,omitempty"` + Namespace string `bson:"ns,omitempty"` + Object bson.D `bson:"o,omitempty"` + Query bson.D `bson:"o2,omitempty"` +} + +type OplogColStats struct { + NumExtents int + IndexDetails bson.M + Nindexes int + TotalIndexSize int64 + Size int64 + PaddingFactorNote string + Capped bool + MaxSize int64 + IndexSizes bson.M + GleStats struct { + LastOpTime int64 + ElectionId string + } `bson:"$gleStats"` + StorageSize int64 + PaddingFactor int64 + AvgObjSize int64 + LastExtentSize int64 + UserFlags int64 + Max int64 + Ok int + Ns string + Count int64 +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/profiler_status.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/profiler_status.go new file mode 100644 index 00000000..53383113 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/profiler_status.go @@ -0,0 +1,13 @@ +package proto + +// ProfilerStatus is a struct to hold the results of db.getProfilingLevel() +// var ps proto.ProfilerStatus +// err := db.Run(bson.M{"profile": -1}, &ps) +type ProfilerStatus struct { + Was int64 `bson:"was"` + SlowMs int64 `bson:"slowms"` + GleStats struct { + ElectionID string `bson:"electionId"` + LastOpTime int64 `bson:"lastOpTime"` + } `bson:"$gleStats"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/replconfig.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/replconfig.go new file mode 100644 index 00000000..8ce984d2 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/replconfig.go @@ -0,0 +1,50 @@ +package proto + +import ( + "gopkg.in/mgo.v2/bson" +) + +type ReplicaSetConfigTags map[string]string +type GetLastErrorModes map[string]*ReplicaSetConfigTags + +// https://docs.mongodb.com/v3.2/reference/command/getLastError/#dbcmd.getLastError +type GetLastErrorDefaults struct { + Journal bool `bson:"j,omitempty"` // If true, wait for the next journal commit before returning, rather than waiting for a full disk flush. + WriteConcern int64 `bson:"w,omitempty"` // When running with replication, this is the number of servers to replicate to before returning. + WTimeout int64 `bson:"wtimeout,omitempty"` // Optional. Milliseconds. Specify a value in milliseconds to control how long to wait for write propagation to complete. +} + +// https://docs.mongodb.com/v3.2/reference/replica-configuration/#rsconf.members +type ReplicaSetConfigMember struct { + ID int64 `bson:"_id,omitempty"` // An integer identifier of every member in the replica set. + Host string `bson:"host,omitempty"` // The hostname and, if specified, the port number, of the set member. + ArbiterOnly bool `bson:"arbiterOnly,omitempty"` // A boolean that identifies an arbiter. A value of true indicates that the member is an arbiter. + BuildIndexes bool `bson:"buildIndexes,omitempty"` // A boolean that indicates whether the mongod builds indexes on this member. + Hidden bool `bson:"hidden,omitempty"` // When this value is true, the replica set hides this instance and does not include the member in the output of db.isMaster() or isMaster. + Priority int64 `bson:"priority,omitempty"` // A number that indicates the relative eligibility of a member to become a primary. + Tags *ReplicaSetConfigTags `bson:"tags,omitempty"` // A tag set document containing mappings of arbitrary keys and values. + SlaveDelay int64 `bson:"slaveDelay,omitempty"` // The number of seconds “behind” the primary that this replica set member should “lag”. + Votes int64 `bson:"votes,omitempty"` // The number of votes a server will cast in a replica set election. +} + +// https://docs.mongodb.com/v3.2/reference/replica-configuration/#rsconf.settings +type ReplicaSetConfigSettings struct { + ChainingAllowed bool `bson:"chainingAllowed,omitempty"` // When chainingAllowed is true, the replica set allows secondary members to replicate from other secondary members. + HeartbeatTimeoutSecs int64 `bson:"heartbeatTimeoutSecs,omitempty"` // Number of seconds that the replica set members wait for a successful heartbeat from each other. + HeartbeatIntervalMillis int64 `bson:"heartbeatIntervalMillis,omitempty"` // The frequency in milliseconds of the heartbeats. + ElectionTimeoutMillis int64 `bson:"electionTimeoutMillis,omitempty"` // The time limit in milliseconds for detecting when a replica set’s primary is unreachable. + GetLastErrorDefaults *GetLastErrorDefaults `bson:"getLastErrorDefaults,omitempty"` // A document that specifies the write concern for the replica set. + GetLastErrorModes *GetLastErrorModes `bson:"getLastErrorModes,omitempty"` // A document used to define an extended write concern through the use of members[n].tags. + ReplicaSetId *bson.ObjectId `bson:"replicaSetId,omitempty"` // Replset Id (ObjectId) +} + +type ReplicaSetConfig struct { + Config struct { + ID string `bson:"_id,omitempty"` // The name of the replica set. Once set, you cannot change the name of a replica set. + ProtocolVersion int64 `bson:"protocolVersion,omitempty"` // By default, new replica sets in MongoDB 3.2 use protocolVersion: 1. Previous versions of MongoDB use version 0. + Version int64 `bson:"version,omitempty"` // An incrementing number used to distinguish revisions of the replica set configuration object from previous iterations. + Members []*ReplicaSetConfigMember `bson:"members,omitempty"` // An array of member configuration documents, one for each member of the replica set. + Settings *ReplicaSetConfigSettings `bson:"settings,omitempty"` // A document that contains configuration options that apply to the whole replica set. + } `bson:"config,omitempty"` // https://docs.mongodb.com/v3.2/reference/replica-configuration/#replica-set-configuration-fields + Ok int64 `bson:"ok,omitempty"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/replicas.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/replicas.go new file mode 100644 index 00000000..dd6206cd --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/replicas.go @@ -0,0 +1,13 @@ +package proto + +type Repl struct { + Rbid float64 `bson:"rbid"` + SetVersion float64 `bson:"setVersion"` + ElectionId string `bson:"electionId"` + Primary string `bson:"primary"` + Me string `bson:"me"` + Secondary bool `bson:"secondary"` + SetName string `bson:"setName"` + Hosts []string `bson:"hosts"` + Ismaster bool `bson:"ismaster"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/replstatus.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/replstatus.go new file mode 100644 index 00000000..1396bf5e --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/replstatus.go @@ -0,0 +1,48 @@ +package proto + +const ( + REPLICA_SET_MEMBER_STARTUP = iota + REPLICA_SET_MEMBER_PRIMARY + REPLICA_SET_MEMBER_SECONDARY + REPLICA_SET_MEMBER_RECOVERING + REPLICA_SET_MEMBER_STARTUP2 + REPLICA_SET_MEMBER_UNKNOWN + REPLICA_SET_MEMBER_ARBITER + REPLICA_SET_MEMBER_DOWN + REPLICA_SET_MEMBER_ROOLBACK + REPLICA_SET_MEMBER_REMOVED +) + +type Optime struct { + Ts float64 `bson:"ts"` // The Timestamp of the last operation applied to this member of the replica set from the oplog. + T float64 `bson:"t"` // The term in which the last applied operation was originally generated on the primary. +} + +type Members struct { + Optime *Optime `bson:"optime"` // See Optime struct + OptimeDate string `bson:"optimeDate"` // The last entry from the oplog that this member applied. + InfoMessage string `bson:"infoMessage"` // A message + ID int64 `bson:"_id"` // Server ID + Name string `bson:"name"` // server name + Health float64 `bson:"health"` // This field conveys if the member is up (i.e. 1) or down (i.e. 0). + StateStr string `bson:"stateStr"` // A string that describes state. + Uptime float64 `bson:"uptime"` // number of seconds that this member has been online. + ConfigVersion float64 `bson:"configVersion"` // revision # of the replica set configuration object from previous iterations of the configuration. + Self bool `bson:"self"` // true if this is the server we are currently connected + State float64 `bson:"state"` // integer between 0 and 10 that represents the replica state of the member. + ElectionTime int64 `bson:"electionTime"` // For the current primary, information regarding the election Timestamp from the operation log. + ElectionDate string `bson:"electionDate"` // For the current primary, an ISODate formatted date string that reflects the election date + Set string `bson:"-"` + StorageEngine StorageEngine +} + +// Struct for replSetGetStatus +type ReplicaSetStatus struct { + Date string `bson:"date"` // Current date + MyState float64 `bson:"myState"` // Integer between 0 and 10 that represents the replica state of the current member + Term float64 `bson:"term"` // The election count for the replica set, as known to this replica set member. Mongo 3.2+ + HeartbeatIntervalMillis float64 `bson:"heartbeatIntervalMillis"` // The frequency in milliseconds of the heartbeats. 3.2+ + Members []Members `bson:"members"` // + Ok float64 `bson:"ok"` // + Set string `bson:"set"` // Replica set name +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/server_status.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/server_status.go new file mode 100644 index 00000000..c5a881c3 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/server_status.go @@ -0,0 +1,196 @@ +package proto + +import "time" + +type ServerStatus struct { + Host string `bson:"host"` + Version string `bson:"version"` + Process string `bson:"process"` + Pid int64 `bson:"pid"` + Uptime int64 `bson:"uptime"` + UptimeMillis int64 `bson:"uptimeMillis"` + UptimeEstimate int64 `bson:"uptimeEstimate"` + LocalTime time.Time `bson:"localTime"` + Asserts map[string]int64 `bson:"asserts"` + BackgroundFlushing *FlushStats `bson:"backgroundFlushing"` + ExtraInfo *ExtraInfo `bson:"extra_info"` + Connections *ConnectionStats `bson:"connections"` + Dur *DurStats `bson:"dur"` + GlobalLock *GlobalLockStats `bson:"globalLock"` + Locks map[string]LockStats `bson:"locks,omitempty"` + Network *NetworkStats `bson:"network"` + Opcounters *OpcountStats `bson:"opcounters"` + OpcountersRepl *OpcountStats `bson:"opcountersRepl"` + RecordStats *DBRecordStats `bson:"recordStats"` + Mem *MemStats `bson:"mem"` + Repl *ReplStatus `bson:"repl"` + ShardCursorType map[string]interface{} `bson:"shardCursorType"` + StorageEngine StorageEngine `bson:"storageEngine"` + WiredTiger *WiredTiger `bson:"wiredTiger"` +} + +type StorageEngine struct { + Name string `bson:"name"` + SupportCommittedREads bool `bson:supportsCommittedReads"` + ReadOnly bool `bson:"readOnly"` + Persistent bool `bson:"persistent"` +} + +// WiredTiger stores information related to the WiredTiger storage engine. +type WiredTiger struct { + Transaction TransactionStats `bson:"transaction"` + Concurrent ConcurrentTransactions `bson:"concurrentTransactions"` + Cache CacheStats `bson:"cache"` +} + +type ConcurrentTransactions struct { + Write ConcurrentTransStats `bson:"write"` + Read ConcurrentTransStats `bson:"read"` +} + +type ConcurrentTransStats struct { + Out int64 `bson:"out"` +} + +// CacheStats stores cache statistics for WiredTiger. +type CacheStats struct { + TrackedDirtyBytes int64 `bson:"tracked dirty bytes in the cache"` + CurrentCachedBytes int64 `bson:"bytes currently in the cache"` + MaxBytesConfigured int64 `bson:"maximum bytes configured"` +} + +// TransactionStats stores transaction checkpoints in WiredTiger. +type TransactionStats struct { + TransCheckpoints int64 `bson:"transaction checkpoints"` +} + +// ReplStatus stores data related to replica sets. +type ReplStatus struct { + SetName string `bson:"setName"` + IsMaster interface{} `bson:"ismaster"` + Secondary interface{} `bson:"secondary"` + IsReplicaSet interface{} `bson:"isreplicaset"` + ArbiterOnly interface{} `bson:"arbiterOnly"` + Hosts []string `bson:"hosts"` + Passives []string `bson:"passives"` + Me string `bson:"me"` +} + +// DBRecordStats stores data related to memory operations across databases. +type DBRecordStats struct { + AccessesNotInMemory int64 `bson:"accessesNotInMemory"` + PageFaultExceptionsThrown int64 `bson:"pageFaultExceptionsThrown"` + DBRecordAccesses map[string]RecordAccesses `bson:",inline"` +} + +// RecordAccesses stores data related to memory operations scoped to a database. +type RecordAccesses struct { + AccessesNotInMemory int64 `bson:"accessesNotInMemory"` + PageFaultExceptionsThrown int64 `bson:"pageFaultExceptionsThrown"` +} + +// MemStats stores data related to memory statistics. +type MemStats struct { + Bits int64 `bson:"bits"` + Resident int64 `bson:"resident"` + Virtual int64 `bson:"virtual"` + Supported interface{} `bson:"supported"` + Mapped int64 `bson:"mapped"` + MappedWithJournal int64 `bson:"mappedWithJournal"` +} + +// FlushStats stores information about memory flushes. +type FlushStats struct { + Flushes int64 `bson:"flushes"` + TotalMs int64 `bson:"total_ms"` + AverageMs float64 `bson:"average_ms"` + LastMs int64 `bson:"last_ms"` + LastFinished time.Time `bson:"last_finished"` +} + +// ConnectionStats stores information related to incoming database connections. +type ConnectionStats struct { + Current int64 `bson:"current"` + Available int64 `bson:"available"` + TotalCreated int64 `bson:"totalCreated"` +} + +// DurTiming stores information related to journaling. +type DurTiming struct { + Dt int64 `bson:"dt"` + PrepLogBuffer int64 `bson:"prepLogBuffer"` + WriteToJournal int64 `bson:"writeToJournal"` + WriteToDataFiles int64 `bson:"writeToDataFiles"` + RemapPrivateView int64 `bson:"remapPrivateView"` +} + +// DurStats stores information related to journaling statistics. +type DurStats struct { + Commits int64 `bson:"commits"` + JournaledMB int64 `bson:"journaledMB"` + WriteToDataFilesMB int64 `bson:"writeToDataFilesMB"` + Compression int64 `bson:"compression"` + CommitsInWriteLock int64 `bson:"commitsInWriteLock"` + EarlyCommits int64 `bson:"earlyCommits"` + TimeMs DurTiming +} + +// QueueStats stores the number of queued read/write operations. +type QueueStats struct { + Total int64 `bson:"total"` + Readers int64 `bson:"readers"` + Writers int64 `bson:"writers"` +} + +// ClientStats stores the number of active read/write operations. +type ClientStats struct { + Total int64 `bson:"total"` + Readers int64 `bson:"readers"` + Writers int64 `bson:"writers"` +} + +// GlobalLockStats stores information related locks in the MMAP storage engine. +type GlobalLockStats struct { + TotalTime int64 `bson:"totalTime"` + LockTime int64 `bson:"lockTime"` + CurrentQueue *QueueStats `bson:"currentQueue"` + ActiveClients *ClientStats `bson:"activeClients"` +} + +// NetworkStats stores information related to network traffic. +type NetworkStats struct { + BytesIn int64 `bson:"bytesIn"` + BytesOut int64 `bson:"bytesOut"` + NumRequests int64 `bson:"numRequests"` +} + +// OpcountStats stores information related to comamnds and basic CRUD operations. +type OpcountStats struct { + Command int64 `bson:"command"` + Delete int64 `bson:"delete"` + GetMore int64 `bson:"getmore"` + Insert int64 `bson:"insert"` + Query int64 `bson:"query"` + Update int64 `bson:"update"` +} + +// ReadWriteLockTimes stores time spent holding read/write locks. +type ReadWriteLockTimes struct { + Read int64 `bson:"R"` + Write int64 `bson:"W"` + ReadLower int64 `bson:"r"` + WriteLower int64 `bson:"w"` +} + +// LockStats stores information related to time spent acquiring/holding locks +// for a given database. +type LockStats struct { + TimeLockedMicros ReadWriteLockTimes `bson:"timeLockedMicros"` + TimeAcquiringMicros ReadWriteLockTimes `bson:"timeAcquiringMicros"` + + // AcquireCount and AcquireWaitCount are new fields of the lock stats only populated on 3.0 or newer. + // Typed as a pointer so that if it is nil, mongostat can assume the field is not populated + // with real namespace data. + AcquireCount *ReadWriteLockTimes `bson:"acquireCount,omitempty"` + AcquireWaitCount *ReadWriteLockTimes `bson:"acquireWaitCount,omitempty"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/sharding_changelog_stats.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/sharding_changelog_stats.go new file mode 100644 index 00000000..f88404a5 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/sharding_changelog_stats.go @@ -0,0 +1,15 @@ +package proto + +type ShardingChangelogSummaryId struct { + Event string `bson:"event"` + Note string `bson:"note"` +} + +type ShardingChangelogSummary struct { + Id *ShardingChangelogSummaryId `bson:"_id"` + Count float64 `bson:"count"` +} + +type ShardingChangelogStats struct { + Items *[]ShardingChangelogSummary +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/shards.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/shards.go new file mode 100644 index 00000000..892fa1fe --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/shards.go @@ -0,0 +1,11 @@ +package proto + +type Shard struct { + ID string `bson:"_id"` + Host string `bson:"host"` +} + +type ShardsInfo struct { + Shards []Shard `bson:"shards"` + OK int `bson:"ok"` +} diff --git a/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/system.profile.go b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/system.profile.go new file mode 100644 index 00000000..cfd0e263 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/src/go/mongolib/proto/system.profile.go @@ -0,0 +1,286 @@ +package proto + +import ( + "strings" + "time" + + "gopkg.in/mgo.v2/bson" +) + +// docsExamined is renamed from nscannedObjects in 3.2.0 +// https://docs.mongodb.com/manual/reference/database-profiler/#system.profile.docsExamined +type SystemProfile struct { + AllUsers []interface{} `bson:"allUsers"` + Client string `bson:"client"` + CursorExhausted bool `bson:"cursorExhausted"` + DocsExamined int `bson:"docsExamined"` + NscannedObjects int `bson:"nscannedObjects"` + ExecStats struct { + Advanced int `bson:"advanced"` + ExecutionTimeMillisEstimate int `bson:"executionTimeMillisEstimate"` + InputStage struct { + Advanced int `bson:"advanced"` + Direction string `bson:"direction"` + DocsExamined int `bson:"docsExamined"` + ExecutionTimeMillisEstimate int `bson:"executionTimeMillisEstimate"` + Filter struct { + Date struct { + Eq string `bson:"$eq"` + } `bson:"date"` + } `bson:"filter"` + Invalidates int `bson:"invalidates"` + IsEOF int `bson:"isEOF"` + NReturned int `bson:"nReturned"` + NeedTime int `bson:"needTime"` + NeedYield int `bson:"needYield"` + RestoreState int `bson:"restoreState"` + SaveState int `bson:"saveState"` + Stage string `bson:"stage"` + Works int `bson:"works"` + } `bson:"inputStage"` + Invalidates int `bson:"invalidates"` + IsEOF int `bson:"isEOF"` + LimitAmount int `bson:"limitAmount"` + NReturned int `bson:"nReturned"` + NeedTime int `bson:"needTime"` + NeedYield int `bson:"needYield"` + RestoreState int `bson:"restoreState"` + SaveState int `bson:"saveState"` + Stage string `bson:"stage"` + Works int `bson:"works"` + } `bson:"execStats"` + KeyUpdates int `bson:"keyUpdates"` + KeysExamined int `bson:"keysExamined"` + Locks struct { + Collection struct { + AcquireCount struct { + R int `bson:"R"` + } `bson:"acquireCount"` + } `bson:"Collection"` + Database struct { + AcquireCount struct { + R int `bson:"r"` + } `bson:"acquireCount"` + } `bson:"Database"` + Global struct { + AcquireCount struct { + R int `bson:"r"` + } `bson:"acquireCount"` + } `bson:"Global"` + MMAPV1Journal struct { + AcquireCount struct { + R int `bson:"r"` + } `bson:"acquireCount"` + } `bson:"MMAPV1Journal"` + } `bson:"locks"` + Millis int `bson:"millis"` + Nreturned int `bson:"nreturned"` + Ns string `bson:"ns"` + NumYield int `bson:"numYield"` + Op string `bson:"op"` + Protocol string `bson:"protocol"` + Query BsonD `bson:"query"` + UpdateObj BsonD `bson:"updateobj"` + Command BsonD `bson:"command"` + OriginatingCommand BsonD `bson:"originatingCommand"` + ResponseLength int `bson:"responseLength"` + Ts time.Time `bson:"ts"` + User string `bson:"user"` + WriteConflicts int `bson:"writeConflicts"` +} + +func NewExampleQuery(doc SystemProfile) ExampleQuery { + return ExampleQuery{ + Ns: doc.Ns, + Op: doc.Op, + Query: doc.Query, + Command: doc.Command, + OriginatingCommand: doc.OriginatingCommand, + UpdateObj: doc.UpdateObj, + } +} + +// ExampleQuery is a subset of SystemProfile +type ExampleQuery struct { + Ns string `bson:"ns" json:"ns"` + Op string `bson:"op" json:"op"` + Query BsonD `bson:"query,omitempty" json:"query,omitempty"` + Command BsonD `bson:"command,omitempty" json:"command,omitempty"` + OriginatingCommand BsonD `bson:"originatingCommand,omitempty" json:"originatingCommand,omitempty"` + UpdateObj BsonD `bson:"updateobj,omitempty" json:"updateobj,omitempty"` +} + +func (self ExampleQuery) Db() string { + ns := strings.SplitN(self.Ns, ".", 2) + if len(ns) > 0 { + return ns[0] + } + return "" +} + +// ExplainCmd returns bson.D ready to use in https://godoc.org/labix.org/v2/mgo#Database.Run +func (self ExampleQuery) ExplainCmd() bson.D { + cmd := self.Command + + switch self.Op { + case "query": + if cmd.Len() == 0 { + cmd = self.Query + } + + // MongoDB 2.6: + // + // "query" : { + // "query" : { + // + // }, + // "$explain" : true + // }, + if _, ok := cmd.Map()["$explain"]; ok { + cmd = BsonD{ + {"explain", ""}, + } + break + } + + if cmd.Len() == 0 || cmd[0].Name != "find" { + var filter interface{} + if cmd.Len() > 0 && cmd[0].Name == "query" { + filter = cmd[0].Value + } else { + filter = cmd + } + + coll := "" + s := strings.SplitN(self.Ns, ".", 2) + if len(s) == 2 { + coll = s[1] + } + + cmd = BsonD{ + {"find", coll}, + {"filter", filter}, + } + } else { + for i := range cmd { + switch cmd[i].Name { + // PMM-1905: Drop "ntoreturn" if it's negative. + case "ntoreturn": + // If it's non-negative, then we are fine, continue to next param. + if cmd[i].Value.(int64) >= 0 { + continue + } + fallthrough + // Drop $db as it is not supported in MongoDB 3.0. + case "$db": + if len(cmd)-1 == i { + cmd = cmd[:i] + } else { + cmd = append(cmd[:i], cmd[i+1:]...) + } + break + } + } + } + case "update": + s := strings.SplitN(self.Ns, ".", 2) + coll := "" + if len(s) == 2 { + coll = s[1] + } + if cmd.Len() == 0 { + cmd = BsonD{ + {Name: "q", Value: self.Query}, + {Name: "u", Value: self.UpdateObj}, + } + } + cmd = BsonD{ + {Name: "update", Value: coll}, + {Name: "updates", Value: []interface{}{cmd}}, + } + case "remove": + s := strings.SplitN(self.Ns, ".", 2) + coll := "" + if len(s) == 2 { + coll = s[1] + } + if cmd.Len() == 0 { + cmd = BsonD{ + {Name: "q", Value: self.Query}, + // we can't determine if limit was 1 or 0 so we assume 0 + {Name: "limit", Value: 0}, + } + } + cmd = BsonD{ + {Name: "delete", Value: coll}, + {Name: "deletes", Value: []interface{}{cmd}}, + } + case "insert": + if cmd.Len() == 0 { + cmd = self.Query + } + if cmd.Len() == 0 || cmd[0].Name != "insert" { + coll := "" + s := strings.SplitN(self.Ns, ".", 2) + if len(s) == 2 { + coll = s[1] + } + + cmd = BsonD{ + {"insert", coll}, + } + } + case "getmore": + if self.OriginatingCommand.Len() > 0 { + cmd = self.OriginatingCommand + for i := range cmd { + // drop $db param as it is not supported in MongoDB 3.0 + if cmd[i].Name == "$db" { + if len(cmd)-1 == i { + cmd = cmd[:i] + } else { + cmd = append(cmd[:i], cmd[i+1:]...) + } + break + } + } + } else { + cmd = BsonD{ + {Name: "getmore", Value: ""}, + } + } + case "command": + if cmd.Len() == 0 || cmd[0].Name != "group" { + break + } + + if group, ok := cmd[0].Value.(BsonD); ok { + for i := range group { + // for MongoDB <= 3.2 + // "$reduce" : function () {} + // It is then Unmarshaled as empty value, so in essence not working + // + // for MongoDB >= 3.4 + // "$reduce" : { + // "code" : "function () {}" + // } + // It is then properly Unmarshaled but then explain fails with "not code" + // + // The $reduce function shouldn't affect explain execution plan (e.g. what indexes are picked) + // so we ignore it for now until we find better way to handle this issue + if group[i].Name == "$reduce" { + group[i].Value = "{}" + cmd[0].Value = group + break + } + } + } + } + + return bson.D{ + { + Name: "explain", + Value: cmd, + }, + } +} diff --git a/vendor/github.com/percona/percona-toolkit/t/lib/bash/alt_cmds.t b/vendor/github.com/percona/percona-toolkit/t/lib/bash/alt_cmds.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/t/lib/bash/alt_cmds.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/vendor/github.com/percona/percona-toolkit/t/lib/bash/collect.t b/vendor/github.com/percona/percona-toolkit/t/lib/bash/collect.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/t/lib/bash/collect.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/vendor/github.com/percona/percona-toolkit/t/lib/bash/collect_mysql_info.t b/vendor/github.com/percona/percona-toolkit/t/lib/bash/collect_mysql_info.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/t/lib/bash/collect_mysql_info.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/vendor/github.com/percona/percona-toolkit/t/lib/bash/collect_system_info.t b/vendor/github.com/percona/percona-toolkit/t/lib/bash/collect_system_info.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/t/lib/bash/collect_system_info.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/vendor/github.com/percona/percona-toolkit/t/lib/bash/daemon.t b/vendor/github.com/percona/percona-toolkit/t/lib/bash/daemon.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/t/lib/bash/daemon.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/vendor/github.com/percona/percona-toolkit/t/lib/bash/log_warn_die.t b/vendor/github.com/percona/percona-toolkit/t/lib/bash/log_warn_die.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/t/lib/bash/log_warn_die.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/vendor/github.com/percona/percona-toolkit/t/lib/bash/mysql_options.t b/vendor/github.com/percona/percona-toolkit/t/lib/bash/mysql_options.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/t/lib/bash/mysql_options.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/vendor/github.com/percona/percona-toolkit/t/lib/bash/parse_options.t b/vendor/github.com/percona/percona-toolkit/t/lib/bash/parse_options.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/t/lib/bash/parse_options.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/vendor/github.com/percona/percona-toolkit/t/lib/bash/report_formatting.t b/vendor/github.com/percona/percona-toolkit/t/lib/bash/report_formatting.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/t/lib/bash/report_formatting.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/vendor/github.com/percona/percona-toolkit/t/lib/bash/report_mysql_info.t b/vendor/github.com/percona/percona-toolkit/t/lib/bash/report_mysql_info.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/t/lib/bash/report_mysql_info.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/vendor/github.com/percona/percona-toolkit/t/lib/bash/report_system_info.t b/vendor/github.com/percona/percona-toolkit/t/lib/bash/report_system_info.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/t/lib/bash/report_system_info.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/vendor/github.com/percona/percona-toolkit/t/lib/bash/safeguards.t b/vendor/github.com/percona/percona-toolkit/t/lib/bash/safeguards.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/t/lib/bash/safeguards.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/vendor/github.com/percona/percona-toolkit/t/lib/bash/summary_common.t b/vendor/github.com/percona/percona-toolkit/t/lib/bash/summary_common.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/t/lib/bash/summary_common.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/vendor/github.com/percona/percona-toolkit/t/lib/bash/tmpdir.t b/vendor/github.com/percona/percona-toolkit/t/lib/bash/tmpdir.t new file mode 120000 index 00000000..edb04c13 --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/t/lib/bash/tmpdir.t @@ -0,0 +1 @@ +../../../util/test-bash-functions \ No newline at end of file diff --git a/vendor/github.com/percona/percona-toolkit/util/NaturalDocs/License.txt b/vendor/github.com/percona/percona-toolkit/util/NaturalDocs/License.txt new file mode 100644 index 00000000..70aabbed --- /dev/null +++ b/vendor/github.com/percona/percona-toolkit/util/NaturalDocs/License.txt @@ -0,0 +1,275 @@ +Title: License + +Natural Docs is Copyright 2003-2010 Greg Valure. Natural Docs is licensed under version 3 of the GNU +Affero General Public License (AGPL). + +Natural Docs incorporates code from Google Prettify, which is Copyright 2006 Google Inc. Google Prettify +may be obtained separately under version 2.0 of the Apache License. However, this combined product is still +licensed under the terms of the AGPLv3. + +Portions of Natural Docs may be distributed with generated documentation files in order to help them function, +such as JavaScript and CSS files. These individual files retain their copyright and licensing terms, but they do +not apply to the remainder of the generated documentation. All other generated documentation files remain +under the copyright and distribution terms decided by its author(s). + + +Topic: GNU Affero General Public License + + Version 3, 19 November 2007 + + Copyright 2007 Free Software Foundation, Inc. + + Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. + + + Preamble: + + The GNU Affero General Public License is a free, copyleft license for software and other kinds of works, specifically designed to ensure cooperation with the community in the case of network server software. + + The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, our General Public Licenses are intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. + + When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. + + Developers that use our General Public Licenses protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License which gives you legal permission to copy, distribute and/or modify the software. + + A secondary benefit of defending all users' freedom is that improvements made in alternate versions of the program, if they receive widespread use, become available for other developers to incorporate. Many developers of free software are heartened and encouraged by the resulting cooperation. However, in the case of software used on network servers, this result may fail to come about. The GNU General Public License permits making a modified version and letting the public access it on a server without ever releasing its source code to the public. + + The GNU Affero General Public License is designed specifically to ensure that, in such cases, the modified source code becomes available to the community. It requires the operator of a network server to provide the source code of the modified version running there to the users of that server. Therefore, public use of a modified version, on a publicly accessible server, gives the public access to the source code of the modified version. + + An older license, called the Affero General Public License and published by Affero, was designed to accomplish similar goals. This is a different license, not a version of the Affero GPL, but Affero has released a new version of the Affero GPL which permits relicensing under this license. + + The precise terms and conditions for copying, distribution and modification follow. + + + TERMS AND CONDITIONS: + + 0. Definitions: + + "This License" refers to version 3 of the GNU Affero General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations + + To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based on the Program. + + To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this + License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. + + + 1. Source Code: + + The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. + + A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. + + The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. + + The Corresponding Source for a work in source code form is that same work. + + + 2. Basic Permissions: + + All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. + + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law: + + No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. + + When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. + + + 4. Conveying Verbatim Copies: + + You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. + + + 5. Conveying Modified Source Versions: + + You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code +under the terms of section 4, provided that you also meet all of these conditions: + + * a) The work must carry prominent notices stating that you modified it, and giving a relevant date. + * b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". + * c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. + * d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. + + A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. + + + 6. Conveying Non-Source Forms: + + You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, in one of these ways: + + * a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. + * b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. + * c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. + * d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. + * e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. + + If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). + + The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. + + + 7. Additional Terms: + + "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the +copyright holders of that material) supplement the terms of this License with terms: + + * a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or + * b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or + * c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or + * d) Limiting the use for publicity purposes of names of licensors or authors of the material; or + * e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or + * f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. + + All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. + + + 8. Termination: + + You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). + + However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. + + Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. + + + 9. Acceptance Not Required for Having Copies: + + You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. + + + 10. Automatic Licensing of Downstream Recipients: + + Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. + + + 11. Patents: + + A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. + + If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. + + A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. + + + 12. No Surrender of Others' Freedom: + + If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. + + + 13. Remote Network Interaction; Use with the GNU General Public License: + + Notwithstanding any other provision of this License, if you modify the Program, your modified version must prominently offer all users interacting with it remotely through a computer network (if your version supports such interaction) an opportunity to receive the Corresponding Source of your version by providing access to the Corresponding Source from a network server at no charge, through some standard or customary means of facilitating copying of software. This Corresponding Source shall include the Corresponding Source for any work covered by version 3 of the GNU General Public License that is incorporated pursuant to the following paragraph. + + Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the work with which it is combined will remain governed by version 3 of the GNU General Public License. + + + 14. Revised Versions of this License: + + The Free Software Foundation may publish revised and/or new versions of the GNU Affero General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU Affero General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU Affero General Public License, you may choose any version ever published by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future versions of the GNU Affero General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. + + Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. + + + 15. Disclaimer of Warranty: + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + + 16. Limitation of Liability: + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + + + 17. Interpretation of Sections 15 and 16: + + If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + +How to Apply These Terms to Your New Programs: + +If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. + +To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + +If your software can interact with users remotely through a computer network, you should also make sure that it provides a way for users to get its source. For example, if your program is a web application, its interface could display a "Source" link that leads users to an archive of the code. There are many ways you could offer source, and different solutions will be better for different programs; see section 13 for the specific requirements. + +You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU AGPL, see . + diff --git a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/add_qan_my_sql_perf_schema_agent_responses.go b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/add_qan_my_sql_perf_schema_agent_responses.go index 6cbf744f..7dc51fb0 100644 --- a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/add_qan_my_sql_perf_schema_agent_responses.go +++ b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/add_qan_my_sql_perf_schema_agent_responses.go @@ -283,6 +283,9 @@ type AddQANMySQLPerfSchemaAgentOKBodyQANMysqlPerfschemaAgent struct { // The pmm-agent identifier which runs this instance. PMMAgentID string `json:"pmm_agent_id,omitempty"` + // True if query examples are disabled. + QueryExamplesDisabled bool `json:"query_examples_disabled,omitempty"` + // Service identifier. ServiceID string `json:"service_id,omitempty"` diff --git a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/add_qan_my_sql_slowlog_agent_responses.go b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/add_qan_my_sql_slowlog_agent_responses.go index fcfcd8fb..2975e98a 100644 --- a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/add_qan_my_sql_slowlog_agent_responses.go +++ b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/add_qan_my_sql_slowlog_agent_responses.go @@ -127,6 +127,13 @@ type AddQANMySQLSlowlogAgentBody struct { // Custom user-assigned labels. CustomLabels map[string]string `json:"custom_labels,omitempty"` + // Disable query examples. + DisableQueryExamples bool `json:"disable_query_examples,omitempty"` + + // Rotate slowlog file at this size if > 0. + // Use zero or negative value to disable rotation. + MaxSlowlogFileSize string `json:"max_slowlog_file_size,omitempty"` + // MySQL password for getting slowlog data. Password string `json:"password,omitempty"` @@ -277,12 +284,18 @@ type AddQANMySQLSlowlogAgentOKBodyQANMysqlSlowlogAgent struct { // Desired Agent status: enabled (false) or disabled (true). Disabled bool `json:"disabled,omitempty"` + // Slowlog file is rotated at this size if > 0. + MaxSlowlogFileSize string `json:"max_slowlog_file_size,omitempty"` + // MySQL password for getting performance data. Password string `json:"password,omitempty"` // The pmm-agent identifier which runs this instance. PMMAgentID string `json:"pmm_agent_id,omitempty"` + // True if query examples are disabled. + QueryExamplesDisabled bool `json:"query_examples_disabled,omitempty"` + // Service identifier. ServiceID string `json:"service_id,omitempty"` diff --git a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/change_qan_my_sql_perf_schema_agent_responses.go b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/change_qan_my_sql_perf_schema_agent_responses.go index 7f8348fd..a16c0087 100644 --- a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/change_qan_my_sql_perf_schema_agent_responses.go +++ b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/change_qan_my_sql_perf_schema_agent_responses.go @@ -298,6 +298,9 @@ type ChangeQANMySQLPerfSchemaAgentOKBodyQANMysqlPerfschemaAgent struct { // The pmm-agent identifier which runs this instance. PMMAgentID string `json:"pmm_agent_id,omitempty"` + // True if query examples are disabled. + QueryExamplesDisabled bool `json:"query_examples_disabled,omitempty"` + // Service identifier. ServiceID string `json:"service_id,omitempty"` diff --git a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/change_qan_my_sql_slowlog_agent_responses.go b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/change_qan_my_sql_slowlog_agent_responses.go index 1c9953a5..9c26782f 100644 --- a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/change_qan_my_sql_slowlog_agent_responses.go +++ b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/change_qan_my_sql_slowlog_agent_responses.go @@ -292,12 +292,18 @@ type ChangeQANMySQLSlowlogAgentOKBodyQANMysqlSlowlogAgent struct { // Desired Agent status: enabled (false) or disabled (true). Disabled bool `json:"disabled,omitempty"` + // Slowlog file is rotated at this size if > 0. + MaxSlowlogFileSize string `json:"max_slowlog_file_size,omitempty"` + // MySQL password for getting performance data. Password string `json:"password,omitempty"` // The pmm-agent identifier which runs this instance. PMMAgentID string `json:"pmm_agent_id,omitempty"` + // True if query examples are disabled. + QueryExamplesDisabled bool `json:"query_examples_disabled,omitempty"` + // Service identifier. ServiceID string `json:"service_id,omitempty"` diff --git a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/get_agent_responses.go b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/get_agent_responses.go index 3b35bc2c..d01e651d 100644 --- a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/get_agent_responses.go +++ b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/get_agent_responses.go @@ -1345,6 +1345,9 @@ type GetAgentOKBodyQANMysqlPerfschemaAgent struct { // The pmm-agent identifier which runs this instance. PMMAgentID string `json:"pmm_agent_id,omitempty"` + // True if query examples are disabled. + QueryExamplesDisabled bool `json:"query_examples_disabled,omitempty"` + // Service identifier. ServiceID string `json:"service_id,omitempty"` @@ -1457,12 +1460,18 @@ type GetAgentOKBodyQANMysqlSlowlogAgent struct { // Desired Agent status: enabled (false) or disabled (true). Disabled bool `json:"disabled,omitempty"` + // Slowlog file is rotated at this size if > 0. + MaxSlowlogFileSize string `json:"max_slowlog_file_size,omitempty"` + // MySQL password for getting performance data. Password string `json:"password,omitempty"` // The pmm-agent identifier which runs this instance. PMMAgentID string `json:"pmm_agent_id,omitempty"` + // True if query examples are disabled. + QueryExamplesDisabled bool `json:"query_examples_disabled,omitempty"` + // Service identifier. ServiceID string `json:"service_id,omitempty"` diff --git a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/list_agents_responses.go b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/list_agents_responses.go index f03640aa..f2214ed3 100644 --- a/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/list_agents_responses.go +++ b/vendor/github.com/percona/pmm/api/inventorypb/json/client/agents/list_agents_responses.go @@ -1436,6 +1436,9 @@ type QANMysqlPerfschemaAgentItems0 struct { // The pmm-agent identifier which runs this instance. PMMAgentID string `json:"pmm_agent_id,omitempty"` + // True if query examples are disabled. + QueryExamplesDisabled bool `json:"query_examples_disabled,omitempty"` + // Service identifier. ServiceID string `json:"service_id,omitempty"` @@ -1548,12 +1551,18 @@ type QANMysqlSlowlogAgentItems0 struct { // Desired Agent status: enabled (false) or disabled (true). Disabled bool `json:"disabled,omitempty"` + // Slowlog file is rotated at this size if > 0. + MaxSlowlogFileSize string `json:"max_slowlog_file_size,omitempty"` + // MySQL password for getting performance data. Password string `json:"password,omitempty"` // The pmm-agent identifier which runs this instance. PMMAgentID string `json:"pmm_agent_id,omitempty"` + // True if query examples are disabled. + QueryExamplesDisabled bool `json:"query_examples_disabled,omitempty"` + // Service identifier. ServiceID string `json:"service_id,omitempty"` diff --git a/vendor/github.com/percona/pmm/api/managementpb/json/client/actions/actions_client.go b/vendor/github.com/percona/pmm/api/managementpb/json/client/actions/actions_client.go index 593be2f1..c27fe256 100644 --- a/vendor/github.com/percona/pmm/api/managementpb/json/client/actions/actions_client.go +++ b/vendor/github.com/percona/pmm/api/managementpb/json/client/actions/actions_client.go @@ -90,39 +90,6 @@ func (a *Client) GetAction(params *GetActionParams) (*GetActionOK, error) { return nil, runtime.NewAPIError("unexpected success response: content available as default response in error", unexpectedSuccess, unexpectedSuccess.Code()) } -/* -StartMongoDBExplainAction starts mongo DB explain action starts postgre SQL e x p l a i n action -*/ -func (a *Client) StartMongoDBExplainAction(params *StartMongoDBExplainActionParams) (*StartMongoDBExplainActionOK, error) { - // TODO: Validate the params before sending - if params == nil { - params = NewStartMongoDBExplainActionParams() - } - - result, err := a.transport.Submit(&runtime.ClientOperation{ - ID: "StartMongoDBExplainAction", - Method: "POST", - PathPattern: "/v0/management/Actions/StartMongoDBExplain", - ProducesMediaTypes: []string{"application/json"}, - ConsumesMediaTypes: []string{"application/json"}, - Schemes: []string{"http", "https"}, - Params: params, - Reader: &StartMongoDBExplainActionReader{formats: a.formats}, - Context: params.Context, - Client: params.HTTPClient, - }) - if err != nil { - return nil, err - } - success, ok := result.(*StartMongoDBExplainActionOK) - if ok { - return success, nil - } - // unexpected success response - unexpectedSuccess := result.(*StartMongoDBExplainActionDefault) - return nil, runtime.NewAPIError("unexpected success response: content available as default response in error", unexpectedSuccess, unexpectedSuccess.Code()) -} - /* StartMySQLExplainAction starts my SQL explain action starts my SQL e x p l a i n action with traditional output */ diff --git a/vendor/github.com/percona/pmm/api/managementpb/json/client/mongo_db/add_mongo_db_responses.go b/vendor/github.com/percona/pmm/api/managementpb/json/client/mongo_db/add_mongo_db_responses.go index 58a544e5..d1b092ed 100644 --- a/vendor/github.com/percona/pmm/api/managementpb/json/client/mongo_db/add_mongo_db_responses.go +++ b/vendor/github.com/percona/pmm/api/managementpb/json/client/mongo_db/add_mongo_db_responses.go @@ -124,6 +124,9 @@ swagger:model AddMongoDBBody */ type AddMongoDBBody struct { + // add node + AddNode *AddMongoDBParamsBodyAddNode `json:"add_node,omitempty"` + // Node and Service access address (DNS name or IP). Required. Address string `json:"address,omitempty"` @@ -136,9 +139,14 @@ type AddMongoDBBody struct { // Environment name. Environment string `json:"environment,omitempty"` - // Node identifier on which a service is been running. Required. + // Node identifier on which a service is been running. + // Exactly one of these parameters should be present: node_id, node_name, add_node. NodeID string `json:"node_id,omitempty"` + // Node name on which a service is been running. + // Exactly one of these parameters should be present: node_id, node_name, add_node. + NodeName string `json:"node_name,omitempty"` + // MongoDB password for exporter and QAN agent access. Password string `json:"password,omitempty"` @@ -166,6 +174,33 @@ type AddMongoDBBody struct { // Validate validates this add mongo DB body func (o *AddMongoDBBody) Validate(formats strfmt.Registry) error { + var res []error + + if err := o.validateAddNode(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +func (o *AddMongoDBBody) validateAddNode(formats strfmt.Registry) error { + + if swag.IsZero(o.AddNode) { // not required + return nil + } + + if o.AddNode != nil { + if err := o.AddNode.Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("body" + "." + "add_node") + } + return err + } + } + return nil } @@ -628,3 +663,125 @@ func (o *AddMongoDBOKBodyService) UnmarshalBinary(b []byte) error { *o = res return nil } + +/*AddMongoDBParamsBodyAddNode AddNodeParams is a params to add new node to inventory while adding new service. +swagger:model AddMongoDBParamsBodyAddNode +*/ +type AddMongoDBParamsBodyAddNode struct { + + // Node availability zone. + Az string `json:"az,omitempty"` + + // Container identifier. If specified, must be a unique Docker container identifier. + ContainerID string `json:"container_id,omitempty"` + + // Container name. + ContainerName string `json:"container_name,omitempty"` + + // Custom user-assigned labels. + CustomLabels map[string]string `json:"custom_labels,omitempty"` + + // Linux distribution name and version. + Distro string `json:"distro,omitempty"` + + // Linux machine-id. + // Must be unique across all Generic Nodes if specified. + MachineID string `json:"machine_id,omitempty"` + + // Node model. + NodeModel string `json:"node_model,omitempty"` + + // Unique across all Nodes user-defined name. Can't be changed. + NodeName string `json:"node_name,omitempty"` + + // NodeType describes supported Node types. + // Enum: [NODE_TYPE_INVALID GENERIC_NODE CONTAINER_NODE REMOTE_NODE REMOTE_AMAZON_RDS_NODE] + NodeType *string `json:"node_type,omitempty"` + + // Node region. + Region string `json:"region,omitempty"` +} + +// Validate validates this add mongo DB params body add node +func (o *AddMongoDBParamsBodyAddNode) Validate(formats strfmt.Registry) error { + var res []error + + if err := o.validateNodeType(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +var addMongoDbParamsBodyAddNodeTypeNodeTypePropEnum []interface{} + +func init() { + var res []string + if err := json.Unmarshal([]byte(`["NODE_TYPE_INVALID","GENERIC_NODE","CONTAINER_NODE","REMOTE_NODE","REMOTE_AMAZON_RDS_NODE"]`), &res); err != nil { + panic(err) + } + for _, v := range res { + addMongoDbParamsBodyAddNodeTypeNodeTypePropEnum = append(addMongoDbParamsBodyAddNodeTypeNodeTypePropEnum, v) + } +} + +const ( + + // AddMongoDBParamsBodyAddNodeNodeTypeNODETYPEINVALID captures enum value "NODE_TYPE_INVALID" + AddMongoDBParamsBodyAddNodeNodeTypeNODETYPEINVALID string = "NODE_TYPE_INVALID" + + // AddMongoDBParamsBodyAddNodeNodeTypeGENERICNODE captures enum value "GENERIC_NODE" + AddMongoDBParamsBodyAddNodeNodeTypeGENERICNODE string = "GENERIC_NODE" + + // AddMongoDBParamsBodyAddNodeNodeTypeCONTAINERNODE captures enum value "CONTAINER_NODE" + AddMongoDBParamsBodyAddNodeNodeTypeCONTAINERNODE string = "CONTAINER_NODE" + + // AddMongoDBParamsBodyAddNodeNodeTypeREMOTENODE captures enum value "REMOTE_NODE" + AddMongoDBParamsBodyAddNodeNodeTypeREMOTENODE string = "REMOTE_NODE" + + // AddMongoDBParamsBodyAddNodeNodeTypeREMOTEAMAZONRDSNODE captures enum value "REMOTE_AMAZON_RDS_NODE" + AddMongoDBParamsBodyAddNodeNodeTypeREMOTEAMAZONRDSNODE string = "REMOTE_AMAZON_RDS_NODE" +) + +// prop value enum +func (o *AddMongoDBParamsBodyAddNode) validateNodeTypeEnum(path, location string, value string) error { + if err := validate.Enum(path, location, value, addMongoDbParamsBodyAddNodeTypeNodeTypePropEnum); err != nil { + return err + } + return nil +} + +func (o *AddMongoDBParamsBodyAddNode) validateNodeType(formats strfmt.Registry) error { + + if swag.IsZero(o.NodeType) { // not required + return nil + } + + // value enum + if err := o.validateNodeTypeEnum("body"+"."+"add_node"+"."+"node_type", "body", *o.NodeType); err != nil { + return err + } + + return nil +} + +// MarshalBinary interface implementation +func (o *AddMongoDBParamsBodyAddNode) MarshalBinary() ([]byte, error) { + if o == nil { + return nil, nil + } + return swag.WriteJSON(o) +} + +// UnmarshalBinary interface implementation +func (o *AddMongoDBParamsBodyAddNode) UnmarshalBinary(b []byte) error { + var res AddMongoDBParamsBodyAddNode + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *o = res + return nil +} diff --git a/vendor/github.com/percona/pmm/api/managementpb/json/client/my_sql/add_my_sql_responses.go b/vendor/github.com/percona/pmm/api/managementpb/json/client/my_sql/add_my_sql_responses.go index 64b13abf..08c96b0c 100644 --- a/vendor/github.com/percona/pmm/api/managementpb/json/client/my_sql/add_my_sql_responses.go +++ b/vendor/github.com/percona/pmm/api/managementpb/json/client/my_sql/add_my_sql_responses.go @@ -136,9 +136,16 @@ type AddMySQLBody struct { // Custom user-assigned labels. CustomLabels map[string]string `json:"custom_labels,omitempty"` + // Disable query examples. + DisableQueryExamples bool `json:"disable_query_examples,omitempty"` + // Environment name. Environment string `json:"environment,omitempty"` + // If qan-mysql-slowlog-agent is added, slowlog file is rotated at this size if > 0. + // If zero, default value 1GB is used. Use negative value to disable rotation. + MaxSlowlogFileSize string `json:"max_slowlog_file_size,omitempty"` + // Node identifier on which a service is been running. // Exactly one of these parameters should be present: node_id, node_name, add_node. NodeID string `json:"node_id,omitempty"` @@ -538,6 +545,9 @@ type AddMySQLOKBodyQANMysqlPerfschema struct { // The pmm-agent identifier which runs this instance. PMMAgentID string `json:"pmm_agent_id,omitempty"` + // True if query examples are disabled. + QueryExamplesDisabled bool `json:"query_examples_disabled,omitempty"` + // Service identifier. ServiceID string `json:"service_id,omitempty"` @@ -650,12 +660,18 @@ type AddMySQLOKBodyQANMysqlSlowlog struct { // Desired Agent status: enabled (false) or disabled (true). Disabled bool `json:"disabled,omitempty"` + // Slowlog file is rotated at this size if > 0. + MaxSlowlogFileSize string `json:"max_slowlog_file_size,omitempty"` + // MySQL password for getting performance data. Password string `json:"password,omitempty"` // The pmm-agent identifier which runs this instance. PMMAgentID string `json:"pmm_agent_id,omitempty"` + // True if query examples are disabled. + QueryExamplesDisabled bool `json:"query_examples_disabled,omitempty"` + // Service identifier. ServiceID string `json:"service_id,omitempty"` diff --git a/vendor/github.com/percona/pmm/api/managementpb/json/client/postgre_sql/add_postgre_sql_responses.go b/vendor/github.com/percona/pmm/api/managementpb/json/client/postgre_sql/add_postgre_sql_responses.go index bbc25122..9af8ab02 100644 --- a/vendor/github.com/percona/pmm/api/managementpb/json/client/postgre_sql/add_postgre_sql_responses.go +++ b/vendor/github.com/percona/pmm/api/managementpb/json/client/postgre_sql/add_postgre_sql_responses.go @@ -124,6 +124,9 @@ swagger:model AddPostgreSQLBody */ type AddPostgreSQLBody struct { + // add node + AddNode *AddPostgreSQLParamsBodyAddNode `json:"add_node,omitempty"` + // Node and Service access address (DNS name or IP). Required. Address string `json:"address,omitempty"` @@ -136,9 +139,14 @@ type AddPostgreSQLBody struct { // Environment name. Environment string `json:"environment,omitempty"` - // Node identifier on which a service is been running. Required. + // Node identifier on which a service is been running. + // Exactly one of these parameters should be present: node_id, node_name, add_node. NodeID string `json:"node_id,omitempty"` + // Node name on which a service is been running. + // Exactly one of these parameters should be present: node_id, node_name, add_node. + NodeName string `json:"node_name,omitempty"` + // PostgreSQL password for scraping metrics. Password string `json:"password,omitempty"` @@ -174,6 +182,33 @@ type AddPostgreSQLBody struct { // Validate validates this add postgre SQL body func (o *AddPostgreSQLBody) Validate(formats strfmt.Registry) error { + var res []error + + if err := o.validateAddNode(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +func (o *AddPostgreSQLBody) validateAddNode(formats strfmt.Registry) error { + + if swag.IsZero(o.AddNode) { // not required + return nil + } + + if o.AddNode != nil { + if err := o.AddNode.Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("body" + "." + "add_node") + } + return err + } + } + return nil } @@ -642,3 +677,125 @@ func (o *AddPostgreSQLOKBodyService) UnmarshalBinary(b []byte) error { *o = res return nil } + +/*AddPostgreSQLParamsBodyAddNode AddNodeParams is a params to add new node to inventory while adding new service. +swagger:model AddPostgreSQLParamsBodyAddNode +*/ +type AddPostgreSQLParamsBodyAddNode struct { + + // Node availability zone. + Az string `json:"az,omitempty"` + + // Container identifier. If specified, must be a unique Docker container identifier. + ContainerID string `json:"container_id,omitempty"` + + // Container name. + ContainerName string `json:"container_name,omitempty"` + + // Custom user-assigned labels. + CustomLabels map[string]string `json:"custom_labels,omitempty"` + + // Linux distribution name and version. + Distro string `json:"distro,omitempty"` + + // Linux machine-id. + // Must be unique across all Generic Nodes if specified. + MachineID string `json:"machine_id,omitempty"` + + // Node model. + NodeModel string `json:"node_model,omitempty"` + + // Unique across all Nodes user-defined name. Can't be changed. + NodeName string `json:"node_name,omitempty"` + + // NodeType describes supported Node types. + // Enum: [NODE_TYPE_INVALID GENERIC_NODE CONTAINER_NODE REMOTE_NODE REMOTE_AMAZON_RDS_NODE] + NodeType *string `json:"node_type,omitempty"` + + // Node region. + Region string `json:"region,omitempty"` +} + +// Validate validates this add postgre SQL params body add node +func (o *AddPostgreSQLParamsBodyAddNode) Validate(formats strfmt.Registry) error { + var res []error + + if err := o.validateNodeType(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +var addPostgreSqlParamsBodyAddNodeTypeNodeTypePropEnum []interface{} + +func init() { + var res []string + if err := json.Unmarshal([]byte(`["NODE_TYPE_INVALID","GENERIC_NODE","CONTAINER_NODE","REMOTE_NODE","REMOTE_AMAZON_RDS_NODE"]`), &res); err != nil { + panic(err) + } + for _, v := range res { + addPostgreSqlParamsBodyAddNodeTypeNodeTypePropEnum = append(addPostgreSqlParamsBodyAddNodeTypeNodeTypePropEnum, v) + } +} + +const ( + + // AddPostgreSQLParamsBodyAddNodeNodeTypeNODETYPEINVALID captures enum value "NODE_TYPE_INVALID" + AddPostgreSQLParamsBodyAddNodeNodeTypeNODETYPEINVALID string = "NODE_TYPE_INVALID" + + // AddPostgreSQLParamsBodyAddNodeNodeTypeGENERICNODE captures enum value "GENERIC_NODE" + AddPostgreSQLParamsBodyAddNodeNodeTypeGENERICNODE string = "GENERIC_NODE" + + // AddPostgreSQLParamsBodyAddNodeNodeTypeCONTAINERNODE captures enum value "CONTAINER_NODE" + AddPostgreSQLParamsBodyAddNodeNodeTypeCONTAINERNODE string = "CONTAINER_NODE" + + // AddPostgreSQLParamsBodyAddNodeNodeTypeREMOTENODE captures enum value "REMOTE_NODE" + AddPostgreSQLParamsBodyAddNodeNodeTypeREMOTENODE string = "REMOTE_NODE" + + // AddPostgreSQLParamsBodyAddNodeNodeTypeREMOTEAMAZONRDSNODE captures enum value "REMOTE_AMAZON_RDS_NODE" + AddPostgreSQLParamsBodyAddNodeNodeTypeREMOTEAMAZONRDSNODE string = "REMOTE_AMAZON_RDS_NODE" +) + +// prop value enum +func (o *AddPostgreSQLParamsBodyAddNode) validateNodeTypeEnum(path, location string, value string) error { + if err := validate.Enum(path, location, value, addPostgreSqlParamsBodyAddNodeTypeNodeTypePropEnum); err != nil { + return err + } + return nil +} + +func (o *AddPostgreSQLParamsBodyAddNode) validateNodeType(formats strfmt.Registry) error { + + if swag.IsZero(o.NodeType) { // not required + return nil + } + + // value enum + if err := o.validateNodeTypeEnum("body"+"."+"add_node"+"."+"node_type", "body", *o.NodeType); err != nil { + return err + } + + return nil +} + +// MarshalBinary interface implementation +func (o *AddPostgreSQLParamsBodyAddNode) MarshalBinary() ([]byte, error) { + if o == nil { + return nil, nil + } + return swag.WriteJSON(o) +} + +// UnmarshalBinary interface implementation +func (o *AddPostgreSQLParamsBodyAddNode) UnmarshalBinary(b []byte) error { + var res AddPostgreSQLParamsBodyAddNode + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *o = res + return nil +} diff --git a/vendor/github.com/percona/pmm/api/managementpb/json/client/proxy_sql/add_proxy_sql_responses.go b/vendor/github.com/percona/pmm/api/managementpb/json/client/proxy_sql/add_proxy_sql_responses.go index ca10a37e..476286e7 100644 --- a/vendor/github.com/percona/pmm/api/managementpb/json/client/proxy_sql/add_proxy_sql_responses.go +++ b/vendor/github.com/percona/pmm/api/managementpb/json/client/proxy_sql/add_proxy_sql_responses.go @@ -124,6 +124,9 @@ swagger:model AddProxySQLBody */ type AddProxySQLBody struct { + // add node + AddNode *AddProxySQLParamsBodyAddNode `json:"add_node,omitempty"` + // Node and Service access address (DNS name or IP). Required. Address string `json:"address,omitempty"` @@ -136,9 +139,14 @@ type AddProxySQLBody struct { // Environment name. Environment string `json:"environment,omitempty"` - // Node identifier on which a service is been running. Required. + // Node identifier on which a service is been running. + // Exactly one of these parameters should be present: node_id, node_name, add_node. NodeID string `json:"node_id,omitempty"` + // Node name on which a service is been running. + // Exactly one of these parameters should be present: node_id, node_name, add_node. + NodeName string `json:"node_name,omitempty"` + // ProxySQL password for scraping metrics. Password string `json:"password,omitempty"` @@ -163,6 +171,33 @@ type AddProxySQLBody struct { // Validate validates this add proxy SQL body func (o *AddProxySQLBody) Validate(formats strfmt.Registry) error { + var res []error + + if err := o.validateAddNode(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +func (o *AddProxySQLBody) validateAddNode(formats strfmt.Registry) error { + + if swag.IsZero(o.AddNode) { // not required + return nil + } + + if o.AddNode != nil { + if err := o.AddNode.Validate(formats); err != nil { + if ve, ok := err.(*errors.Validation); ok { + return ve.ValidateName("body" + "." + "add_node") + } + return err + } + } + return nil } @@ -482,3 +517,125 @@ func (o *AddProxySQLOKBodyService) UnmarshalBinary(b []byte) error { *o = res return nil } + +/*AddProxySQLParamsBodyAddNode AddNodeParams is a params to add new node to inventory while adding new service. +swagger:model AddProxySQLParamsBodyAddNode +*/ +type AddProxySQLParamsBodyAddNode struct { + + // Node availability zone. + Az string `json:"az,omitempty"` + + // Container identifier. If specified, must be a unique Docker container identifier. + ContainerID string `json:"container_id,omitempty"` + + // Container name. + ContainerName string `json:"container_name,omitempty"` + + // Custom user-assigned labels. + CustomLabels map[string]string `json:"custom_labels,omitempty"` + + // Linux distribution name and version. + Distro string `json:"distro,omitempty"` + + // Linux machine-id. + // Must be unique across all Generic Nodes if specified. + MachineID string `json:"machine_id,omitempty"` + + // Node model. + NodeModel string `json:"node_model,omitempty"` + + // Unique across all Nodes user-defined name. Can't be changed. + NodeName string `json:"node_name,omitempty"` + + // NodeType describes supported Node types. + // Enum: [NODE_TYPE_INVALID GENERIC_NODE CONTAINER_NODE REMOTE_NODE REMOTE_AMAZON_RDS_NODE] + NodeType *string `json:"node_type,omitempty"` + + // Node region. + Region string `json:"region,omitempty"` +} + +// Validate validates this add proxy SQL params body add node +func (o *AddProxySQLParamsBodyAddNode) Validate(formats strfmt.Registry) error { + var res []error + + if err := o.validateNodeType(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +var addProxySqlParamsBodyAddNodeTypeNodeTypePropEnum []interface{} + +func init() { + var res []string + if err := json.Unmarshal([]byte(`["NODE_TYPE_INVALID","GENERIC_NODE","CONTAINER_NODE","REMOTE_NODE","REMOTE_AMAZON_RDS_NODE"]`), &res); err != nil { + panic(err) + } + for _, v := range res { + addProxySqlParamsBodyAddNodeTypeNodeTypePropEnum = append(addProxySqlParamsBodyAddNodeTypeNodeTypePropEnum, v) + } +} + +const ( + + // AddProxySQLParamsBodyAddNodeNodeTypeNODETYPEINVALID captures enum value "NODE_TYPE_INVALID" + AddProxySQLParamsBodyAddNodeNodeTypeNODETYPEINVALID string = "NODE_TYPE_INVALID" + + // AddProxySQLParamsBodyAddNodeNodeTypeGENERICNODE captures enum value "GENERIC_NODE" + AddProxySQLParamsBodyAddNodeNodeTypeGENERICNODE string = "GENERIC_NODE" + + // AddProxySQLParamsBodyAddNodeNodeTypeCONTAINERNODE captures enum value "CONTAINER_NODE" + AddProxySQLParamsBodyAddNodeNodeTypeCONTAINERNODE string = "CONTAINER_NODE" + + // AddProxySQLParamsBodyAddNodeNodeTypeREMOTENODE captures enum value "REMOTE_NODE" + AddProxySQLParamsBodyAddNodeNodeTypeREMOTENODE string = "REMOTE_NODE" + + // AddProxySQLParamsBodyAddNodeNodeTypeREMOTEAMAZONRDSNODE captures enum value "REMOTE_AMAZON_RDS_NODE" + AddProxySQLParamsBodyAddNodeNodeTypeREMOTEAMAZONRDSNODE string = "REMOTE_AMAZON_RDS_NODE" +) + +// prop value enum +func (o *AddProxySQLParamsBodyAddNode) validateNodeTypeEnum(path, location string, value string) error { + if err := validate.Enum(path, location, value, addProxySqlParamsBodyAddNodeTypeNodeTypePropEnum); err != nil { + return err + } + return nil +} + +func (o *AddProxySQLParamsBodyAddNode) validateNodeType(formats strfmt.Registry) error { + + if swag.IsZero(o.NodeType) { // not required + return nil + } + + // value enum + if err := o.validateNodeTypeEnum("body"+"."+"add_node"+"."+"node_type", "body", *o.NodeType); err != nil { + return err + } + + return nil +} + +// MarshalBinary interface implementation +func (o *AddProxySQLParamsBodyAddNode) MarshalBinary() ([]byte, error) { + if o == nil { + return nil, nil + } + return swag.WriteJSON(o) +} + +// UnmarshalBinary interface implementation +func (o *AddProxySQLParamsBodyAddNode) UnmarshalBinary(b []byte) error { + var res AddProxySQLParamsBodyAddNode + if err := swag.ReadJSON(b, &res); err != nil { + return err + } + *o = res + return nil +} diff --git a/vendor/gopkg.in/mgo.v2/LICENSE b/vendor/gopkg.in/mgo.v2/LICENSE new file mode 100644 index 00000000..770c7672 --- /dev/null +++ b/vendor/gopkg.in/mgo.v2/LICENSE @@ -0,0 +1,25 @@ +mgo - MongoDB driver for Go + +Copyright (c) 2010-2013 - Gustavo Niemeyer + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/gopkg.in/mgo.v2/bson/LICENSE b/vendor/gopkg.in/mgo.v2/bson/LICENSE new file mode 100644 index 00000000..89032601 --- /dev/null +++ b/vendor/gopkg.in/mgo.v2/bson/LICENSE @@ -0,0 +1,25 @@ +BSON library for Go + +Copyright (c) 2010-2012 - Gustavo Niemeyer + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/gopkg.in/mgo.v2/bson/bson.go b/vendor/gopkg.in/mgo.v2/bson/bson.go new file mode 100644 index 00000000..90ec7e8a --- /dev/null +++ b/vendor/gopkg.in/mgo.v2/bson/bson.go @@ -0,0 +1,734 @@ +// BSON library for Go +// +// Copyright (c) 2010-2012 - Gustavo Niemeyer +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Package bson is an implementation of the BSON specification for Go: +// +// http://bsonspec.org +// +// It was created as part of the mgo MongoDB driver for Go, but is standalone +// and may be used on its own without the driver. +package bson + +import ( + "bytes" + "crypto/md5" + "encoding/binary" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "os" + "reflect" + "runtime" + "strings" + "sync" + "sync/atomic" + "time" +) + +// -------------------------------------------------------------------------- +// The public API. + +// A value implementing the bson.Getter interface will have its GetBSON +// method called when the given value has to be marshalled, and the result +// of this method will be marshaled in place of the actual object. +// +// If GetBSON returns return a non-nil error, the marshalling procedure +// will stop and error out with the provided value. +type Getter interface { + GetBSON() (interface{}, error) +} + +// A value implementing the bson.Setter interface will receive the BSON +// value via the SetBSON method during unmarshaling, and the object +// itself will not be changed as usual. +// +// If setting the value works, the method should return nil or alternatively +// bson.SetZero to set the respective field to its zero value (nil for +// pointer types). If SetBSON returns a value of type bson.TypeError, the +// BSON value will be omitted from a map or slice being decoded and the +// unmarshalling will continue. If it returns any other non-nil error, the +// unmarshalling procedure will stop and error out with the provided value. +// +// This interface is generally useful in pointer receivers, since the method +// will want to change the receiver. A type field that implements the Setter +// interface doesn't have to be a pointer, though. +// +// Unlike the usual behavior, unmarshalling onto a value that implements a +// Setter interface will NOT reset the value to its zero state. This allows +// the value to decide by itself how to be unmarshalled. +// +// For example: +// +// type MyString string +// +// func (s *MyString) SetBSON(raw bson.Raw) error { +// return raw.Unmarshal(s) +// } +// +type Setter interface { + SetBSON(raw Raw) error +} + +// SetZero may be returned from a SetBSON method to have the value set to +// its respective zero value. When used in pointer values, this will set the +// field to nil rather than to the pre-allocated value. +var SetZero = errors.New("set to zero") + +// M is a convenient alias for a map[string]interface{} map, useful for +// dealing with BSON in a native way. For instance: +// +// bson.M{"a": 1, "b": true} +// +// There's no special handling for this type in addition to what's done anyway +// for an equivalent map type. Elements in the map will be dumped in an +// undefined ordered. See also the bson.D type for an ordered alternative. +type M map[string]interface{} + +// D represents a BSON document containing ordered elements. For example: +// +// bson.D{{"a", 1}, {"b", true}} +// +// In some situations, such as when creating indexes for MongoDB, the order in +// which the elements are defined is important. If the order is not important, +// using a map is generally more comfortable. See bson.M and bson.RawD. +type D []DocElem + +// DocElem is an element of the bson.D document representation. +type DocElem struct { + Name string + Value interface{} +} + +// Map returns a map out of the ordered element name/value pairs in d. +func (d D) Map() (m M) { + m = make(M, len(d)) + for _, item := range d { + m[item.Name] = item.Value + } + return m +} + +// The Raw type represents raw unprocessed BSON documents and elements. +// Kind is the kind of element as defined per the BSON specification, and +// Data is the raw unprocessed data for the respective element. +// Using this type it is possible to unmarshal or marshal values partially. +// +// Relevant documentation: +// +// http://bsonspec.org/#/specification +// +type Raw struct { + Kind byte + Data []byte +} + +// RawD represents a BSON document containing raw unprocessed elements. +// This low-level representation may be useful when lazily processing +// documents of uncertain content, or when manipulating the raw content +// documents in general. +type RawD []RawDocElem + +// See the RawD type. +type RawDocElem struct { + Name string + Value Raw +} + +// ObjectId is a unique ID identifying a BSON value. It must be exactly 12 bytes +// long. MongoDB objects by default have such a property set in their "_id" +// property. +// +// http://www.mongodb.org/display/DOCS/Object+IDs +type ObjectId string + +// ObjectIdHex returns an ObjectId from the provided hex representation. +// Calling this function with an invalid hex representation will +// cause a runtime panic. See the IsObjectIdHex function. +func ObjectIdHex(s string) ObjectId { + d, err := hex.DecodeString(s) + if err != nil || len(d) != 12 { + panic(fmt.Sprintf("invalid input to ObjectIdHex: %q", s)) + } + return ObjectId(d) +} + +// IsObjectIdHex returns whether s is a valid hex representation of +// an ObjectId. See the ObjectIdHex function. +func IsObjectIdHex(s string) bool { + if len(s) != 24 { + return false + } + _, err := hex.DecodeString(s) + return err == nil +} + +// objectIdCounter is atomically incremented when generating a new ObjectId +// using NewObjectId() function. It's used as a counter part of an id. +var objectIdCounter uint32 = readRandomUint32() + +// readRandomUint32 returns a random objectIdCounter. +func readRandomUint32() uint32 { + // We've found systems hanging in this function due to lack of entropy. + // The randomness of these bytes is just preventing nearby clashes, so + // just look at the time. + return uint32(time.Now().UnixNano()) +} + +// machineId stores machine id generated once and used in subsequent calls +// to NewObjectId function. +var machineId = readMachineId() +var processId = os.Getpid() + +// readMachineId generates and returns a machine id. +// If this function fails to get the hostname it will cause a runtime error. +func readMachineId() []byte { + var sum [3]byte + id := sum[:] + hostname, err1 := os.Hostname() + if err1 != nil { + n := uint32(time.Now().UnixNano()) + sum[0] = byte(n >> 0) + sum[1] = byte(n >> 8) + sum[2] = byte(n >> 16) + return id + } + hw := md5.New() + hw.Write([]byte(hostname)) + copy(id, hw.Sum(nil)) + return id +} + +// NewObjectId returns a new unique ObjectId. +func NewObjectId() ObjectId { + var b [12]byte + // Timestamp, 4 bytes, big endian + binary.BigEndian.PutUint32(b[:], uint32(time.Now().Unix())) + // Machine, first 3 bytes of md5(hostname) + b[4] = machineId[0] + b[5] = machineId[1] + b[6] = machineId[2] + // Pid, 2 bytes, specs don't specify endianness, but we use big endian. + b[7] = byte(processId >> 8) + b[8] = byte(processId) + // Increment, 3 bytes, big endian + i := atomic.AddUint32(&objectIdCounter, 1) + b[9] = byte(i >> 16) + b[10] = byte(i >> 8) + b[11] = byte(i) + return ObjectId(b[:]) +} + +// NewObjectIdWithTime returns a dummy ObjectId with the timestamp part filled +// with the provided number of seconds from epoch UTC, and all other parts +// filled with zeroes. It's not safe to insert a document with an id generated +// by this method, it is useful only for queries to find documents with ids +// generated before or after the specified timestamp. +func NewObjectIdWithTime(t time.Time) ObjectId { + var b [12]byte + binary.BigEndian.PutUint32(b[:4], uint32(t.Unix())) + return ObjectId(string(b[:])) +} + +// String returns a hex string representation of the id. +// Example: ObjectIdHex("4d88e15b60f486e428412dc9"). +func (id ObjectId) String() string { + return fmt.Sprintf(`ObjectIdHex("%x")`, string(id)) +} + +// Hex returns a hex representation of the ObjectId. +func (id ObjectId) Hex() string { + return hex.EncodeToString([]byte(id)) +} + +// MarshalJSON turns a bson.ObjectId into a json.Marshaller. +func (id ObjectId) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%x"`, string(id))), nil +} + +var nullBytes = []byte("null") + +// UnmarshalJSON turns *bson.ObjectId into a json.Unmarshaller. +func (id *ObjectId) UnmarshalJSON(data []byte) error { + if len(data) > 0 && (data[0] == '{' || data[0] == 'O') { + var v struct { + Id json.RawMessage `json:"$oid"` + Func struct { + Id json.RawMessage + } `json:"$oidFunc"` + } + err := jdec(data, &v) + if err == nil { + if len(v.Id) > 0 { + data = []byte(v.Id) + } else { + data = []byte(v.Func.Id) + } + } + } + if len(data) == 2 && data[0] == '"' && data[1] == '"' || bytes.Equal(data, nullBytes) { + *id = "" + return nil + } + if len(data) != 26 || data[0] != '"' || data[25] != '"' { + return errors.New(fmt.Sprintf("invalid ObjectId in JSON: %s", string(data))) + } + var buf [12]byte + _, err := hex.Decode(buf[:], data[1:25]) + if err != nil { + return errors.New(fmt.Sprintf("invalid ObjectId in JSON: %s (%s)", string(data), err)) + } + *id = ObjectId(string(buf[:])) + return nil +} + +// MarshalText turns bson.ObjectId into an encoding.TextMarshaler. +func (id ObjectId) MarshalText() ([]byte, error) { + return []byte(fmt.Sprintf("%x", string(id))), nil +} + +// UnmarshalText turns *bson.ObjectId into an encoding.TextUnmarshaler. +func (id *ObjectId) UnmarshalText(data []byte) error { + if len(data) == 1 && data[0] == ' ' || len(data) == 0 { + *id = "" + return nil + } + if len(data) != 24 { + return fmt.Errorf("invalid ObjectId: %s", data) + } + var buf [12]byte + _, err := hex.Decode(buf[:], data[:]) + if err != nil { + return fmt.Errorf("invalid ObjectId: %s (%s)", data, err) + } + *id = ObjectId(string(buf[:])) + return nil +} + +// Valid returns true if id is valid. A valid id must contain exactly 12 bytes. +func (id ObjectId) Valid() bool { + return len(id) == 12 +} + +// byteSlice returns byte slice of id from start to end. +// Calling this function with an invalid id will cause a runtime panic. +func (id ObjectId) byteSlice(start, end int) []byte { + if len(id) != 12 { + panic(fmt.Sprintf("invalid ObjectId: %q", string(id))) + } + return []byte(string(id)[start:end]) +} + +// Time returns the timestamp part of the id. +// It's a runtime error to call this method with an invalid id. +func (id ObjectId) Time() time.Time { + // First 4 bytes of ObjectId is 32-bit big-endian seconds from epoch. + secs := int64(binary.BigEndian.Uint32(id.byteSlice(0, 4))) + return time.Unix(secs, 0) +} + +// Machine returns the 3-byte machine id part of the id. +// It's a runtime error to call this method with an invalid id. +func (id ObjectId) Machine() []byte { + return id.byteSlice(4, 7) +} + +// Pid returns the process id part of the id. +// It's a runtime error to call this method with an invalid id. +func (id ObjectId) Pid() uint16 { + return binary.BigEndian.Uint16(id.byteSlice(7, 9)) +} + +// Counter returns the incrementing value part of the id. +// It's a runtime error to call this method with an invalid id. +func (id ObjectId) Counter() int32 { + b := id.byteSlice(9, 12) + // Counter is stored as big-endian 3-byte value + return int32(uint32(b[0])<<16 | uint32(b[1])<<8 | uint32(b[2])) +} + +// The Symbol type is similar to a string and is used in languages with a +// distinct symbol type. +type Symbol string + +// Now returns the current time with millisecond precision. MongoDB stores +// timestamps with the same precision, so a Time returned from this method +// will not change after a roundtrip to the database. That's the only reason +// why this function exists. Using the time.Now function also works fine +// otherwise. +func Now() time.Time { + return time.Unix(0, time.Now().UnixNano()/1e6*1e6) +} + +// MongoTimestamp is a special internal type used by MongoDB that for some +// strange reason has its own datatype defined in BSON. +type MongoTimestamp int64 + +type orderKey int64 + +// MaxKey is a special value that compares higher than all other possible BSON +// values in a MongoDB database. +var MaxKey = orderKey(1<<63 - 1) + +// MinKey is a special value that compares lower than all other possible BSON +// values in a MongoDB database. +var MinKey = orderKey(-1 << 63) + +type undefined struct{} + +// Undefined represents the undefined BSON value. +var Undefined undefined + +// Binary is a representation for non-standard binary values. Any kind should +// work, but the following are known as of this writing: +// +// 0x00 - Generic. This is decoded as []byte(data), not Binary{0x00, data}. +// 0x01 - Function (!?) +// 0x02 - Obsolete generic. +// 0x03 - UUID +// 0x05 - MD5 +// 0x80 - User defined. +// +type Binary struct { + Kind byte + Data []byte +} + +// RegEx represents a regular expression. The Options field may contain +// individual characters defining the way in which the pattern should be +// applied, and must be sorted. Valid options as of this writing are 'i' for +// case insensitive matching, 'm' for multi-line matching, 'x' for verbose +// mode, 'l' to make \w, \W, and similar be locale-dependent, 's' for dot-all +// mode (a '.' matches everything), and 'u' to make \w, \W, and similar match +// unicode. The value of the Options parameter is not verified before being +// marshaled into the BSON format. +type RegEx struct { + Pattern string + Options string +} + +// JavaScript is a type that holds JavaScript code. If Scope is non-nil, it +// will be marshaled as a mapping from identifiers to values that may be +// used when evaluating the provided Code. +type JavaScript struct { + Code string + Scope interface{} +} + +// DBPointer refers to a document id in a namespace. +// +// This type is deprecated in the BSON specification and should not be used +// except for backwards compatibility with ancient applications. +type DBPointer struct { + Namespace string + Id ObjectId +} + +const initialBufferSize = 64 + +func handleErr(err *error) { + if r := recover(); r != nil { + if _, ok := r.(runtime.Error); ok { + panic(r) + } else if _, ok := r.(externalPanic); ok { + panic(r) + } else if s, ok := r.(string); ok { + *err = errors.New(s) + } else if e, ok := r.(error); ok { + *err = e + } else { + panic(r) + } + } +} + +// Marshal serializes the in value, which may be a map or a struct value. +// In the case of struct values, only exported fields will be serialized, +// and the order of serialized fields will match that of the struct itself. +// The lowercased field name is used as the key for each exported field, +// but this behavior may be changed using the respective field tag. +// The tag may also contain flags to tweak the marshalling behavior for +// the field. The tag formats accepted are: +// +// "[][,[,]]" +// +// `(...) bson:"[][,[,]]" (...)` +// +// The following flags are currently supported: +// +// omitempty Only include the field if it's not set to the zero +// value for the type or to empty slices or maps. +// +// minsize Marshal an int64 value as an int32, if that's feasible +// while preserving the numeric value. +// +// inline Inline the field, which must be a struct or a map, +// causing all of its fields or keys to be processed as if +// they were part of the outer struct. For maps, keys must +// not conflict with the bson keys of other struct fields. +// +// Some examples: +// +// type T struct { +// A bool +// B int "myb" +// C string "myc,omitempty" +// D string `bson:",omitempty" json:"jsonkey"` +// E int64 ",minsize" +// F int64 "myf,omitempty,minsize" +// } +// +func Marshal(in interface{}) (out []byte, err error) { + defer handleErr(&err) + e := &encoder{make([]byte, 0, initialBufferSize)} + e.addDoc(reflect.ValueOf(in)) + return e.out, nil +} + +// Unmarshal deserializes data from in into the out value. The out value +// must be a map, a pointer to a struct, or a pointer to a bson.D value. +// In the case of struct values, only exported fields will be deserialized. +// The lowercased field name is used as the key for each exported field, +// but this behavior may be changed using the respective field tag. +// The tag may also contain flags to tweak the marshalling behavior for +// the field. The tag formats accepted are: +// +// "[][,[,]]" +// +// `(...) bson:"[][,[,]]" (...)` +// +// The following flags are currently supported during unmarshal (see the +// Marshal method for other flags): +// +// inline Inline the field, which must be a struct or a map. +// Inlined structs are handled as if its fields were part +// of the outer struct. An inlined map causes keys that do +// not match any other struct field to be inserted in the +// map rather than being discarded as usual. +// +// The target field or element types of out may not necessarily match +// the BSON values of the provided data. The following conversions are +// made automatically: +// +// - Numeric types are converted if at least the integer part of the +// value would be preserved correctly +// - Bools are converted to numeric types as 1 or 0 +// - Numeric types are converted to bools as true if not 0 or false otherwise +// - Binary and string BSON data is converted to a string, array or byte slice +// +// If the value would not fit the type and cannot be converted, it's +// silently skipped. +// +// Pointer values are initialized when necessary. +func Unmarshal(in []byte, out interface{}) (err error) { + if raw, ok := out.(*Raw); ok { + raw.Kind = 3 + raw.Data = in + return nil + } + defer handleErr(&err) + v := reflect.ValueOf(out) + switch v.Kind() { + case reflect.Ptr: + fallthrough + case reflect.Map: + d := newDecoder(in) + d.readDocTo(v) + case reflect.Struct: + return errors.New("Unmarshal can't deal with struct values. Use a pointer.") + default: + return errors.New("Unmarshal needs a map or a pointer to a struct.") + } + return nil +} + +// Unmarshal deserializes raw into the out value. If the out value type +// is not compatible with raw, a *bson.TypeError is returned. +// +// See the Unmarshal function documentation for more details on the +// unmarshalling process. +func (raw Raw) Unmarshal(out interface{}) (err error) { + defer handleErr(&err) + v := reflect.ValueOf(out) + switch v.Kind() { + case reflect.Ptr: + v = v.Elem() + fallthrough + case reflect.Map: + d := newDecoder(raw.Data) + good := d.readElemTo(v, raw.Kind) + if !good { + return &TypeError{v.Type(), raw.Kind} + } + case reflect.Struct: + return errors.New("Raw Unmarshal can't deal with struct values. Use a pointer.") + default: + return errors.New("Raw Unmarshal needs a map or a valid pointer.") + } + return nil +} + +type TypeError struct { + Type reflect.Type + Kind byte +} + +func (e *TypeError) Error() string { + return fmt.Sprintf("BSON kind 0x%02x isn't compatible with type %s", e.Kind, e.Type.String()) +} + +// -------------------------------------------------------------------------- +// Maintain a mapping of keys to structure field indexes + +type structInfo struct { + FieldsMap map[string]fieldInfo + FieldsList []fieldInfo + InlineMap int + Zero reflect.Value +} + +type fieldInfo struct { + Key string + Num int + OmitEmpty bool + MinSize bool + Inline []int +} + +var structMap = make(map[reflect.Type]*structInfo) +var structMapMutex sync.RWMutex + +type externalPanic string + +func (e externalPanic) String() string { + return string(e) +} + +func getStructInfo(st reflect.Type) (*structInfo, error) { + structMapMutex.RLock() + sinfo, found := structMap[st] + structMapMutex.RUnlock() + if found { + return sinfo, nil + } + n := st.NumField() + fieldsMap := make(map[string]fieldInfo) + fieldsList := make([]fieldInfo, 0, n) + inlineMap := -1 + for i := 0; i != n; i++ { + field := st.Field(i) + if field.PkgPath != "" && !field.Anonymous { + continue // Private field + } + + info := fieldInfo{Num: i} + + tag := field.Tag.Get("bson") + if tag == "" && strings.Index(string(field.Tag), ":") < 0 { + tag = string(field.Tag) + } + if tag == "-" { + continue + } + + inline := false + fields := strings.Split(tag, ",") + if len(fields) > 1 { + for _, flag := range fields[1:] { + switch flag { + case "omitempty": + info.OmitEmpty = true + case "minsize": + info.MinSize = true + case "inline": + inline = true + default: + msg := fmt.Sprintf("Unsupported flag %q in tag %q of type %s", flag, tag, st) + panic(externalPanic(msg)) + } + } + tag = fields[0] + } + + if inline { + switch field.Type.Kind() { + case reflect.Map: + if inlineMap >= 0 { + return nil, errors.New("Multiple ,inline maps in struct " + st.String()) + } + if field.Type.Key() != reflect.TypeOf("") { + return nil, errors.New("Option ,inline needs a map with string keys in struct " + st.String()) + } + inlineMap = info.Num + case reflect.Struct: + sinfo, err := getStructInfo(field.Type) + if err != nil { + return nil, err + } + for _, finfo := range sinfo.FieldsList { + if _, found := fieldsMap[finfo.Key]; found { + msg := "Duplicated key '" + finfo.Key + "' in struct " + st.String() + return nil, errors.New(msg) + } + if finfo.Inline == nil { + finfo.Inline = []int{i, finfo.Num} + } else { + finfo.Inline = append([]int{i}, finfo.Inline...) + } + fieldsMap[finfo.Key] = finfo + fieldsList = append(fieldsList, finfo) + } + default: + panic("Option ,inline needs a struct value or map field") + } + continue + } + + if tag != "" { + info.Key = tag + } else { + info.Key = strings.ToLower(field.Name) + } + + if _, found = fieldsMap[info.Key]; found { + msg := "Duplicated key '" + info.Key + "' in struct " + st.String() + return nil, errors.New(msg) + } + + fieldsList = append(fieldsList, info) + fieldsMap[info.Key] = info + } + sinfo = &structInfo{ + fieldsMap, + fieldsList, + inlineMap, + reflect.New(st).Elem(), + } + structMapMutex.Lock() + structMap[st] = sinfo + structMapMutex.Unlock() + return sinfo, nil +} diff --git a/vendor/gopkg.in/mgo.v2/bson/decimal.go b/vendor/gopkg.in/mgo.v2/bson/decimal.go new file mode 100644 index 00000000..3d2f7002 --- /dev/null +++ b/vendor/gopkg.in/mgo.v2/bson/decimal.go @@ -0,0 +1,310 @@ +// BSON library for Go +// +// Copyright (c) 2010-2012 - Gustavo Niemeyer +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package bson + +import ( + "fmt" + "strconv" + "strings" +) + +// Decimal128 holds decimal128 BSON values. +type Decimal128 struct { + h, l uint64 +} + +func (d Decimal128) String() string { + var pos int // positive sign + var e int // exponent + var h, l uint64 // significand high/low + + if d.h>>63&1 == 0 { + pos = 1 + } + + switch d.h >> 58 & (1<<5 - 1) { + case 0x1F: + return "NaN" + case 0x1E: + return "-Inf"[pos:] + } + + l = d.l + if d.h>>61&3 == 3 { + // Bits: 1*sign 2*ignored 14*exponent 111*significand. + // Implicit 0b100 prefix in significand. + e = int(d.h>>47&(1<<14-1)) - 6176 + //h = 4<<47 | d.h&(1<<47-1) + // Spec says all of these values are out of range. + h, l = 0, 0 + } else { + // Bits: 1*sign 14*exponent 113*significand + e = int(d.h>>49&(1<<14-1)) - 6176 + h = d.h & (1<<49 - 1) + } + + // Would be handled by the logic below, but that's trivial and common. + if h == 0 && l == 0 && e == 0 { + return "-0"[pos:] + } + + var repr [48]byte // Loop 5 times over 9 digits plus dot, negative sign, and leading zero. + var last = len(repr) + var i = len(repr) + var dot = len(repr) + e + var rem uint32 +Loop: + for d9 := 0; d9 < 5; d9++ { + h, l, rem = divmod(h, l, 1e9) + for d1 := 0; d1 < 9; d1++ { + // Handle "-0.0", "0.00123400", "-1.00E-6", "1.050E+3", etc. + if i < len(repr) && (dot == i || l == 0 && h == 0 && rem > 0 && rem < 10 && (dot < i-6 || e > 0)) { + e += len(repr) - i + i-- + repr[i] = '.' + last = i - 1 + dot = len(repr) // Unmark. + } + c := '0' + byte(rem%10) + rem /= 10 + i-- + repr[i] = c + // Handle "0E+3", "1E+3", etc. + if l == 0 && h == 0 && rem == 0 && i == len(repr)-1 && (dot < i-5 || e > 0) { + last = i + break Loop + } + if c != '0' { + last = i + } + // Break early. Works without it, but why. + if dot > i && l == 0 && h == 0 && rem == 0 { + break Loop + } + } + } + repr[last-1] = '-' + last-- + + if e > 0 { + return string(repr[last+pos:]) + "E+" + strconv.Itoa(e) + } + if e < 0 { + return string(repr[last+pos:]) + "E" + strconv.Itoa(e) + } + return string(repr[last+pos:]) +} + +func divmod(h, l uint64, div uint32) (qh, ql uint64, rem uint32) { + div64 := uint64(div) + a := h >> 32 + aq := a / div64 + ar := a % div64 + b := ar<<32 + h&(1<<32-1) + bq := b / div64 + br := b % div64 + c := br<<32 + l>>32 + cq := c / div64 + cr := c % div64 + d := cr<<32 + l&(1<<32-1) + dq := d / div64 + dr := d % div64 + return (aq<<32 | bq), (cq<<32 | dq), uint32(dr) +} + +var dNaN = Decimal128{0x1F << 58, 0} +var dPosInf = Decimal128{0x1E << 58, 0} +var dNegInf = Decimal128{0x3E << 58, 0} + +func dErr(s string) (Decimal128, error) { + return dNaN, fmt.Errorf("cannot parse %q as a decimal128", s) +} + +func ParseDecimal128(s string) (Decimal128, error) { + orig := s + if s == "" { + return dErr(orig) + } + neg := s[0] == '-' + if neg || s[0] == '+' { + s = s[1:] + } + + if (len(s) == 3 || len(s) == 8) && (s[0] == 'N' || s[0] == 'n' || s[0] == 'I' || s[0] == 'i') { + if s == "NaN" || s == "nan" || strings.EqualFold(s, "nan") { + return dNaN, nil + } + if s == "Inf" || s == "inf" || strings.EqualFold(s, "inf") || strings.EqualFold(s, "infinity") { + if neg { + return dNegInf, nil + } + return dPosInf, nil + } + return dErr(orig) + } + + var h, l uint64 + var e int + + var add, ovr uint32 + var mul uint32 = 1 + var dot = -1 + var digits = 0 + var i = 0 + for i < len(s) { + c := s[i] + if mul == 1e9 { + h, l, ovr = muladd(h, l, mul, add) + mul, add = 1, 0 + if ovr > 0 || h&((1<<15-1)<<49) > 0 { + return dErr(orig) + } + } + if c >= '0' && c <= '9' { + i++ + if c > '0' || digits > 0 { + digits++ + } + if digits > 34 { + if c == '0' { + // Exact rounding. + e++ + continue + } + return dErr(orig) + } + mul *= 10 + add *= 10 + add += uint32(c - '0') + continue + } + if c == '.' { + i++ + if dot >= 0 || i == 1 && len(s) == 1 { + return dErr(orig) + } + if i == len(s) { + break + } + if s[i] < '0' || s[i] > '9' || e > 0 { + return dErr(orig) + } + dot = i + continue + } + break + } + if i == 0 { + return dErr(orig) + } + if mul > 1 { + h, l, ovr = muladd(h, l, mul, add) + if ovr > 0 || h&((1<<15-1)<<49) > 0 { + return dErr(orig) + } + } + if dot >= 0 { + e += dot - i + } + if i+1 < len(s) && (s[i] == 'E' || s[i] == 'e') { + i++ + eneg := s[i] == '-' + if eneg || s[i] == '+' { + i++ + if i == len(s) { + return dErr(orig) + } + } + n := 0 + for i < len(s) && n < 1e4 { + c := s[i] + i++ + if c < '0' || c > '9' { + return dErr(orig) + } + n *= 10 + n += int(c - '0') + } + if eneg { + n = -n + } + e += n + for e < -6176 { + // Subnormal. + var div uint32 = 1 + for div < 1e9 && e < -6176 { + div *= 10 + e++ + } + var rem uint32 + h, l, rem = divmod(h, l, div) + if rem > 0 { + return dErr(orig) + } + } + for e > 6111 { + // Clamped. + var mul uint32 = 1 + for mul < 1e9 && e > 6111 { + mul *= 10 + e-- + } + h, l, ovr = muladd(h, l, mul, 0) + if ovr > 0 || h&((1<<15-1)<<49) > 0 { + return dErr(orig) + } + } + if e < -6176 || e > 6111 { + return dErr(orig) + } + } + + if i < len(s) { + return dErr(orig) + } + + h |= uint64(e+6176) & uint64(1<<14-1) << 49 + if neg { + h |= 1 << 63 + } + return Decimal128{h, l}, nil +} + +func muladd(h, l uint64, mul uint32, add uint32) (resh, resl uint64, overflow uint32) { + mul64 := uint64(mul) + a := mul64 * (l & (1<<32 - 1)) + b := a>>32 + mul64*(l>>32) + c := b>>32 + mul64*(h&(1<<32-1)) + d := c>>32 + mul64*(h>>32) + + a = a&(1<<32-1) + uint64(add) + b = b&(1<<32-1) + a>>32 + c = c&(1<<32-1) + b>>32 + d = d&(1<<32-1) + c>>32 + + return (d<<32 | c&(1<<32-1)), (b<<32 | a&(1<<32-1)), uint32(d >> 32) +} diff --git a/vendor/gopkg.in/mgo.v2/bson/decode.go b/vendor/gopkg.in/mgo.v2/bson/decode.go new file mode 100644 index 00000000..7c2d8416 --- /dev/null +++ b/vendor/gopkg.in/mgo.v2/bson/decode.go @@ -0,0 +1,849 @@ +// BSON library for Go +// +// Copyright (c) 2010-2012 - Gustavo Niemeyer +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// gobson - BSON library for Go. + +package bson + +import ( + "fmt" + "math" + "net/url" + "reflect" + "strconv" + "sync" + "time" +) + +type decoder struct { + in []byte + i int + docType reflect.Type +} + +var typeM = reflect.TypeOf(M{}) + +func newDecoder(in []byte) *decoder { + return &decoder{in, 0, typeM} +} + +// -------------------------------------------------------------------------- +// Some helper functions. + +func corrupted() { + panic("Document is corrupted") +} + +func settableValueOf(i interface{}) reflect.Value { + v := reflect.ValueOf(i) + sv := reflect.New(v.Type()).Elem() + sv.Set(v) + return sv +} + +// -------------------------------------------------------------------------- +// Unmarshaling of documents. + +const ( + setterUnknown = iota + setterNone + setterType + setterAddr +) + +var setterStyles map[reflect.Type]int +var setterIface reflect.Type +var setterMutex sync.RWMutex + +func init() { + var iface Setter + setterIface = reflect.TypeOf(&iface).Elem() + setterStyles = make(map[reflect.Type]int) +} + +func setterStyle(outt reflect.Type) int { + setterMutex.RLock() + style := setterStyles[outt] + setterMutex.RUnlock() + if style == setterUnknown { + setterMutex.Lock() + defer setterMutex.Unlock() + if outt.Implements(setterIface) { + setterStyles[outt] = setterType + } else if reflect.PtrTo(outt).Implements(setterIface) { + setterStyles[outt] = setterAddr + } else { + setterStyles[outt] = setterNone + } + style = setterStyles[outt] + } + return style +} + +func getSetter(outt reflect.Type, out reflect.Value) Setter { + style := setterStyle(outt) + if style == setterNone { + return nil + } + if style == setterAddr { + if !out.CanAddr() { + return nil + } + out = out.Addr() + } else if outt.Kind() == reflect.Ptr && out.IsNil() { + out.Set(reflect.New(outt.Elem())) + } + return out.Interface().(Setter) +} + +func clearMap(m reflect.Value) { + var none reflect.Value + for _, k := range m.MapKeys() { + m.SetMapIndex(k, none) + } +} + +func (d *decoder) readDocTo(out reflect.Value) { + var elemType reflect.Type + outt := out.Type() + outk := outt.Kind() + + for { + if outk == reflect.Ptr && out.IsNil() { + out.Set(reflect.New(outt.Elem())) + } + if setter := getSetter(outt, out); setter != nil { + var raw Raw + d.readDocTo(reflect.ValueOf(&raw)) + err := setter.SetBSON(raw) + if _, ok := err.(*TypeError); err != nil && !ok { + panic(err) + } + return + } + if outk == reflect.Ptr { + out = out.Elem() + outt = out.Type() + outk = out.Kind() + continue + } + break + } + + var fieldsMap map[string]fieldInfo + var inlineMap reflect.Value + start := d.i + + origout := out + if outk == reflect.Interface { + if d.docType.Kind() == reflect.Map { + mv := reflect.MakeMap(d.docType) + out.Set(mv) + out = mv + } else { + dv := reflect.New(d.docType).Elem() + out.Set(dv) + out = dv + } + outt = out.Type() + outk = outt.Kind() + } + + docType := d.docType + keyType := typeString + convertKey := false + switch outk { + case reflect.Map: + keyType = outt.Key() + if keyType.Kind() != reflect.String { + panic("BSON map must have string keys. Got: " + outt.String()) + } + if keyType != typeString { + convertKey = true + } + elemType = outt.Elem() + if elemType == typeIface { + d.docType = outt + } + if out.IsNil() { + out.Set(reflect.MakeMap(out.Type())) + } else if out.Len() > 0 { + clearMap(out) + } + case reflect.Struct: + if outt != typeRaw { + sinfo, err := getStructInfo(out.Type()) + if err != nil { + panic(err) + } + fieldsMap = sinfo.FieldsMap + out.Set(sinfo.Zero) + if sinfo.InlineMap != -1 { + inlineMap = out.Field(sinfo.InlineMap) + if !inlineMap.IsNil() && inlineMap.Len() > 0 { + clearMap(inlineMap) + } + elemType = inlineMap.Type().Elem() + if elemType == typeIface { + d.docType = inlineMap.Type() + } + } + } + case reflect.Slice: + switch outt.Elem() { + case typeDocElem: + origout.Set(d.readDocElems(outt)) + return + case typeRawDocElem: + origout.Set(d.readRawDocElems(outt)) + return + } + fallthrough + default: + panic("Unsupported document type for unmarshalling: " + out.Type().String()) + } + + end := int(d.readInt32()) + end += d.i - 4 + if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' { + corrupted() + } + for d.in[d.i] != '\x00' { + kind := d.readByte() + name := d.readCStr() + if d.i >= end { + corrupted() + } + + switch outk { + case reflect.Map: + e := reflect.New(elemType).Elem() + if d.readElemTo(e, kind) { + k := reflect.ValueOf(name) + if convertKey { + k = k.Convert(keyType) + } + out.SetMapIndex(k, e) + } + case reflect.Struct: + if outt == typeRaw { + d.dropElem(kind) + } else { + if info, ok := fieldsMap[name]; ok { + if info.Inline == nil { + d.readElemTo(out.Field(info.Num), kind) + } else { + d.readElemTo(out.FieldByIndex(info.Inline), kind) + } + } else if inlineMap.IsValid() { + if inlineMap.IsNil() { + inlineMap.Set(reflect.MakeMap(inlineMap.Type())) + } + e := reflect.New(elemType).Elem() + if d.readElemTo(e, kind) { + inlineMap.SetMapIndex(reflect.ValueOf(name), e) + } + } else { + d.dropElem(kind) + } + } + case reflect.Slice: + } + + if d.i >= end { + corrupted() + } + } + d.i++ // '\x00' + if d.i != end { + corrupted() + } + d.docType = docType + + if outt == typeRaw { + out.Set(reflect.ValueOf(Raw{0x03, d.in[start:d.i]})) + } +} + +func (d *decoder) readArrayDocTo(out reflect.Value) { + end := int(d.readInt32()) + end += d.i - 4 + if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' { + corrupted() + } + i := 0 + l := out.Len() + for d.in[d.i] != '\x00' { + if i >= l { + panic("Length mismatch on array field") + } + kind := d.readByte() + for d.i < end && d.in[d.i] != '\x00' { + d.i++ + } + if d.i >= end { + corrupted() + } + d.i++ + d.readElemTo(out.Index(i), kind) + if d.i >= end { + corrupted() + } + i++ + } + if i != l { + panic("Length mismatch on array field") + } + d.i++ // '\x00' + if d.i != end { + corrupted() + } +} + +func (d *decoder) readSliceDoc(t reflect.Type) interface{} { + tmp := make([]reflect.Value, 0, 8) + elemType := t.Elem() + if elemType == typeRawDocElem { + d.dropElem(0x04) + return reflect.Zero(t).Interface() + } + + end := int(d.readInt32()) + end += d.i - 4 + if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' { + corrupted() + } + for d.in[d.i] != '\x00' { + kind := d.readByte() + for d.i < end && d.in[d.i] != '\x00' { + d.i++ + } + if d.i >= end { + corrupted() + } + d.i++ + e := reflect.New(elemType).Elem() + if d.readElemTo(e, kind) { + tmp = append(tmp, e) + } + if d.i >= end { + corrupted() + } + } + d.i++ // '\x00' + if d.i != end { + corrupted() + } + + n := len(tmp) + slice := reflect.MakeSlice(t, n, n) + for i := 0; i != n; i++ { + slice.Index(i).Set(tmp[i]) + } + return slice.Interface() +} + +var typeSlice = reflect.TypeOf([]interface{}{}) +var typeIface = typeSlice.Elem() + +func (d *decoder) readDocElems(typ reflect.Type) reflect.Value { + docType := d.docType + d.docType = typ + slice := make([]DocElem, 0, 8) + d.readDocWith(func(kind byte, name string) { + e := DocElem{Name: name} + v := reflect.ValueOf(&e.Value) + if d.readElemTo(v.Elem(), kind) { + slice = append(slice, e) + } + }) + slicev := reflect.New(typ).Elem() + slicev.Set(reflect.ValueOf(slice)) + d.docType = docType + return slicev +} + +func (d *decoder) readRawDocElems(typ reflect.Type) reflect.Value { + docType := d.docType + d.docType = typ + slice := make([]RawDocElem, 0, 8) + d.readDocWith(func(kind byte, name string) { + e := RawDocElem{Name: name} + v := reflect.ValueOf(&e.Value) + if d.readElemTo(v.Elem(), kind) { + slice = append(slice, e) + } + }) + slicev := reflect.New(typ).Elem() + slicev.Set(reflect.ValueOf(slice)) + d.docType = docType + return slicev +} + +func (d *decoder) readDocWith(f func(kind byte, name string)) { + end := int(d.readInt32()) + end += d.i - 4 + if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' { + corrupted() + } + for d.in[d.i] != '\x00' { + kind := d.readByte() + name := d.readCStr() + if d.i >= end { + corrupted() + } + f(kind, name) + if d.i >= end { + corrupted() + } + } + d.i++ // '\x00' + if d.i != end { + corrupted() + } +} + +// -------------------------------------------------------------------------- +// Unmarshaling of individual elements within a document. + +var blackHole = settableValueOf(struct{}{}) + +func (d *decoder) dropElem(kind byte) { + d.readElemTo(blackHole, kind) +} + +// Attempt to decode an element from the document and put it into out. +// If the types are not compatible, the returned ok value will be +// false and out will be unchanged. +func (d *decoder) readElemTo(out reflect.Value, kind byte) (good bool) { + + start := d.i + + if kind == 0x03 { + // Delegate unmarshaling of documents. + outt := out.Type() + outk := out.Kind() + switch outk { + case reflect.Interface, reflect.Ptr, reflect.Struct, reflect.Map: + d.readDocTo(out) + return true + } + if setterStyle(outt) != setterNone { + d.readDocTo(out) + return true + } + if outk == reflect.Slice { + switch outt.Elem() { + case typeDocElem: + out.Set(d.readDocElems(outt)) + case typeRawDocElem: + out.Set(d.readRawDocElems(outt)) + default: + d.readDocTo(blackHole) + } + return true + } + d.readDocTo(blackHole) + return true + } + + var in interface{} + + switch kind { + case 0x01: // Float64 + in = d.readFloat64() + case 0x02: // UTF-8 string + in = d.readStr() + case 0x03: // Document + panic("Can't happen. Handled above.") + case 0x04: // Array + outt := out.Type() + if setterStyle(outt) != setterNone { + // Skip the value so its data is handed to the setter below. + d.dropElem(kind) + break + } + for outt.Kind() == reflect.Ptr { + outt = outt.Elem() + } + switch outt.Kind() { + case reflect.Array: + d.readArrayDocTo(out) + return true + case reflect.Slice: + in = d.readSliceDoc(outt) + default: + in = d.readSliceDoc(typeSlice) + } + case 0x05: // Binary + b := d.readBinary() + if b.Kind == 0x00 || b.Kind == 0x02 { + in = b.Data + } else { + in = b + } + case 0x06: // Undefined (obsolete, but still seen in the wild) + in = Undefined + case 0x07: // ObjectId + in = ObjectId(d.readBytes(12)) + case 0x08: // Bool + in = d.readBool() + case 0x09: // Timestamp + // MongoDB handles timestamps as milliseconds. + i := d.readInt64() + if i == -62135596800000 { + in = time.Time{} // In UTC for convenience. + } else { + in = time.Unix(i/1e3, i%1e3*1e6) + } + case 0x0A: // Nil + in = nil + case 0x0B: // RegEx + in = d.readRegEx() + case 0x0C: + in = DBPointer{Namespace: d.readStr(), Id: ObjectId(d.readBytes(12))} + case 0x0D: // JavaScript without scope + in = JavaScript{Code: d.readStr()} + case 0x0E: // Symbol + in = Symbol(d.readStr()) + case 0x0F: // JavaScript with scope + d.i += 4 // Skip length + js := JavaScript{d.readStr(), make(M)} + d.readDocTo(reflect.ValueOf(js.Scope)) + in = js + case 0x10: // Int32 + in = int(d.readInt32()) + case 0x11: // Mongo-specific timestamp + in = MongoTimestamp(d.readInt64()) + case 0x12: // Int64 + in = d.readInt64() + case 0x13: // Decimal128 + in = Decimal128{ + l: uint64(d.readInt64()), + h: uint64(d.readInt64()), + } + case 0x7F: // Max key + in = MaxKey + case 0xFF: // Min key + in = MinKey + default: + panic(fmt.Sprintf("Unknown element kind (0x%02X)", kind)) + } + + outt := out.Type() + + if outt == typeRaw { + out.Set(reflect.ValueOf(Raw{kind, d.in[start:d.i]})) + return true + } + + if setter := getSetter(outt, out); setter != nil { + err := setter.SetBSON(Raw{kind, d.in[start:d.i]}) + if err == SetZero { + out.Set(reflect.Zero(outt)) + return true + } + if err == nil { + return true + } + if _, ok := err.(*TypeError); !ok { + panic(err) + } + return false + } + + if in == nil { + out.Set(reflect.Zero(outt)) + return true + } + + outk := outt.Kind() + + // Dereference and initialize pointer if necessary. + first := true + for outk == reflect.Ptr { + if !out.IsNil() { + out = out.Elem() + } else { + elem := reflect.New(outt.Elem()) + if first { + // Only set if value is compatible. + first = false + defer func(out, elem reflect.Value) { + if good { + out.Set(elem) + } + }(out, elem) + } else { + out.Set(elem) + } + out = elem + } + outt = out.Type() + outk = outt.Kind() + } + + inv := reflect.ValueOf(in) + if outt == inv.Type() { + out.Set(inv) + return true + } + + switch outk { + case reflect.Interface: + out.Set(inv) + return true + case reflect.String: + switch inv.Kind() { + case reflect.String: + out.SetString(inv.String()) + return true + case reflect.Slice: + if b, ok := in.([]byte); ok { + out.SetString(string(b)) + return true + } + case reflect.Int, reflect.Int64: + if outt == typeJSONNumber { + out.SetString(strconv.FormatInt(inv.Int(), 10)) + return true + } + case reflect.Float64: + if outt == typeJSONNumber { + out.SetString(strconv.FormatFloat(inv.Float(), 'f', -1, 64)) + return true + } + } + case reflect.Slice, reflect.Array: + // Remember, array (0x04) slices are built with the correct + // element type. If we are here, must be a cross BSON kind + // conversion (e.g. 0x05 unmarshalling on string). + if outt.Elem().Kind() != reflect.Uint8 { + break + } + switch inv.Kind() { + case reflect.String: + slice := []byte(inv.String()) + out.Set(reflect.ValueOf(slice)) + return true + case reflect.Slice: + switch outt.Kind() { + case reflect.Array: + reflect.Copy(out, inv) + case reflect.Slice: + out.SetBytes(inv.Bytes()) + } + return true + } + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + switch inv.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + out.SetInt(inv.Int()) + return true + case reflect.Float32, reflect.Float64: + out.SetInt(int64(inv.Float())) + return true + case reflect.Bool: + if inv.Bool() { + out.SetInt(1) + } else { + out.SetInt(0) + } + return true + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + panic("can't happen: no uint types in BSON (!?)") + } + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + switch inv.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + out.SetUint(uint64(inv.Int())) + return true + case reflect.Float32, reflect.Float64: + out.SetUint(uint64(inv.Float())) + return true + case reflect.Bool: + if inv.Bool() { + out.SetUint(1) + } else { + out.SetUint(0) + } + return true + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + panic("Can't happen. No uint types in BSON.") + } + case reflect.Float32, reflect.Float64: + switch inv.Kind() { + case reflect.Float32, reflect.Float64: + out.SetFloat(inv.Float()) + return true + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + out.SetFloat(float64(inv.Int())) + return true + case reflect.Bool: + if inv.Bool() { + out.SetFloat(1) + } else { + out.SetFloat(0) + } + return true + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + panic("Can't happen. No uint types in BSON?") + } + case reflect.Bool: + switch inv.Kind() { + case reflect.Bool: + out.SetBool(inv.Bool()) + return true + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + out.SetBool(inv.Int() != 0) + return true + case reflect.Float32, reflect.Float64: + out.SetBool(inv.Float() != 0) + return true + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + panic("Can't happen. No uint types in BSON?") + } + case reflect.Struct: + if outt == typeURL && inv.Kind() == reflect.String { + u, err := url.Parse(inv.String()) + if err != nil { + panic(err) + } + out.Set(reflect.ValueOf(u).Elem()) + return true + } + if outt == typeBinary { + if b, ok := in.([]byte); ok { + out.Set(reflect.ValueOf(Binary{Data: b})) + return true + } + } + } + + return false +} + +// -------------------------------------------------------------------------- +// Parsers of basic types. + +func (d *decoder) readRegEx() RegEx { + re := RegEx{} + re.Pattern = d.readCStr() + re.Options = d.readCStr() + return re +} + +func (d *decoder) readBinary() Binary { + l := d.readInt32() + b := Binary{} + b.Kind = d.readByte() + b.Data = d.readBytes(l) + if b.Kind == 0x02 && len(b.Data) >= 4 { + // Weird obsolete format with redundant length. + b.Data = b.Data[4:] + } + return b +} + +func (d *decoder) readStr() string { + l := d.readInt32() + b := d.readBytes(l - 1) + if d.readByte() != '\x00' { + corrupted() + } + return string(b) +} + +func (d *decoder) readCStr() string { + start := d.i + end := start + l := len(d.in) + for ; end != l; end++ { + if d.in[end] == '\x00' { + break + } + } + d.i = end + 1 + if d.i > l { + corrupted() + } + return string(d.in[start:end]) +} + +func (d *decoder) readBool() bool { + b := d.readByte() + if b == 0 { + return false + } + if b == 1 { + return true + } + panic(fmt.Sprintf("encoded boolean must be 1 or 0, found %d", b)) +} + +func (d *decoder) readFloat64() float64 { + return math.Float64frombits(uint64(d.readInt64())) +} + +func (d *decoder) readInt32() int32 { + b := d.readBytes(4) + return int32((uint32(b[0]) << 0) | + (uint32(b[1]) << 8) | + (uint32(b[2]) << 16) | + (uint32(b[3]) << 24)) +} + +func (d *decoder) readInt64() int64 { + b := d.readBytes(8) + return int64((uint64(b[0]) << 0) | + (uint64(b[1]) << 8) | + (uint64(b[2]) << 16) | + (uint64(b[3]) << 24) | + (uint64(b[4]) << 32) | + (uint64(b[5]) << 40) | + (uint64(b[6]) << 48) | + (uint64(b[7]) << 56)) +} + +func (d *decoder) readByte() byte { + i := d.i + d.i++ + if d.i > len(d.in) { + corrupted() + } + return d.in[i] +} + +func (d *decoder) readBytes(length int32) []byte { + if length < 0 { + corrupted() + } + start := d.i + d.i += int(length) + if d.i < start || d.i > len(d.in) { + corrupted() + } + return d.in[start : start+int(length)] +} diff --git a/vendor/gopkg.in/mgo.v2/bson/encode.go b/vendor/gopkg.in/mgo.v2/bson/encode.go new file mode 100644 index 00000000..add39e86 --- /dev/null +++ b/vendor/gopkg.in/mgo.v2/bson/encode.go @@ -0,0 +1,514 @@ +// BSON library for Go +// +// Copyright (c) 2010-2012 - Gustavo Niemeyer +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// gobson - BSON library for Go. + +package bson + +import ( + "encoding/json" + "fmt" + "math" + "net/url" + "reflect" + "strconv" + "time" +) + +// -------------------------------------------------------------------------- +// Some internal infrastructure. + +var ( + typeBinary = reflect.TypeOf(Binary{}) + typeObjectId = reflect.TypeOf(ObjectId("")) + typeDBPointer = reflect.TypeOf(DBPointer{"", ObjectId("")}) + typeSymbol = reflect.TypeOf(Symbol("")) + typeMongoTimestamp = reflect.TypeOf(MongoTimestamp(0)) + typeOrderKey = reflect.TypeOf(MinKey) + typeDocElem = reflect.TypeOf(DocElem{}) + typeRawDocElem = reflect.TypeOf(RawDocElem{}) + typeRaw = reflect.TypeOf(Raw{}) + typeURL = reflect.TypeOf(url.URL{}) + typeTime = reflect.TypeOf(time.Time{}) + typeString = reflect.TypeOf("") + typeJSONNumber = reflect.TypeOf(json.Number("")) +) + +const itoaCacheSize = 32 + +var itoaCache []string + +func init() { + itoaCache = make([]string, itoaCacheSize) + for i := 0; i != itoaCacheSize; i++ { + itoaCache[i] = strconv.Itoa(i) + } +} + +func itoa(i int) string { + if i < itoaCacheSize { + return itoaCache[i] + } + return strconv.Itoa(i) +} + +// -------------------------------------------------------------------------- +// Marshaling of the document value itself. + +type encoder struct { + out []byte +} + +func (e *encoder) addDoc(v reflect.Value) { + for { + if vi, ok := v.Interface().(Getter); ok { + getv, err := vi.GetBSON() + if err != nil { + panic(err) + } + v = reflect.ValueOf(getv) + continue + } + if v.Kind() == reflect.Ptr { + v = v.Elem() + continue + } + break + } + + if v.Type() == typeRaw { + raw := v.Interface().(Raw) + if raw.Kind != 0x03 && raw.Kind != 0x00 { + panic("Attempted to marshal Raw kind " + strconv.Itoa(int(raw.Kind)) + " as a document") + } + if len(raw.Data) == 0 { + panic("Attempted to marshal empty Raw document") + } + e.addBytes(raw.Data...) + return + } + + start := e.reserveInt32() + + switch v.Kind() { + case reflect.Map: + e.addMap(v) + case reflect.Struct: + e.addStruct(v) + case reflect.Array, reflect.Slice: + e.addSlice(v) + default: + panic("Can't marshal " + v.Type().String() + " as a BSON document") + } + + e.addBytes(0) + e.setInt32(start, int32(len(e.out)-start)) +} + +func (e *encoder) addMap(v reflect.Value) { + for _, k := range v.MapKeys() { + e.addElem(k.String(), v.MapIndex(k), false) + } +} + +func (e *encoder) addStruct(v reflect.Value) { + sinfo, err := getStructInfo(v.Type()) + if err != nil { + panic(err) + } + var value reflect.Value + if sinfo.InlineMap >= 0 { + m := v.Field(sinfo.InlineMap) + if m.Len() > 0 { + for _, k := range m.MapKeys() { + ks := k.String() + if _, found := sinfo.FieldsMap[ks]; found { + panic(fmt.Sprintf("Can't have key %q in inlined map; conflicts with struct field", ks)) + } + e.addElem(ks, m.MapIndex(k), false) + } + } + } + for _, info := range sinfo.FieldsList { + if info.Inline == nil { + value = v.Field(info.Num) + } else { + value = v.FieldByIndex(info.Inline) + } + if info.OmitEmpty && isZero(value) { + continue + } + e.addElem(info.Key, value, info.MinSize) + } +} + +func isZero(v reflect.Value) bool { + switch v.Kind() { + case reflect.String: + return len(v.String()) == 0 + case reflect.Ptr, reflect.Interface: + return v.IsNil() + case reflect.Slice: + return v.Len() == 0 + case reflect.Map: + return v.Len() == 0 + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return v.Int() == 0 + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + return v.Uint() == 0 + case reflect.Float32, reflect.Float64: + return v.Float() == 0 + case reflect.Bool: + return !v.Bool() + case reflect.Struct: + vt := v.Type() + if vt == typeTime { + return v.Interface().(time.Time).IsZero() + } + for i := 0; i < v.NumField(); i++ { + if vt.Field(i).PkgPath != "" && !vt.Field(i).Anonymous { + continue // Private field + } + if !isZero(v.Field(i)) { + return false + } + } + return true + } + return false +} + +func (e *encoder) addSlice(v reflect.Value) { + vi := v.Interface() + if d, ok := vi.(D); ok { + for _, elem := range d { + e.addElem(elem.Name, reflect.ValueOf(elem.Value), false) + } + return + } + if d, ok := vi.(RawD); ok { + for _, elem := range d { + e.addElem(elem.Name, reflect.ValueOf(elem.Value), false) + } + return + } + l := v.Len() + et := v.Type().Elem() + if et == typeDocElem { + for i := 0; i < l; i++ { + elem := v.Index(i).Interface().(DocElem) + e.addElem(elem.Name, reflect.ValueOf(elem.Value), false) + } + return + } + if et == typeRawDocElem { + for i := 0; i < l; i++ { + elem := v.Index(i).Interface().(RawDocElem) + e.addElem(elem.Name, reflect.ValueOf(elem.Value), false) + } + return + } + for i := 0; i < l; i++ { + e.addElem(itoa(i), v.Index(i), false) + } +} + +// -------------------------------------------------------------------------- +// Marshaling of elements in a document. + +func (e *encoder) addElemName(kind byte, name string) { + e.addBytes(kind) + e.addBytes([]byte(name)...) + e.addBytes(0) +} + +func (e *encoder) addElem(name string, v reflect.Value, minSize bool) { + + if !v.IsValid() { + e.addElemName(0x0A, name) + return + } + + if getter, ok := v.Interface().(Getter); ok { + getv, err := getter.GetBSON() + if err != nil { + panic(err) + } + e.addElem(name, reflect.ValueOf(getv), minSize) + return + } + + switch v.Kind() { + + case reflect.Interface: + e.addElem(name, v.Elem(), minSize) + + case reflect.Ptr: + e.addElem(name, v.Elem(), minSize) + + case reflect.String: + s := v.String() + switch v.Type() { + case typeObjectId: + if len(s) != 12 { + panic("ObjectIDs must be exactly 12 bytes long (got " + + strconv.Itoa(len(s)) + ")") + } + e.addElemName(0x07, name) + e.addBytes([]byte(s)...) + case typeSymbol: + e.addElemName(0x0E, name) + e.addStr(s) + case typeJSONNumber: + n := v.Interface().(json.Number) + if i, err := n.Int64(); err == nil { + e.addElemName(0x12, name) + e.addInt64(i) + } else if f, err := n.Float64(); err == nil { + e.addElemName(0x01, name) + e.addFloat64(f) + } else { + panic("failed to convert json.Number to a number: " + s) + } + default: + e.addElemName(0x02, name) + e.addStr(s) + } + + case reflect.Float32, reflect.Float64: + e.addElemName(0x01, name) + e.addFloat64(v.Float()) + + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + u := v.Uint() + if int64(u) < 0 { + panic("BSON has no uint64 type, and value is too large to fit correctly in an int64") + } else if u <= math.MaxInt32 && (minSize || v.Kind() <= reflect.Uint32) { + e.addElemName(0x10, name) + e.addInt32(int32(u)) + } else { + e.addElemName(0x12, name) + e.addInt64(int64(u)) + } + + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + switch v.Type() { + case typeMongoTimestamp: + e.addElemName(0x11, name) + e.addInt64(v.Int()) + + case typeOrderKey: + if v.Int() == int64(MaxKey) { + e.addElemName(0x7F, name) + } else { + e.addElemName(0xFF, name) + } + + default: + i := v.Int() + if (minSize || v.Type().Kind() != reflect.Int64) && i >= math.MinInt32 && i <= math.MaxInt32 { + // It fits into an int32, encode as such. + e.addElemName(0x10, name) + e.addInt32(int32(i)) + } else { + e.addElemName(0x12, name) + e.addInt64(i) + } + } + + case reflect.Bool: + e.addElemName(0x08, name) + if v.Bool() { + e.addBytes(1) + } else { + e.addBytes(0) + } + + case reflect.Map: + e.addElemName(0x03, name) + e.addDoc(v) + + case reflect.Slice: + vt := v.Type() + et := vt.Elem() + if et.Kind() == reflect.Uint8 { + e.addElemName(0x05, name) + e.addBinary(0x00, v.Bytes()) + } else if et == typeDocElem || et == typeRawDocElem { + e.addElemName(0x03, name) + e.addDoc(v) + } else { + e.addElemName(0x04, name) + e.addDoc(v) + } + + case reflect.Array: + et := v.Type().Elem() + if et.Kind() == reflect.Uint8 { + e.addElemName(0x05, name) + if v.CanAddr() { + e.addBinary(0x00, v.Slice(0, v.Len()).Interface().([]byte)) + } else { + n := v.Len() + e.addInt32(int32(n)) + e.addBytes(0x00) + for i := 0; i < n; i++ { + el := v.Index(i) + e.addBytes(byte(el.Uint())) + } + } + } else { + e.addElemName(0x04, name) + e.addDoc(v) + } + + case reflect.Struct: + switch s := v.Interface().(type) { + + case Raw: + kind := s.Kind + if kind == 0x00 { + kind = 0x03 + } + if len(s.Data) == 0 && kind != 0x06 && kind != 0x0A && kind != 0xFF && kind != 0x7F { + panic("Attempted to marshal empty Raw document") + } + e.addElemName(kind, name) + e.addBytes(s.Data...) + + case Binary: + e.addElemName(0x05, name) + e.addBinary(s.Kind, s.Data) + + case Decimal128: + e.addElemName(0x13, name) + e.addInt64(int64(s.l)) + e.addInt64(int64(s.h)) + + case DBPointer: + e.addElemName(0x0C, name) + e.addStr(s.Namespace) + if len(s.Id) != 12 { + panic("ObjectIDs must be exactly 12 bytes long (got " + + strconv.Itoa(len(s.Id)) + ")") + } + e.addBytes([]byte(s.Id)...) + + case RegEx: + e.addElemName(0x0B, name) + e.addCStr(s.Pattern) + e.addCStr(s.Options) + + case JavaScript: + if s.Scope == nil { + e.addElemName(0x0D, name) + e.addStr(s.Code) + } else { + e.addElemName(0x0F, name) + start := e.reserveInt32() + e.addStr(s.Code) + e.addDoc(reflect.ValueOf(s.Scope)) + e.setInt32(start, int32(len(e.out)-start)) + } + + case time.Time: + // MongoDB handles timestamps as milliseconds. + e.addElemName(0x09, name) + e.addInt64(s.Unix()*1000 + int64(s.Nanosecond()/1e6)) + + case url.URL: + e.addElemName(0x02, name) + e.addStr(s.String()) + + case undefined: + e.addElemName(0x06, name) + + default: + e.addElemName(0x03, name) + e.addDoc(v) + } + + default: + panic("Can't marshal " + v.Type().String() + " in a BSON document") + } +} + +// -------------------------------------------------------------------------- +// Marshaling of base types. + +func (e *encoder) addBinary(subtype byte, v []byte) { + if subtype == 0x02 { + // Wonder how that brilliant idea came to life. Obsolete, luckily. + e.addInt32(int32(len(v) + 4)) + e.addBytes(subtype) + e.addInt32(int32(len(v))) + } else { + e.addInt32(int32(len(v))) + e.addBytes(subtype) + } + e.addBytes(v...) +} + +func (e *encoder) addStr(v string) { + e.addInt32(int32(len(v) + 1)) + e.addCStr(v) +} + +func (e *encoder) addCStr(v string) { + e.addBytes([]byte(v)...) + e.addBytes(0) +} + +func (e *encoder) reserveInt32() (pos int) { + pos = len(e.out) + e.addBytes(0, 0, 0, 0) + return pos +} + +func (e *encoder) setInt32(pos int, v int32) { + e.out[pos+0] = byte(v) + e.out[pos+1] = byte(v >> 8) + e.out[pos+2] = byte(v >> 16) + e.out[pos+3] = byte(v >> 24) +} + +func (e *encoder) addInt32(v int32) { + u := uint32(v) + e.addBytes(byte(u), byte(u>>8), byte(u>>16), byte(u>>24)) +} + +func (e *encoder) addInt64(v int64) { + u := uint64(v) + e.addBytes(byte(u), byte(u>>8), byte(u>>16), byte(u>>24), + byte(u>>32), byte(u>>40), byte(u>>48), byte(u>>56)) +} + +func (e *encoder) addFloat64(v float64) { + e.addInt64(int64(math.Float64bits(v))) +} + +func (e *encoder) addBytes(v ...byte) { + e.out = append(e.out, v...) +} diff --git a/vendor/gopkg.in/mgo.v2/bson/json.go b/vendor/gopkg.in/mgo.v2/bson/json.go new file mode 100644 index 00000000..09df8260 --- /dev/null +++ b/vendor/gopkg.in/mgo.v2/bson/json.go @@ -0,0 +1,380 @@ +package bson + +import ( + "bytes" + "encoding/base64" + "fmt" + "gopkg.in/mgo.v2/internal/json" + "strconv" + "time" +) + +// UnmarshalJSON unmarshals a JSON value that may hold non-standard +// syntax as defined in BSON's extended JSON specification. +func UnmarshalJSON(data []byte, value interface{}) error { + d := json.NewDecoder(bytes.NewBuffer(data)) + d.Extend(&jsonExt) + return d.Decode(value) +} + +// MarshalJSON marshals a JSON value that may hold non-standard +// syntax as defined in BSON's extended JSON specification. +func MarshalJSON(value interface{}) ([]byte, error) { + var buf bytes.Buffer + e := json.NewEncoder(&buf) + e.Extend(&jsonExt) + err := e.Encode(value) + if err != nil { + return nil, err + } + return buf.Bytes(), nil +} + +// jdec is used internally by the JSON decoding functions +// so they may unmarshal functions without getting into endless +// recursion due to keyed objects. +func jdec(data []byte, value interface{}) error { + d := json.NewDecoder(bytes.NewBuffer(data)) + d.Extend(&funcExt) + return d.Decode(value) +} + +var jsonExt json.Extension +var funcExt json.Extension + +// TODO +// - Shell regular expressions ("/regexp/opts") + +func init() { + jsonExt.DecodeUnquotedKeys(true) + jsonExt.DecodeTrailingCommas(true) + + funcExt.DecodeFunc("BinData", "$binaryFunc", "$type", "$binary") + jsonExt.DecodeKeyed("$binary", jdecBinary) + jsonExt.DecodeKeyed("$binaryFunc", jdecBinary) + jsonExt.EncodeType([]byte(nil), jencBinarySlice) + jsonExt.EncodeType(Binary{}, jencBinaryType) + + funcExt.DecodeFunc("ISODate", "$dateFunc", "S") + funcExt.DecodeFunc("new Date", "$dateFunc", "S") + jsonExt.DecodeKeyed("$date", jdecDate) + jsonExt.DecodeKeyed("$dateFunc", jdecDate) + jsonExt.EncodeType(time.Time{}, jencDate) + + funcExt.DecodeFunc("Timestamp", "$timestamp", "t", "i") + jsonExt.DecodeKeyed("$timestamp", jdecTimestamp) + jsonExt.EncodeType(MongoTimestamp(0), jencTimestamp) + + funcExt.DecodeConst("undefined", Undefined) + + jsonExt.DecodeKeyed("$regex", jdecRegEx) + jsonExt.EncodeType(RegEx{}, jencRegEx) + + funcExt.DecodeFunc("ObjectId", "$oidFunc", "Id") + jsonExt.DecodeKeyed("$oid", jdecObjectId) + jsonExt.DecodeKeyed("$oidFunc", jdecObjectId) + jsonExt.EncodeType(ObjectId(""), jencObjectId) + + funcExt.DecodeFunc("DBRef", "$dbrefFunc", "$ref", "$id") + jsonExt.DecodeKeyed("$dbrefFunc", jdecDBRef) + + funcExt.DecodeFunc("NumberLong", "$numberLongFunc", "N") + jsonExt.DecodeKeyed("$numberLong", jdecNumberLong) + jsonExt.DecodeKeyed("$numberLongFunc", jdecNumberLong) + jsonExt.EncodeType(int64(0), jencNumberLong) + jsonExt.EncodeType(int(0), jencInt) + + funcExt.DecodeConst("MinKey", MinKey) + funcExt.DecodeConst("MaxKey", MaxKey) + jsonExt.DecodeKeyed("$minKey", jdecMinKey) + jsonExt.DecodeKeyed("$maxKey", jdecMaxKey) + jsonExt.EncodeType(orderKey(0), jencMinMaxKey) + + jsonExt.DecodeKeyed("$undefined", jdecUndefined) + jsonExt.EncodeType(Undefined, jencUndefined) + + jsonExt.Extend(&funcExt) +} + +func fbytes(format string, args ...interface{}) []byte { + var buf bytes.Buffer + fmt.Fprintf(&buf, format, args...) + return buf.Bytes() +} + +func jdecBinary(data []byte) (interface{}, error) { + var v struct { + Binary []byte `json:"$binary"` + Type string `json:"$type"` + Func struct { + Binary []byte `json:"$binary"` + Type int64 `json:"$type"` + } `json:"$binaryFunc"` + } + err := jdec(data, &v) + if err != nil { + return nil, err + } + + var binData []byte + var binKind int64 + if v.Type == "" && v.Binary == nil { + binData = v.Func.Binary + binKind = v.Func.Type + } else if v.Type == "" { + return v.Binary, nil + } else { + binData = v.Binary + binKind, err = strconv.ParseInt(v.Type, 0, 64) + if err != nil { + binKind = -1 + } + } + + if binKind == 0 { + return binData, nil + } + if binKind < 0 || binKind > 255 { + return nil, fmt.Errorf("invalid type in binary object: %s", data) + } + + return Binary{Kind: byte(binKind), Data: binData}, nil +} + +func jencBinarySlice(v interface{}) ([]byte, error) { + in := v.([]byte) + out := make([]byte, base64.StdEncoding.EncodedLen(len(in))) + base64.StdEncoding.Encode(out, in) + return fbytes(`{"$binary":"%s","$type":"0x0"}`, out), nil +} + +func jencBinaryType(v interface{}) ([]byte, error) { + in := v.(Binary) + out := make([]byte, base64.StdEncoding.EncodedLen(len(in.Data))) + base64.StdEncoding.Encode(out, in.Data) + return fbytes(`{"$binary":"%s","$type":"0x%x"}`, out, in.Kind), nil +} + +const jdateFormat = "2006-01-02T15:04:05.999Z" + +func jdecDate(data []byte) (interface{}, error) { + var v struct { + S string `json:"$date"` + Func struct { + S string + } `json:"$dateFunc"` + } + _ = jdec(data, &v) + if v.S == "" { + v.S = v.Func.S + } + if v.S != "" { + for _, format := range []string{jdateFormat, "2006-01-02"} { + t, err := time.Parse(format, v.S) + if err == nil { + return t, nil + } + } + return nil, fmt.Errorf("cannot parse date: %q", v.S) + } + + var vn struct { + Date struct { + N int64 `json:"$numberLong,string"` + } `json:"$date"` + Func struct { + S int64 + } `json:"$dateFunc"` + } + err := jdec(data, &vn) + if err != nil { + return nil, fmt.Errorf("cannot parse date: %q", data) + } + n := vn.Date.N + if n == 0 { + n = vn.Func.S + } + return time.Unix(n/1000, n%1000*1e6).UTC(), nil +} + +func jencDate(v interface{}) ([]byte, error) { + t := v.(time.Time) + return fbytes(`{"$date":%q}`, t.Format(jdateFormat)), nil +} + +func jdecTimestamp(data []byte) (interface{}, error) { + var v struct { + Func struct { + T int32 `json:"t"` + I int32 `json:"i"` + } `json:"$timestamp"` + } + err := jdec(data, &v) + if err != nil { + return nil, err + } + return MongoTimestamp(uint64(v.Func.T)<<32 | uint64(uint32(v.Func.I))), nil +} + +func jencTimestamp(v interface{}) ([]byte, error) { + ts := uint64(v.(MongoTimestamp)) + return fbytes(`{"$timestamp":{"t":%d,"i":%d}}`, ts>>32, uint32(ts)), nil +} + +func jdecRegEx(data []byte) (interface{}, error) { + var v struct { + Regex string `json:"$regex"` + Options string `json:"$options"` + } + err := jdec(data, &v) + if err != nil { + return nil, err + } + return RegEx{v.Regex, v.Options}, nil +} + +func jencRegEx(v interface{}) ([]byte, error) { + re := v.(RegEx) + type regex struct { + Regex string `json:"$regex"` + Options string `json:"$options"` + } + return json.Marshal(regex{re.Pattern, re.Options}) +} + +func jdecObjectId(data []byte) (interface{}, error) { + var v struct { + Id string `json:"$oid"` + Func struct { + Id string + } `json:"$oidFunc"` + } + err := jdec(data, &v) + if err != nil { + return nil, err + } + if v.Id == "" { + v.Id = v.Func.Id + } + return ObjectIdHex(v.Id), nil +} + +func jencObjectId(v interface{}) ([]byte, error) { + return fbytes(`{"$oid":"%s"}`, v.(ObjectId).Hex()), nil +} + +func jdecDBRef(data []byte) (interface{}, error) { + // TODO Support unmarshaling $ref and $id into the input value. + var v struct { + Obj map[string]interface{} `json:"$dbrefFunc"` + } + // TODO Fix this. Must not be required. + v.Obj = make(map[string]interface{}) + err := jdec(data, &v) + if err != nil { + return nil, err + } + return v.Obj, nil +} + +func jdecNumberLong(data []byte) (interface{}, error) { + var v struct { + N int64 `json:"$numberLong,string"` + Func struct { + N int64 `json:",string"` + } `json:"$numberLongFunc"` + } + var vn struct { + N int64 `json:"$numberLong"` + Func struct { + N int64 + } `json:"$numberLongFunc"` + } + err := jdec(data, &v) + if err != nil { + err = jdec(data, &vn) + v.N = vn.N + v.Func.N = vn.Func.N + } + if err != nil { + return nil, err + } + if v.N != 0 { + return v.N, nil + } + return v.Func.N, nil +} + +func jencNumberLong(v interface{}) ([]byte, error) { + n := v.(int64) + f := `{"$numberLong":"%d"}` + if n <= 1<<53 { + f = `{"$numberLong":%d}` + } + return fbytes(f, n), nil +} + +func jencInt(v interface{}) ([]byte, error) { + n := v.(int) + f := `{"$numberLong":"%d"}` + if int64(n) <= 1<<53 { + f = `%d` + } + return fbytes(f, n), nil +} + +func jdecMinKey(data []byte) (interface{}, error) { + var v struct { + N int64 `json:"$minKey"` + } + err := jdec(data, &v) + if err != nil { + return nil, err + } + if v.N != 1 { + return nil, fmt.Errorf("invalid $minKey object: %s", data) + } + return MinKey, nil +} + +func jdecMaxKey(data []byte) (interface{}, error) { + var v struct { + N int64 `json:"$maxKey"` + } + err := jdec(data, &v) + if err != nil { + return nil, err + } + if v.N != 1 { + return nil, fmt.Errorf("invalid $maxKey object: %s", data) + } + return MaxKey, nil +} + +func jencMinMaxKey(v interface{}) ([]byte, error) { + switch v.(orderKey) { + case MinKey: + return []byte(`{"$minKey":1}`), nil + case MaxKey: + return []byte(`{"$maxKey":1}`), nil + } + panic(fmt.Sprintf("invalid $minKey/$maxKey value: %d", v)) +} + +func jdecUndefined(data []byte) (interface{}, error) { + var v struct { + B bool `json:"$undefined"` + } + err := jdec(data, &v) + if err != nil { + return nil, err + } + if !v.B { + return nil, fmt.Errorf("invalid $undefined object: %s", data) + } + return Undefined, nil +} + +func jencUndefined(v interface{}) ([]byte, error) { + return []byte(`{"$undefined":true}`), nil +} diff --git a/vendor/gopkg.in/mgo.v2/internal/json/LICENSE b/vendor/gopkg.in/mgo.v2/internal/json/LICENSE new file mode 100644 index 00000000..74487567 --- /dev/null +++ b/vendor/gopkg.in/mgo.v2/internal/json/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2012 The Go Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/gopkg.in/mgo.v2/internal/json/decode.go b/vendor/gopkg.in/mgo.v2/internal/json/decode.go new file mode 100644 index 00000000..ce7c7d24 --- /dev/null +++ b/vendor/gopkg.in/mgo.v2/internal/json/decode.go @@ -0,0 +1,1685 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Represents JSON data structure using native Go types: booleans, floats, +// strings, arrays, and maps. + +package json + +import ( + "bytes" + "encoding" + "encoding/base64" + "errors" + "fmt" + "reflect" + "runtime" + "strconv" + "unicode" + "unicode/utf16" + "unicode/utf8" +) + +// Unmarshal parses the JSON-encoded data and stores the result +// in the value pointed to by v. +// +// Unmarshal uses the inverse of the encodings that +// Marshal uses, allocating maps, slices, and pointers as necessary, +// with the following additional rules: +// +// To unmarshal JSON into a pointer, Unmarshal first handles the case of +// the JSON being the JSON literal null. In that case, Unmarshal sets +// the pointer to nil. Otherwise, Unmarshal unmarshals the JSON into +// the value pointed at by the pointer. If the pointer is nil, Unmarshal +// allocates a new value for it to point to. +// +// To unmarshal JSON into a struct, Unmarshal matches incoming object +// keys to the keys used by Marshal (either the struct field name or its tag), +// preferring an exact match but also accepting a case-insensitive match. +// Unmarshal will only set exported fields of the struct. +// +// To unmarshal JSON into an interface value, +// Unmarshal stores one of these in the interface value: +// +// bool, for JSON booleans +// float64, for JSON numbers +// string, for JSON strings +// []interface{}, for JSON arrays +// map[string]interface{}, for JSON objects +// nil for JSON null +// +// To unmarshal a JSON array into a slice, Unmarshal resets the slice length +// to zero and then appends each element to the slice. +// As a special case, to unmarshal an empty JSON array into a slice, +// Unmarshal replaces the slice with a new empty slice. +// +// To unmarshal a JSON array into a Go array, Unmarshal decodes +// JSON array elements into corresponding Go array elements. +// If the Go array is smaller than the JSON array, +// the additional JSON array elements are discarded. +// If the JSON array is smaller than the Go array, +// the additional Go array elements are set to zero values. +// +// To unmarshal a JSON object into a map, Unmarshal first establishes a map to +// use, If the map is nil, Unmarshal allocates a new map. Otherwise Unmarshal +// reuses the existing map, keeping existing entries. Unmarshal then stores key- +// value pairs from the JSON object into the map. The map's key type must +// either be a string or implement encoding.TextUnmarshaler. +// +// If a JSON value is not appropriate for a given target type, +// or if a JSON number overflows the target type, Unmarshal +// skips that field and completes the unmarshaling as best it can. +// If no more serious errors are encountered, Unmarshal returns +// an UnmarshalTypeError describing the earliest such error. +// +// The JSON null value unmarshals into an interface, map, pointer, or slice +// by setting that Go value to nil. Because null is often used in JSON to mean +// ``not present,'' unmarshaling a JSON null into any other Go type has no effect +// on the value and produces no error. +// +// When unmarshaling quoted strings, invalid UTF-8 or +// invalid UTF-16 surrogate pairs are not treated as an error. +// Instead, they are replaced by the Unicode replacement +// character U+FFFD. +// +func Unmarshal(data []byte, v interface{}) error { + // Check for well-formedness. + // Avoids filling out half a data structure + // before discovering a JSON syntax error. + var d decodeState + err := checkValid(data, &d.scan) + if err != nil { + return err + } + + d.init(data) + return d.unmarshal(v) +} + +// Unmarshaler is the interface implemented by types +// that can unmarshal a JSON description of themselves. +// The input can be assumed to be a valid encoding of +// a JSON value. UnmarshalJSON must copy the JSON data +// if it wishes to retain the data after returning. +type Unmarshaler interface { + UnmarshalJSON([]byte) error +} + +// An UnmarshalTypeError describes a JSON value that was +// not appropriate for a value of a specific Go type. +type UnmarshalTypeError struct { + Value string // description of JSON value - "bool", "array", "number -5" + Type reflect.Type // type of Go value it could not be assigned to + Offset int64 // error occurred after reading Offset bytes +} + +func (e *UnmarshalTypeError) Error() string { + return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String() +} + +// An UnmarshalFieldError describes a JSON object key that +// led to an unexported (and therefore unwritable) struct field. +// (No longer used; kept for compatibility.) +type UnmarshalFieldError struct { + Key string + Type reflect.Type + Field reflect.StructField +} + +func (e *UnmarshalFieldError) Error() string { + return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String() +} + +// An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. +// (The argument to Unmarshal must be a non-nil pointer.) +type InvalidUnmarshalError struct { + Type reflect.Type +} + +func (e *InvalidUnmarshalError) Error() string { + if e.Type == nil { + return "json: Unmarshal(nil)" + } + + if e.Type.Kind() != reflect.Ptr { + return "json: Unmarshal(non-pointer " + e.Type.String() + ")" + } + return "json: Unmarshal(nil " + e.Type.String() + ")" +} + +func (d *decodeState) unmarshal(v interface{}) (err error) { + defer func() { + if r := recover(); r != nil { + if _, ok := r.(runtime.Error); ok { + panic(r) + } + err = r.(error) + } + }() + + rv := reflect.ValueOf(v) + if rv.Kind() != reflect.Ptr || rv.IsNil() { + return &InvalidUnmarshalError{reflect.TypeOf(v)} + } + + d.scan.reset() + // We decode rv not rv.Elem because the Unmarshaler interface + // test must be applied at the top level of the value. + d.value(rv) + return d.savedError +} + +// A Number represents a JSON number literal. +type Number string + +// String returns the literal text of the number. +func (n Number) String() string { return string(n) } + +// Float64 returns the number as a float64. +func (n Number) Float64() (float64, error) { + return strconv.ParseFloat(string(n), 64) +} + +// Int64 returns the number as an int64. +func (n Number) Int64() (int64, error) { + return strconv.ParseInt(string(n), 10, 64) +} + +// isValidNumber reports whether s is a valid JSON number literal. +func isValidNumber(s string) bool { + // This function implements the JSON numbers grammar. + // See https://tools.ietf.org/html/rfc7159#section-6 + // and http://json.org/number.gif + + if s == "" { + return false + } + + // Optional - + if s[0] == '-' { + s = s[1:] + if s == "" { + return false + } + } + + // Digits + switch { + default: + return false + + case s[0] == '0': + s = s[1:] + + case '1' <= s[0] && s[0] <= '9': + s = s[1:] + for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { + s = s[1:] + } + } + + // . followed by 1 or more digits. + if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' { + s = s[2:] + for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { + s = s[1:] + } + } + + // e or E followed by an optional - or + and + // 1 or more digits. + if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') { + s = s[1:] + if s[0] == '+' || s[0] == '-' { + s = s[1:] + if s == "" { + return false + } + } + for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { + s = s[1:] + } + } + + // Make sure we are at the end. + return s == "" +} + +// decodeState represents the state while decoding a JSON value. +type decodeState struct { + data []byte + off int // read offset in data + scan scanner + nextscan scanner // for calls to nextValue + savedError error + useNumber bool + ext Extension +} + +// errPhase is used for errors that should not happen unless +// there is a bug in the JSON decoder or something is editing +// the data slice while the decoder executes. +var errPhase = errors.New("JSON decoder out of sync - data changing underfoot?") + +func (d *decodeState) init(data []byte) *decodeState { + d.data = data + d.off = 0 + d.savedError = nil + return d +} + +// error aborts the decoding by panicking with err. +func (d *decodeState) error(err error) { + panic(err) +} + +// saveError saves the first err it is called with, +// for reporting at the end of the unmarshal. +func (d *decodeState) saveError(err error) { + if d.savedError == nil { + d.savedError = err + } +} + +// next cuts off and returns the next full JSON value in d.data[d.off:]. +// The next value is known to be an object or array, not a literal. +func (d *decodeState) next() []byte { + c := d.data[d.off] + item, rest, err := nextValue(d.data[d.off:], &d.nextscan) + if err != nil { + d.error(err) + } + d.off = len(d.data) - len(rest) + + // Our scanner has seen the opening brace/bracket + // and thinks we're still in the middle of the object. + // invent a closing brace/bracket to get it out. + if c == '{' { + d.scan.step(&d.scan, '}') + } else if c == '[' { + d.scan.step(&d.scan, ']') + } else { + // Was inside a function name. Get out of it. + d.scan.step(&d.scan, '(') + d.scan.step(&d.scan, ')') + } + + return item +} + +// scanWhile processes bytes in d.data[d.off:] until it +// receives a scan code not equal to op. +// It updates d.off and returns the new scan code. +func (d *decodeState) scanWhile(op int) int { + var newOp int + for { + if d.off >= len(d.data) { + newOp = d.scan.eof() + d.off = len(d.data) + 1 // mark processed EOF with len+1 + } else { + c := d.data[d.off] + d.off++ + newOp = d.scan.step(&d.scan, c) + } + if newOp != op { + break + } + } + return newOp +} + +// value decodes a JSON value from d.data[d.off:] into the value. +// it updates d.off to point past the decoded value. +func (d *decodeState) value(v reflect.Value) { + if !v.IsValid() { + _, rest, err := nextValue(d.data[d.off:], &d.nextscan) + if err != nil { + d.error(err) + } + d.off = len(d.data) - len(rest) + + // d.scan thinks we're still at the beginning of the item. + // Feed in an empty string - the shortest, simplest value - + // so that it knows we got to the end of the value. + if d.scan.redo { + // rewind. + d.scan.redo = false + d.scan.step = stateBeginValue + } + d.scan.step(&d.scan, '"') + d.scan.step(&d.scan, '"') + + n := len(d.scan.parseState) + if n > 0 && d.scan.parseState[n-1] == parseObjectKey { + // d.scan thinks we just read an object key; finish the object + d.scan.step(&d.scan, ':') + d.scan.step(&d.scan, '"') + d.scan.step(&d.scan, '"') + d.scan.step(&d.scan, '}') + } + + return + } + + switch op := d.scanWhile(scanSkipSpace); op { + default: + d.error(errPhase) + + case scanBeginArray: + d.array(v) + + case scanBeginObject: + d.object(v) + + case scanBeginLiteral: + d.literal(v) + + case scanBeginName: + d.name(v) + } +} + +type unquotedValue struct{} + +// valueQuoted is like value but decodes a +// quoted string literal or literal null into an interface value. +// If it finds anything other than a quoted string literal or null, +// valueQuoted returns unquotedValue{}. +func (d *decodeState) valueQuoted() interface{} { + switch op := d.scanWhile(scanSkipSpace); op { + default: + d.error(errPhase) + + case scanBeginArray: + d.array(reflect.Value{}) + + case scanBeginObject: + d.object(reflect.Value{}) + + case scanBeginName: + switch v := d.nameInterface().(type) { + case nil, string: + return v + } + + case scanBeginLiteral: + switch v := d.literalInterface().(type) { + case nil, string: + return v + } + } + return unquotedValue{} +} + +// indirect walks down v allocating pointers as needed, +// until it gets to a non-pointer. +// if it encounters an Unmarshaler, indirect stops and returns that. +// if decodingNull is true, indirect stops at the last pointer so it can be set to nil. +func (d *decodeState) indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) { + // If v is a named type and is addressable, + // start with its address, so that if the type has pointer methods, + // we find them. + if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() { + v = v.Addr() + } + for { + // Load value from interface, but only if the result will be + // usefully addressable. + if v.Kind() == reflect.Interface && !v.IsNil() { + e := v.Elem() + if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) { + v = e + continue + } + } + + if v.Kind() != reflect.Ptr { + break + } + + if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() { + break + } + if v.IsNil() { + v.Set(reflect.New(v.Type().Elem())) + } + if v.Type().NumMethod() > 0 { + if u, ok := v.Interface().(Unmarshaler); ok { + return u, nil, v + } + if u, ok := v.Interface().(encoding.TextUnmarshaler); ok { + return nil, u, v + } + } + v = v.Elem() + } + return nil, nil, v +} + +// array consumes an array from d.data[d.off-1:], decoding into the value v. +// the first byte of the array ('[') has been read already. +func (d *decodeState) array(v reflect.Value) { + // Check for unmarshaler. + u, ut, pv := d.indirect(v, false) + if u != nil { + d.off-- + err := u.UnmarshalJSON(d.next()) + if err != nil { + d.error(err) + } + return + } + if ut != nil { + d.saveError(&UnmarshalTypeError{"array", v.Type(), int64(d.off)}) + d.off-- + d.next() + return + } + + v = pv + + // Check type of target. + switch v.Kind() { + case reflect.Interface: + if v.NumMethod() == 0 { + // Decoding into nil interface? Switch to non-reflect code. + v.Set(reflect.ValueOf(d.arrayInterface())) + return + } + // Otherwise it's invalid. + fallthrough + default: + d.saveError(&UnmarshalTypeError{"array", v.Type(), int64(d.off)}) + d.off-- + d.next() + return + case reflect.Array: + case reflect.Slice: + break + } + + i := 0 + for { + // Look ahead for ] - can only happen on first iteration. + op := d.scanWhile(scanSkipSpace) + if op == scanEndArray { + break + } + + // Back up so d.value can have the byte we just read. + d.off-- + d.scan.undo(op) + + // Get element of array, growing if necessary. + if v.Kind() == reflect.Slice { + // Grow slice if necessary + if i >= v.Cap() { + newcap := v.Cap() + v.Cap()/2 + if newcap < 4 { + newcap = 4 + } + newv := reflect.MakeSlice(v.Type(), v.Len(), newcap) + reflect.Copy(newv, v) + v.Set(newv) + } + if i >= v.Len() { + v.SetLen(i + 1) + } + } + + if i < v.Len() { + // Decode into element. + d.value(v.Index(i)) + } else { + // Ran out of fixed array: skip. + d.value(reflect.Value{}) + } + i++ + + // Next token must be , or ]. + op = d.scanWhile(scanSkipSpace) + if op == scanEndArray { + break + } + if op != scanArrayValue { + d.error(errPhase) + } + } + + if i < v.Len() { + if v.Kind() == reflect.Array { + // Array. Zero the rest. + z := reflect.Zero(v.Type().Elem()) + for ; i < v.Len(); i++ { + v.Index(i).Set(z) + } + } else { + v.SetLen(i) + } + } + if i == 0 && v.Kind() == reflect.Slice { + v.Set(reflect.MakeSlice(v.Type(), 0, 0)) + } +} + +var nullLiteral = []byte("null") +var textUnmarshalerType = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem() + +// object consumes an object from d.data[d.off-1:], decoding into the value v. +// the first byte ('{') of the object has been read already. +func (d *decodeState) object(v reflect.Value) { + // Check for unmarshaler. + u, ut, pv := d.indirect(v, false) + if d.storeKeyed(pv) { + return + } + if u != nil { + d.off-- + err := u.UnmarshalJSON(d.next()) + if err != nil { + d.error(err) + } + return + } + if ut != nil { + d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)}) + d.off-- + d.next() // skip over { } in input + return + } + v = pv + + // Decoding into nil interface? Switch to non-reflect code. + if v.Kind() == reflect.Interface && v.NumMethod() == 0 { + v.Set(reflect.ValueOf(d.objectInterface())) + return + } + + // Check type of target: + // struct or + // map[string]T or map[encoding.TextUnmarshaler]T + switch v.Kind() { + case reflect.Map: + // Map key must either have string kind or be an encoding.TextUnmarshaler. + t := v.Type() + if t.Key().Kind() != reflect.String && + !reflect.PtrTo(t.Key()).Implements(textUnmarshalerType) { + d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)}) + d.off-- + d.next() // skip over { } in input + return + } + if v.IsNil() { + v.Set(reflect.MakeMap(t)) + } + case reflect.Struct: + + default: + d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)}) + d.off-- + d.next() // skip over { } in input + return + } + + var mapElem reflect.Value + + empty := true + for { + // Read opening " of string key or closing }. + op := d.scanWhile(scanSkipSpace) + if op == scanEndObject { + if !empty && !d.ext.trailingCommas { + d.syntaxError("beginning of object key string") + } + break + } + empty = false + if op == scanBeginName { + if !d.ext.unquotedKeys { + d.syntaxError("beginning of object key string") + } + } else if op != scanBeginLiteral { + d.error(errPhase) + } + unquotedKey := op == scanBeginName + + // Read key. + start := d.off - 1 + op = d.scanWhile(scanContinue) + item := d.data[start : d.off-1] + var key []byte + if unquotedKey { + key = item + // TODO Fix code below to quote item when necessary. + } else { + var ok bool + key, ok = unquoteBytes(item) + if !ok { + d.error(errPhase) + } + } + + // Figure out field corresponding to key. + var subv reflect.Value + destring := false // whether the value is wrapped in a string to be decoded first + + if v.Kind() == reflect.Map { + elemType := v.Type().Elem() + if !mapElem.IsValid() { + mapElem = reflect.New(elemType).Elem() + } else { + mapElem.Set(reflect.Zero(elemType)) + } + subv = mapElem + } else { + var f *field + fields := cachedTypeFields(v.Type()) + for i := range fields { + ff := &fields[i] + if bytes.Equal(ff.nameBytes, key) { + f = ff + break + } + if f == nil && ff.equalFold(ff.nameBytes, key) { + f = ff + } + } + if f != nil { + subv = v + destring = f.quoted + for _, i := range f.index { + if subv.Kind() == reflect.Ptr { + if subv.IsNil() { + subv.Set(reflect.New(subv.Type().Elem())) + } + subv = subv.Elem() + } + subv = subv.Field(i) + } + } + } + + // Read : before value. + if op == scanSkipSpace { + op = d.scanWhile(scanSkipSpace) + } + if op != scanObjectKey { + d.error(errPhase) + } + + // Read value. + if destring { + switch qv := d.valueQuoted().(type) { + case nil: + d.literalStore(nullLiteral, subv, false) + case string: + d.literalStore([]byte(qv), subv, true) + default: + d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into %v", subv.Type())) + } + } else { + d.value(subv) + } + + // Write value back to map; + // if using struct, subv points into struct already. + if v.Kind() == reflect.Map { + kt := v.Type().Key() + var kv reflect.Value + switch { + case kt.Kind() == reflect.String: + kv = reflect.ValueOf(key).Convert(v.Type().Key()) + case reflect.PtrTo(kt).Implements(textUnmarshalerType): + kv = reflect.New(v.Type().Key()) + d.literalStore(item, kv, true) + kv = kv.Elem() + default: + panic("json: Unexpected key type") // should never occur + } + v.SetMapIndex(kv, subv) + } + + // Next token must be , or }. + op = d.scanWhile(scanSkipSpace) + if op == scanEndObject { + break + } + if op != scanObjectValue { + d.error(errPhase) + } + } +} + +// isNull returns whether there's a null literal at the provided offset. +func (d *decodeState) isNull(off int) bool { + if off+4 >= len(d.data) || d.data[off] != 'n' || d.data[off+1] != 'u' || d.data[off+2] != 'l' || d.data[off+3] != 'l' { + return false + } + d.nextscan.reset() + for i, c := range d.data[off:] { + if i > 4 { + return false + } + switch d.nextscan.step(&d.nextscan, c) { + case scanContinue, scanBeginName: + continue + } + break + } + return true +} + +// name consumes a const or function from d.data[d.off-1:], decoding into the value v. +// the first byte of the function name has been read already. +func (d *decodeState) name(v reflect.Value) { + if d.isNull(d.off-1) { + d.literal(v) + return + } + + // Check for unmarshaler. + u, ut, pv := d.indirect(v, false) + if d.storeKeyed(pv) { + return + } + if u != nil { + d.off-- + err := u.UnmarshalJSON(d.next()) + if err != nil { + d.error(err) + } + return + } + if ut != nil { + d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)}) + d.off-- + d.next() // skip over function in input + return + } + v = pv + + // Decoding into nil interface? Switch to non-reflect code. + if v.Kind() == reflect.Interface && v.NumMethod() == 0 { + out := d.nameInterface() + if out == nil { + v.Set(reflect.Zero(v.Type())) + } else { + v.Set(reflect.ValueOf(out)) + } + return + } + + nameStart := d.off - 1 + + op := d.scanWhile(scanContinue) + + name := d.data[nameStart : d.off-1] + if op != scanParam { + // Back up so the byte just read is consumed next. + d.off-- + d.scan.undo(op) + if l, ok := d.convertLiteral(name); ok { + d.storeValue(v, l) + return + } + d.error(&SyntaxError{fmt.Sprintf("json: unknown constant %q", name), int64(d.off)}) + } + + funcName := string(name) + funcData := d.ext.funcs[funcName] + if funcData.key == "" { + d.error(fmt.Errorf("json: unknown function %q", funcName)) + } + + // Check type of target: + // struct or + // map[string]T or map[encoding.TextUnmarshaler]T + switch v.Kind() { + case reflect.Map: + // Map key must either have string kind or be an encoding.TextUnmarshaler. + t := v.Type() + if t.Key().Kind() != reflect.String && + !reflect.PtrTo(t.Key()).Implements(textUnmarshalerType) { + d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)}) + d.off-- + d.next() // skip over { } in input + return + } + if v.IsNil() { + v.Set(reflect.MakeMap(t)) + } + case reflect.Struct: + + default: + d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)}) + d.off-- + d.next() // skip over { } in input + return + } + + // TODO Fix case of func field as map. + //topv := v + + // Figure out field corresponding to function. + key := []byte(funcData.key) + if v.Kind() == reflect.Map { + elemType := v.Type().Elem() + v = reflect.New(elemType).Elem() + } else { + var f *field + fields := cachedTypeFields(v.Type()) + for i := range fields { + ff := &fields[i] + if bytes.Equal(ff.nameBytes, key) { + f = ff + break + } + if f == nil && ff.equalFold(ff.nameBytes, key) { + f = ff + } + } + if f != nil { + for _, i := range f.index { + if v.Kind() == reflect.Ptr { + if v.IsNil() { + v.Set(reflect.New(v.Type().Elem())) + } + v = v.Elem() + } + v = v.Field(i) + } + if v.Kind() == reflect.Ptr { + if v.IsNil() { + v.Set(reflect.New(v.Type().Elem())) + } + v = v.Elem() + } + } + } + + // Check for unmarshaler on func field itself. + u, ut, pv = d.indirect(v, false) + if u != nil { + d.off = nameStart + err := u.UnmarshalJSON(d.next()) + if err != nil { + d.error(err) + } + return + } + + var mapElem reflect.Value + + // Parse function arguments. + for i := 0; ; i++ { + // closing ) - can only happen on first iteration. + op := d.scanWhile(scanSkipSpace) + if op == scanEndParams { + break + } + + // Back up so d.value can have the byte we just read. + d.off-- + d.scan.undo(op) + + if i >= len(funcData.args) { + d.error(fmt.Errorf("json: too many arguments for function %s", funcName)) + } + key := []byte(funcData.args[i]) + + // Figure out field corresponding to key. + var subv reflect.Value + destring := false // whether the value is wrapped in a string to be decoded first + + if v.Kind() == reflect.Map { + elemType := v.Type().Elem() + if !mapElem.IsValid() { + mapElem = reflect.New(elemType).Elem() + } else { + mapElem.Set(reflect.Zero(elemType)) + } + subv = mapElem + } else { + var f *field + fields := cachedTypeFields(v.Type()) + for i := range fields { + ff := &fields[i] + if bytes.Equal(ff.nameBytes, key) { + f = ff + break + } + if f == nil && ff.equalFold(ff.nameBytes, key) { + f = ff + } + } + if f != nil { + subv = v + destring = f.quoted + for _, i := range f.index { + if subv.Kind() == reflect.Ptr { + if subv.IsNil() { + subv.Set(reflect.New(subv.Type().Elem())) + } + subv = subv.Elem() + } + subv = subv.Field(i) + } + } + } + + // Read value. + if destring { + switch qv := d.valueQuoted().(type) { + case nil: + d.literalStore(nullLiteral, subv, false) + case string: + d.literalStore([]byte(qv), subv, true) + default: + d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into %v", subv.Type())) + } + } else { + d.value(subv) + } + + // Write value back to map; + // if using struct, subv points into struct already. + if v.Kind() == reflect.Map { + kt := v.Type().Key() + var kv reflect.Value + switch { + case kt.Kind() == reflect.String: + kv = reflect.ValueOf(key).Convert(v.Type().Key()) + case reflect.PtrTo(kt).Implements(textUnmarshalerType): + kv = reflect.New(v.Type().Key()) + d.literalStore(key, kv, true) + kv = kv.Elem() + default: + panic("json: Unexpected key type") // should never occur + } + v.SetMapIndex(kv, subv) + } + + // Next token must be , or ). + op = d.scanWhile(scanSkipSpace) + if op == scanEndParams { + break + } + if op != scanParam { + d.error(errPhase) + } + } +} + +// keyed attempts to decode an object or function using a keyed doc extension, +// and returns the value and true on success, or nil and false otherwise. +func (d *decodeState) keyed() (interface{}, bool) { + if len(d.ext.keyed) == 0 { + return nil, false + } + + unquote := false + + // Look-ahead first key to check for a keyed document extension. + d.nextscan.reset() + var start, end int + for i, c := range d.data[d.off-1:] { + switch op := d.nextscan.step(&d.nextscan, c); op { + case scanSkipSpace, scanContinue, scanBeginObject: + continue + case scanBeginLiteral, scanBeginName: + unquote = op == scanBeginLiteral + start = i + continue + } + end = i + break + } + + name := d.data[d.off-1+start : d.off-1+end] + + var key []byte + var ok bool + if unquote { + key, ok = unquoteBytes(name) + if !ok { + d.error(errPhase) + } + } else { + funcData, ok := d.ext.funcs[string(name)] + if !ok { + return nil, false + } + key = []byte(funcData.key) + } + + decode, ok := d.ext.keyed[string(key)] + if !ok { + return nil, false + } + + d.off-- + out, err := decode(d.next()) + if err != nil { + d.error(err) + } + return out, true +} + +func (d *decodeState) storeKeyed(v reflect.Value) bool { + keyed, ok := d.keyed() + if !ok { + return false + } + d.storeValue(v, keyed) + return true +} + +var ( + trueBytes = []byte("true") + falseBytes = []byte("false") + nullBytes = []byte("null") +) + +func (d *decodeState) storeValue(v reflect.Value, from interface{}) { + switch from { + case nil: + d.literalStore(nullBytes, v, false) + return + case true: + d.literalStore(trueBytes, v, false) + return + case false: + d.literalStore(falseBytes, v, false) + return + } + fromv := reflect.ValueOf(from) + for fromv.Kind() == reflect.Ptr && !fromv.IsNil() { + fromv = fromv.Elem() + } + fromt := fromv.Type() + for v.Kind() == reflect.Ptr && !v.IsNil() { + v = v.Elem() + } + vt := v.Type() + if fromt.AssignableTo(vt) { + v.Set(fromv) + } else if fromt.ConvertibleTo(vt) { + v.Set(fromv.Convert(vt)) + } else { + d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)}) + } +} + +func (d *decodeState) convertLiteral(name []byte) (interface{}, bool) { + if len(name) == 0 { + return nil, false + } + switch name[0] { + case 't': + if bytes.Equal(name, trueBytes) { + return true, true + } + case 'f': + if bytes.Equal(name, falseBytes) { + return false, true + } + case 'n': + if bytes.Equal(name, nullBytes) { + return nil, true + } + } + if l, ok := d.ext.consts[string(name)]; ok { + return l, true + } + return nil, false +} + +// literal consumes a literal from d.data[d.off-1:], decoding into the value v. +// The first byte of the literal has been read already +// (that's how the caller knows it's a literal). +func (d *decodeState) literal(v reflect.Value) { + // All bytes inside literal return scanContinue op code. + start := d.off - 1 + op := d.scanWhile(scanContinue) + + // Scan read one byte too far; back up. + d.off-- + d.scan.undo(op) + + d.literalStore(d.data[start:d.off], v, false) +} + +// convertNumber converts the number literal s to a float64 or a Number +// depending on the setting of d.useNumber. +func (d *decodeState) convertNumber(s string) (interface{}, error) { + if d.useNumber { + return Number(s), nil + } + f, err := strconv.ParseFloat(s, 64) + if err != nil { + return nil, &UnmarshalTypeError{"number " + s, reflect.TypeOf(0.0), int64(d.off)} + } + return f, nil +} + +var numberType = reflect.TypeOf(Number("")) + +// literalStore decodes a literal stored in item into v. +// +// fromQuoted indicates whether this literal came from unwrapping a +// string from the ",string" struct tag option. this is used only to +// produce more helpful error messages. +func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) { + // Check for unmarshaler. + if len(item) == 0 { + //Empty string given + d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) + return + } + wantptr := item[0] == 'n' // null + u, ut, pv := d.indirect(v, wantptr) + if u != nil { + err := u.UnmarshalJSON(item) + if err != nil { + d.error(err) + } + return + } + if ut != nil { + if item[0] != '"' { + if fromQuoted { + d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) + } else { + d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)}) + } + return + } + s, ok := unquoteBytes(item) + if !ok { + if fromQuoted { + d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) + } else { + d.error(errPhase) + } + } + err := ut.UnmarshalText(s) + if err != nil { + d.error(err) + } + return + } + + v = pv + + switch c := item[0]; c { + case 'n': // null + switch v.Kind() { + case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice: + v.Set(reflect.Zero(v.Type())) + // otherwise, ignore null for primitives/string + } + case 't', 'f': // true, false + value := c == 't' + switch v.Kind() { + default: + if fromQuoted { + d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) + } else { + d.saveError(&UnmarshalTypeError{"bool", v.Type(), int64(d.off)}) + } + case reflect.Bool: + v.SetBool(value) + case reflect.Interface: + if v.NumMethod() == 0 { + v.Set(reflect.ValueOf(value)) + } else { + d.saveError(&UnmarshalTypeError{"bool", v.Type(), int64(d.off)}) + } + } + + case '"': // string + s, ok := unquoteBytes(item) + if !ok { + if fromQuoted { + d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) + } else { + d.error(errPhase) + } + } + switch v.Kind() { + default: + d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)}) + case reflect.Slice: + if v.Type().Elem().Kind() != reflect.Uint8 { + d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)}) + break + } + b := make([]byte, base64.StdEncoding.DecodedLen(len(s))) + n, err := base64.StdEncoding.Decode(b, s) + if err != nil { + d.saveError(err) + break + } + v.SetBytes(b[:n]) + case reflect.String: + v.SetString(string(s)) + case reflect.Interface: + if v.NumMethod() == 0 { + v.Set(reflect.ValueOf(string(s))) + } else { + d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)}) + } + } + + default: // number + if c != '-' && (c < '0' || c > '9') { + if fromQuoted { + d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) + } else { + d.error(errPhase) + } + } + s := string(item) + switch v.Kind() { + default: + if v.Kind() == reflect.String && v.Type() == numberType { + v.SetString(s) + if !isValidNumber(s) { + d.error(fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", item)) + } + break + } + if fromQuoted { + d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) + } else { + d.error(&UnmarshalTypeError{"number", v.Type(), int64(d.off)}) + } + case reflect.Interface: + n, err := d.convertNumber(s) + if err != nil { + d.saveError(err) + break + } + if v.NumMethod() != 0 { + d.saveError(&UnmarshalTypeError{"number", v.Type(), int64(d.off)}) + break + } + v.Set(reflect.ValueOf(n)) + + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + n, err := strconv.ParseInt(s, 10, 64) + if err != nil || v.OverflowInt(n) { + d.saveError(&UnmarshalTypeError{"number " + s, v.Type(), int64(d.off)}) + break + } + v.SetInt(n) + + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + n, err := strconv.ParseUint(s, 10, 64) + if err != nil || v.OverflowUint(n) { + d.saveError(&UnmarshalTypeError{"number " + s, v.Type(), int64(d.off)}) + break + } + v.SetUint(n) + + case reflect.Float32, reflect.Float64: + n, err := strconv.ParseFloat(s, v.Type().Bits()) + if err != nil || v.OverflowFloat(n) { + d.saveError(&UnmarshalTypeError{"number " + s, v.Type(), int64(d.off)}) + break + } + v.SetFloat(n) + } + } +} + +// The xxxInterface routines build up a value to be stored +// in an empty interface. They are not strictly necessary, +// but they avoid the weight of reflection in this common case. + +// valueInterface is like value but returns interface{} +func (d *decodeState) valueInterface() interface{} { + switch d.scanWhile(scanSkipSpace) { + default: + d.error(errPhase) + panic("unreachable") + case scanBeginArray: + return d.arrayInterface() + case scanBeginObject: + return d.objectInterface() + case scanBeginLiteral: + return d.literalInterface() + case scanBeginName: + return d.nameInterface() + } +} + +func (d *decodeState) syntaxError(expected string) { + msg := fmt.Sprintf("invalid character '%c' looking for %s", d.data[d.off-1], expected) + d.error(&SyntaxError{msg, int64(d.off)}) +} + +// arrayInterface is like array but returns []interface{}. +func (d *decodeState) arrayInterface() []interface{} { + var v = make([]interface{}, 0) + for { + // Look ahead for ] - can only happen on first iteration. + op := d.scanWhile(scanSkipSpace) + if op == scanEndArray { + if len(v) > 0 && !d.ext.trailingCommas { + d.syntaxError("beginning of value") + } + break + } + + // Back up so d.value can have the byte we just read. + d.off-- + d.scan.undo(op) + + v = append(v, d.valueInterface()) + + // Next token must be , or ]. + op = d.scanWhile(scanSkipSpace) + if op == scanEndArray { + break + } + if op != scanArrayValue { + d.error(errPhase) + } + } + return v +} + +// objectInterface is like object but returns map[string]interface{}. +func (d *decodeState) objectInterface() interface{} { + v, ok := d.keyed() + if ok { + return v + } + + m := make(map[string]interface{}) + for { + // Read opening " of string key or closing }. + op := d.scanWhile(scanSkipSpace) + if op == scanEndObject { + if len(m) > 0 && !d.ext.trailingCommas { + d.syntaxError("beginning of object key string") + } + break + } + if op == scanBeginName { + if !d.ext.unquotedKeys { + d.syntaxError("beginning of object key string") + } + } else if op != scanBeginLiteral { + d.error(errPhase) + } + unquotedKey := op == scanBeginName + + // Read string key. + start := d.off - 1 + op = d.scanWhile(scanContinue) + item := d.data[start : d.off-1] + var key string + if unquotedKey { + key = string(item) + } else { + var ok bool + key, ok = unquote(item) + if !ok { + d.error(errPhase) + } + } + + // Read : before value. + if op == scanSkipSpace { + op = d.scanWhile(scanSkipSpace) + } + if op != scanObjectKey { + d.error(errPhase) + } + + // Read value. + m[key] = d.valueInterface() + + // Next token must be , or }. + op = d.scanWhile(scanSkipSpace) + if op == scanEndObject { + break + } + if op != scanObjectValue { + d.error(errPhase) + } + } + return m +} + +// literalInterface is like literal but returns an interface value. +func (d *decodeState) literalInterface() interface{} { + // All bytes inside literal return scanContinue op code. + start := d.off - 1 + op := d.scanWhile(scanContinue) + + // Scan read one byte too far; back up. + d.off-- + d.scan.undo(op) + item := d.data[start:d.off] + + switch c := item[0]; c { + case 'n': // null + return nil + + case 't', 'f': // true, false + return c == 't' + + case '"': // string + s, ok := unquote(item) + if !ok { + d.error(errPhase) + } + return s + + default: // number + if c != '-' && (c < '0' || c > '9') { + d.error(errPhase) + } + n, err := d.convertNumber(string(item)) + if err != nil { + d.saveError(err) + } + return n + } +} + +// nameInterface is like function but returns map[string]interface{}. +func (d *decodeState) nameInterface() interface{} { + v, ok := d.keyed() + if ok { + return v + } + + nameStart := d.off - 1 + + op := d.scanWhile(scanContinue) + + name := d.data[nameStart : d.off-1] + if op != scanParam { + // Back up so the byte just read is consumed next. + d.off-- + d.scan.undo(op) + if l, ok := d.convertLiteral(name); ok { + return l + } + d.error(&SyntaxError{fmt.Sprintf("json: unknown constant %q", name), int64(d.off)}) + } + + funcName := string(name) + funcData := d.ext.funcs[funcName] + if funcData.key == "" { + d.error(fmt.Errorf("json: unknown function %q", funcName)) + } + + m := make(map[string]interface{}) + for i := 0; ; i++ { + // Look ahead for ) - can only happen on first iteration. + op := d.scanWhile(scanSkipSpace) + if op == scanEndParams { + break + } + + // Back up so d.value can have the byte we just read. + d.off-- + d.scan.undo(op) + + if i >= len(funcData.args) { + d.error(fmt.Errorf("json: too many arguments for function %s", funcName)) + } + m[funcData.args[i]] = d.valueInterface() + + // Next token must be , or ). + op = d.scanWhile(scanSkipSpace) + if op == scanEndParams { + break + } + if op != scanParam { + d.error(errPhase) + } + } + return map[string]interface{}{funcData.key: m} +} + +// getu4 decodes \uXXXX from the beginning of s, returning the hex value, +// or it returns -1. +func getu4(s []byte) rune { + if len(s) < 6 || s[0] != '\\' || s[1] != 'u' { + return -1 + } + r, err := strconv.ParseUint(string(s[2:6]), 16, 64) + if err != nil { + return -1 + } + return rune(r) +} + +// unquote converts a quoted JSON string literal s into an actual string t. +// The rules are different than for Go, so cannot use strconv.Unquote. +func unquote(s []byte) (t string, ok bool) { + s, ok = unquoteBytes(s) + t = string(s) + return +} + +func unquoteBytes(s []byte) (t []byte, ok bool) { + if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' { + return + } + s = s[1 : len(s)-1] + + // Check for unusual characters. If there are none, + // then no unquoting is needed, so return a slice of the + // original bytes. + r := 0 + for r < len(s) { + c := s[r] + if c == '\\' || c == '"' || c < ' ' { + break + } + if c < utf8.RuneSelf { + r++ + continue + } + rr, size := utf8.DecodeRune(s[r:]) + if rr == utf8.RuneError && size == 1 { + break + } + r += size + } + if r == len(s) { + return s, true + } + + b := make([]byte, len(s)+2*utf8.UTFMax) + w := copy(b, s[0:r]) + for r < len(s) { + // Out of room? Can only happen if s is full of + // malformed UTF-8 and we're replacing each + // byte with RuneError. + if w >= len(b)-2*utf8.UTFMax { + nb := make([]byte, (len(b)+utf8.UTFMax)*2) + copy(nb, b[0:w]) + b = nb + } + switch c := s[r]; { + case c == '\\': + r++ + if r >= len(s) { + return + } + switch s[r] { + default: + return + case '"', '\\', '/', '\'': + b[w] = s[r] + r++ + w++ + case 'b': + b[w] = '\b' + r++ + w++ + case 'f': + b[w] = '\f' + r++ + w++ + case 'n': + b[w] = '\n' + r++ + w++ + case 'r': + b[w] = '\r' + r++ + w++ + case 't': + b[w] = '\t' + r++ + w++ + case 'u': + r-- + rr := getu4(s[r:]) + if rr < 0 { + return + } + r += 6 + if utf16.IsSurrogate(rr) { + rr1 := getu4(s[r:]) + if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar { + // A valid pair; consume. + r += 6 + w += utf8.EncodeRune(b[w:], dec) + break + } + // Invalid surrogate; fall back to replacement rune. + rr = unicode.ReplacementChar + } + w += utf8.EncodeRune(b[w:], rr) + } + + // Quote, control characters are invalid. + case c == '"', c < ' ': + return + + // ASCII + case c < utf8.RuneSelf: + b[w] = c + r++ + w++ + + // Coerce to well-formed UTF-8. + default: + rr, size := utf8.DecodeRune(s[r:]) + r += size + w += utf8.EncodeRune(b[w:], rr) + } + } + return b[0:w], true +} diff --git a/vendor/gopkg.in/mgo.v2/internal/json/encode.go b/vendor/gopkg.in/mgo.v2/internal/json/encode.go new file mode 100644 index 00000000..67a0f006 --- /dev/null +++ b/vendor/gopkg.in/mgo.v2/internal/json/encode.go @@ -0,0 +1,1256 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package json implements encoding and decoding of JSON as defined in +// RFC 4627. The mapping between JSON and Go values is described +// in the documentation for the Marshal and Unmarshal functions. +// +// See "JSON and Go" for an introduction to this package: +// https://golang.org/doc/articles/json_and_go.html +package json + +import ( + "bytes" + "encoding" + "encoding/base64" + "fmt" + "math" + "reflect" + "runtime" + "sort" + "strconv" + "strings" + "sync" + "unicode" + "unicode/utf8" +) + +// Marshal returns the JSON encoding of v. +// +// Marshal traverses the value v recursively. +// If an encountered value implements the Marshaler interface +// and is not a nil pointer, Marshal calls its MarshalJSON method +// to produce JSON. If no MarshalJSON method is present but the +// value implements encoding.TextMarshaler instead, Marshal calls +// its MarshalText method. +// The nil pointer exception is not strictly necessary +// but mimics a similar, necessary exception in the behavior of +// UnmarshalJSON. +// +// Otherwise, Marshal uses the following type-dependent default encodings: +// +// Boolean values encode as JSON booleans. +// +// Floating point, integer, and Number values encode as JSON numbers. +// +// String values encode as JSON strings coerced to valid UTF-8, +// replacing invalid bytes with the Unicode replacement rune. +// The angle brackets "<" and ">" are escaped to "\u003c" and "\u003e" +// to keep some browsers from misinterpreting JSON output as HTML. +// Ampersand "&" is also escaped to "\u0026" for the same reason. +// This escaping can be disabled using an Encoder with DisableHTMLEscaping. +// +// Array and slice values encode as JSON arrays, except that +// []byte encodes as a base64-encoded string, and a nil slice +// encodes as the null JSON value. +// +// Struct values encode as JSON objects. Each exported struct field +// becomes a member of the object unless +// - the field's tag is "-", or +// - the field is empty and its tag specifies the "omitempty" option. +// The empty values are false, 0, any +// nil pointer or interface value, and any array, slice, map, or string of +// length zero. The object's default key string is the struct field name +// but can be specified in the struct field's tag value. The "json" key in +// the struct field's tag value is the key name, followed by an optional comma +// and options. Examples: +// +// // Field is ignored by this package. +// Field int `json:"-"` +// +// // Field appears in JSON as key "myName". +// Field int `json:"myName"` +// +// // Field appears in JSON as key "myName" and +// // the field is omitted from the object if its value is empty, +// // as defined above. +// Field int `json:"myName,omitempty"` +// +// // Field appears in JSON as key "Field" (the default), but +// // the field is skipped if empty. +// // Note the leading comma. +// Field int `json:",omitempty"` +// +// The "string" option signals that a field is stored as JSON inside a +// JSON-encoded string. It applies only to fields of string, floating point, +// integer, or boolean types. This extra level of encoding is sometimes used +// when communicating with JavaScript programs: +// +// Int64String int64 `json:",string"` +// +// The key name will be used if it's a non-empty string consisting of +// only Unicode letters, digits, dollar signs, percent signs, hyphens, +// underscores and slashes. +// +// Anonymous struct fields are usually marshaled as if their inner exported fields +// were fields in the outer struct, subject to the usual Go visibility rules amended +// as described in the next paragraph. +// An anonymous struct field with a name given in its JSON tag is treated as +// having that name, rather than being anonymous. +// An anonymous struct field of interface type is treated the same as having +// that type as its name, rather than being anonymous. +// +// The Go visibility rules for struct fields are amended for JSON when +// deciding which field to marshal or unmarshal. If there are +// multiple fields at the same level, and that level is the least +// nested (and would therefore be the nesting level selected by the +// usual Go rules), the following extra rules apply: +// +// 1) Of those fields, if any are JSON-tagged, only tagged fields are considered, +// even if there are multiple untagged fields that would otherwise conflict. +// 2) If there is exactly one field (tagged or not according to the first rule), that is selected. +// 3) Otherwise there are multiple fields, and all are ignored; no error occurs. +// +// Handling of anonymous struct fields is new in Go 1.1. +// Prior to Go 1.1, anonymous struct fields were ignored. To force ignoring of +// an anonymous struct field in both current and earlier versions, give the field +// a JSON tag of "-". +// +// Map values encode as JSON objects. The map's key type must either be a string +// or implement encoding.TextMarshaler. The map keys are used as JSON object +// keys, subject to the UTF-8 coercion described for string values above. +// +// Pointer values encode as the value pointed to. +// A nil pointer encodes as the null JSON value. +// +// Interface values encode as the value contained in the interface. +// A nil interface value encodes as the null JSON value. +// +// Channel, complex, and function values cannot be encoded in JSON. +// Attempting to encode such a value causes Marshal to return +// an UnsupportedTypeError. +// +// JSON cannot represent cyclic data structures and Marshal does not +// handle them. Passing cyclic structures to Marshal will result in +// an infinite recursion. +// +func Marshal(v interface{}) ([]byte, error) { + e := &encodeState{} + err := e.marshal(v, encOpts{escapeHTML: true}) + if err != nil { + return nil, err + } + return e.Bytes(), nil +} + +// MarshalIndent is like Marshal but applies Indent to format the output. +func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) { + b, err := Marshal(v) + if err != nil { + return nil, err + } + var buf bytes.Buffer + err = Indent(&buf, b, prefix, indent) + if err != nil { + return nil, err + } + return buf.Bytes(), nil +} + +// HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029 +// characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029 +// so that the JSON will be safe to embed inside HTML