github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/cptype/protocol.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cptype 16 17 // InitializeOperation . 18 const ( 19 // 协议定义的操作 20 // 用户通过URL初次访问 21 InitializeOperation OperationKey = "__Initialize__" 22 // 用于替换掉DefaultOperation,前端触发某组件,在协议Rending中定义了关联渲染组件,传递的事件是RendingOperation 23 RenderingOperation OperationKey = "__Rendering__" 24 25 SyncOperation OperationKey = "__Sync__" 26 AsyncAtInitOperation OperationKey = "__AsyncAtInit__" 27 ) 28 29 // ComponentProtocol is protocol definition. 30 type ComponentProtocol struct { 31 Version string `json:"version" yaml:"version"` 32 Scenario string `json:"scenario" yaml:"scenario"` 33 GlobalState *GlobalStateData `json:"state" yaml:"state"` 34 Hierarchy Hierarchy `json:"hierarchy" yaml:"hierarchy"` 35 Components map[string]*Component `json:"components" yaml:"components"` 36 Rendering map[string][]RendingItem `json:"rendering" yaml:"rendering"` 37 Options *ProtocolOptions `json:"options" yaml:"options"` 38 } 39 40 // GlobalStateData . 41 type GlobalStateData map[string]interface{} 42 43 // Hierarchy represents components' hierarchy. 44 type Hierarchy struct { 45 Version string `json:"version" yaml:"version"` 46 Root string `json:"root" yaml:"root"` 47 // structure的结构可能是list、map 48 Structure map[string]interface{} `json:"structure" yaml:"structure"` 49 // Parallel 50 Parallel map[string][]string `json:"parallel,omitempty"` 51 } 52 53 // Component defines a component. 54 type Component struct { 55 Version string `json:"version,omitempty" yaml:"version,omitempty"` 56 // 组件类型 57 Type string `json:"type,omitempty" yaml:"type,omitempty"` 58 // 组件名字 59 Name string `json:"name,omitempty" yaml:"name,omitempty"` 60 // table 动态字段 61 Props ComponentProps `json:"props,omitempty" yaml:"props,omitempty"` 62 // 组件业务数据 63 Data ComponentData `json:"data,omitempty" yaml:"data,omitempty"` 64 // 前端组件状态 65 State ComponentState `json:"state,omitempty" yaml:"state,omitempty"` 66 // 组件相关操作(前端定义) 67 Operations ComponentOperations `json:"operations,omitempty" yaml:"operations,omitempty"` 68 // Component Options 69 Options *ComponentOptions `json:"options,omitempty" yaml:"options,omitempty"` 70 } 71 72 // ComponentState . 73 type ComponentState map[string]interface{} 74 75 // ComponentData . 76 type ComponentData map[string]interface{} 77 78 // ComponentProps . 79 type ComponentProps map[string]interface{} 80 81 // ComponentOperations . 82 type ComponentOperations map[string]interface{} 83 84 // ComponentOptions . 85 type ComponentOptions struct { 86 Visible bool `json:"visible,omitempty" yaml:"visible,omitempty"` 87 AsyncAtInit bool `json:"asyncAtInit,omitempty" yaml:"asyncAtInit,omitempty"` 88 89 // enable continueRender if not nil 90 ContinueRender *ContinueRender `json:"continueRender,omitempty" yaml:"continueRender,omitempty"` 91 92 // extra related 93 FlatExtra bool `json:"flatExtra,omitempty" yaml:"flatExtra,omitempty"` 94 RemoveExtraAfterFlat bool `json:"removeExtraAfterFlat,omitempty" yaml:"removeExtraAfterFlat,omitempty"` 95 96 // url query 97 URLQuery string `json:"urlQuery,omitempty" yaml:"urlQuery,omitempty"` 98 } 99 100 // ContinueRender . 101 type ContinueRender struct { 102 OpKey string `json:"opKey,omitempty"` 103 } 104 105 // RendingItem . 106 type RendingItem struct { 107 Name string `json:"name" yaml:"name"` 108 State []RendingState `json:"state" yaml:"state"` 109 } 110 111 // RendingState . 112 type RendingState struct { 113 Name string `json:"name" yaml:"name"` 114 Value string `json:"value" yaml:"value"` 115 } 116 117 // ComponentProtocolRequest . 118 type ComponentProtocolRequest struct { 119 Scenario Scenario `json:"scenario"` 120 Event ComponentEvent `json:"event"` 121 InParams InParams `json:"inParams"` 122 // 初次请求为空,事件出发后,把包含状态的protocol传到后端 123 Protocol *ComponentProtocol `json:"protocol"` 124 125 // DebugOptions debug 选项 126 DebugOptions *ComponentProtocolDebugOptions `json:"debugOptions,omitempty"` 127 } 128 129 // Scenario . 130 type Scenario struct { 131 // 场景类型, 没有则为空 132 ScenarioType string `json:"scenarioType" query:"scenarioType"` 133 // 场景Key 134 ScenarioKey string `json:"scenarioKey" query:"scenarioKey"` 135 } 136 137 // ComponentEvent . 138 type ComponentEvent struct { 139 Component string `json:"component"` 140 Operation OperationKey `json:"operation"` 141 OperationData map[string]interface{} `json:"operationData"` 142 } 143 144 // InParams . 145 type InParams map[string]interface{} 146 147 // OperationKey . 148 type OperationKey string 149 150 // String . 151 func (o OperationKey) String() string { 152 return string(o) 153 } 154 155 // ComponentProtocolParams . 156 type ComponentProtocolParams interface{} 157 158 // ComponentProtocolDebugOptions . 159 type ComponentProtocolDebugOptions struct { 160 ComponentKey string `json:"componentKey"` 161 } 162 163 // ProtocolOptions . 164 type ProtocolOptions struct { 165 // SyncIntervalSecond can be float64, such as 10, 1, 0.5 . 166 SyncIntervalSecond float64 `json:"syncIntervalSecond" yaml:"syncIntervalSecond"` 167 168 // ParallelContinueRenders contains all component-level continue-render. 169 ParallelContinueRenders map[string]ContinueRender `json:"parallelContinueRenders,omitempty" yaml:"parallelContinueRenders,omitempty"` 170 171 // EnableWebSocket enable WebSocket for current scenario. 172 EnableWebSocket bool `json:"enableWebSocket,omitempty" yaml:"enableWebSocket,omitempty"` 173 174 // Only return rendering components if disabled 175 ReturnAllComponents bool `json:"returnAllComponents" yaml:"returnAllComponents"` 176 }