From a773ce78f6871e7763b3e6602361ebfdb9356b12 Mon Sep 17 00:00:00 2001 From: Lynwee Date: Thu, 12 Oct 2023 16:15:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(helper):=20update=20`ScopeRes`=20struct,=20?= =?UTF-8?q?fix=20empty=20time=20field=20when=20deco=E2=80=A6=20(#6230)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(helper): update `ScopeRes` struct, fix empty time field when decoding by mapstructure * fix(ci): fix ci error when checking apache license header --- .../workflows/grafana-dashboards-check.yml | 16 +++++++++++++ .../pluginhelper/api/scope_generic_helper.go | 23 +++---------------- .../helpers/pluginhelper/api/scope_helper.go | 4 ++-- .../services/remote/plugin/scope_api.go | 2 +- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/.github/workflows/grafana-dashboards-check.yml b/.github/workflows/grafana-dashboards-check.yml index 5dfa33aecee..83c428e8b0e 100644 --- a/.github/workflows/grafana-dashboards-check.yml +++ b/.github/workflows/grafana-dashboards-check.yml @@ -1,3 +1,19 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: check-grafana-dashboards on: diff --git a/backend/helpers/pluginhelper/api/scope_generic_helper.go b/backend/helpers/pluginhelper/api/scope_generic_helper.go index 4b6d7181730..a12bd8ee74f 100644 --- a/backend/helpers/pluginhelper/api/scope_generic_helper.go +++ b/backend/helpers/pluginhelper/api/scope_generic_helper.go @@ -18,7 +18,6 @@ limitations under the License. package api import ( - "encoding/json" "fmt" "reflect" "strconv" @@ -36,7 +35,6 @@ import ( "github.com/apache/incubator-devlake/helpers/dbhelper" serviceHelper "github.com/apache/incubator-devlake/helpers/pluginhelper/services" "github.com/go-playground/validator/v10" - "github.com/mitchellh/mapstructure" ) type NoScopeConfig struct{} @@ -64,8 +62,9 @@ type ( // Alias, for swagger purposes ScopeRefDoc = serviceHelper.BlueprintProjectPairs ScopeRes[Scope plugin.ToolLayerScope, ScopeConfig any] struct { - Scope Scope `mapstructure:",squash"` // ideally we need this field to be embedded in the struct - ScopeResDoc[ScopeConfig] `mapstructure:",squash"` // however, only this type of embeding is supported as of golang 1.20 + Scope Scope `mapstructure:"scope,omitempty" json:"scope,omitempty"` + ScopeConfig *ScopeConfig `mapstructure:"scopeConfig,omitempty" json:"scopeConfig,omitempty"` + Blueprints []*models.Blueprint `mapstructure:"blueprints,omitempty" json:"blueprints,omitempty"` } ScopeListRes[Scope plugin.ToolLayerScope, ScopeConfig any] struct { Scopes []*ScopeRes[Scope, ScopeConfig] `mapstructure:"scopes" json:"scopes"` @@ -601,22 +600,6 @@ func (gs *GenericScopeApiHelper[Conn, Scope, ScopeConfig]) transactionalDelete(t return nil } -// Implement MarshalJSON method to flatten all fields -func (sr *ScopeRes[T, Y]) MarshalJSON() ([]byte, error) { - var flatMap map[string]interface{} - err := mapstructure.Decode(sr, &flatMap) - if err != nil { - return nil, err - } - // Encode the flattened map to JSON - result, err := json.Marshal(flatMap) - if err != nil { - return nil, err - } - - return result, nil -} - func (gs *GenericScopeApiHelper[Conn, Scope, ScopeConfig]) getAffectedTables(pluginName string) ([]string, errors.Error) { var tables []string meta, err := plugin.GetPlugin(pluginName) diff --git a/backend/helpers/pluginhelper/api/scope_helper.go b/backend/helpers/pluginhelper/api/scope_helper.go index 87df08fd3b6..6474c47fee8 100644 --- a/backend/helpers/pluginhelper/api/scope_helper.go +++ b/backend/helpers/pluginhelper/api/scope_helper.go @@ -88,11 +88,11 @@ func (c *ScopeApiHelper[Conn, Scope, Tr]) GetScopeList(input *plugin.ApiResource } func (c *ScopeApiHelper[Conn, Scope, Tr]) GetScope(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { - scopeRes, err := c.GenericScopeApiHelper.GetScope(input) + scope, err := c.GenericScopeApiHelper.GetScope(input) if err != nil { return nil, err } - return &plugin.ApiResourceOutput{Body: scopeRes.Scope, Status: http.StatusOK}, nil + return &plugin.ApiResourceOutput{Body: scope, Status: http.StatusOK}, nil } func (c *ScopeApiHelper[Conn, Scope, Tr]) Delete(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { diff --git a/backend/server/services/remote/plugin/scope_api.go b/backend/server/services/remote/plugin/scope_api.go index e934486c4e9..db22f0bc87c 100644 --- a/backend/server/services/remote/plugin/scope_api.go +++ b/backend/server/services/remote/plugin/scope_api.go @@ -115,7 +115,7 @@ func convertScopeResponse(scopes ...*api.ScopeRes[models.DynamicScopeModel, mode responses := make([]map[string]any, len(scopes)) for i, scope := range scopes { resMap := map[string]any{} - err := models.MapTo(scope.ScopeResDoc, &resMap) + err := models.MapTo(scope, &resMap) if err != nil { return nil, err }