eye-gu commented on code in PR #3280:
URL: https://github.com/apache/dubbo-go/pull/3280#discussion_r3051684993


##########
protocol/triple/openapi/schema_resolver.go:
##########
@@ -0,0 +1,270 @@
+/*
+ * 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.
+ */
+
+package openapi
+
+import (
+       "reflect"
+       "strings"
+       "sync"
+       "time"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/global"
+       "dubbo.apache.org/dubbo-go/v3/protocol/triple/openapi/model"
+)
+
+type SchemaResolver struct {
+       config       *global.OpenAPIConfig
+       schemaMap    *sync.Map
+       nameToSchema *sync.Map
+}
+
+func NewSchemaResolver(config *global.OpenAPIConfig) *SchemaResolver {
+       return &SchemaResolver{
+               config:       config,
+               schemaMap:    &sync.Map{},
+               nameToSchema: &sync.Map{},
+       }
+}
+
+func (r *SchemaResolver) Resolve(t reflect.Type) *model.Schema {
+       return r.resolveType(t)
+}
+
+func (r *SchemaResolver) resolveType(t reflect.Type) *model.Schema {
+       if t == nil {
+               return model.NewSchema().SetType(model.SchemaTypeObject)
+       }
+
+       for t.Kind() == reflect.Ptr {
+               t = t.Elem()
+       }
+
+       switch t.Kind() {
+       case reflect.String:
+               return model.NewSchema().SetType(model.SchemaTypeString)
+       case reflect.Bool:
+               return model.NewSchema().SetType(model.SchemaTypeBoolean)
+       case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, 
reflect.Int64:
+               s := model.NewSchema().SetType(model.SchemaTypeInteger)
+               if t.Kind() == reflect.Int64 {
+                       s.SetFormat("int64")
+               } else if t.Kind() == reflect.Int32 {
+                       s.SetFormat("int32")
+               }
+               return s
+       case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, 
reflect.Uint64:
+               s := model.NewSchema().SetType(model.SchemaTypeInteger)
+               if t.Kind() == reflect.Uint64 {
+                       s.SetFormat("int64")
+               } else if t.Kind() == reflect.Uint32 {
+                       s.SetFormat("int32")
+               }
+               return s
+       case reflect.Float32, reflect.Float64:
+               s := model.NewSchema().SetType(model.SchemaTypeNumber)
+               if t.Kind() == reflect.Float64 {
+                       s.SetFormat("double")
+               } else {
+                       s.SetFormat("float")
+               }
+               return s
+       case reflect.Array, reflect.Slice:
+               return model.NewSchema().
+                       SetType(model.SchemaTypeArray).
+                       SetItems(r.resolveType(t.Elem()))
+       case reflect.Map:
+               return model.NewSchema().
+                       SetType(model.SchemaTypeObject).
+                       SetAdditionalProperties(r.resolveType(t.Elem()))
+       case reflect.Struct:
+               return r.resolveStruct(t)
+       case reflect.Interface:
+               return model.NewSchema().SetType(model.SchemaTypeObject)
+       default:
+               return model.NewSchema().SetType(model.SchemaTypeObject)
+       }
+}
+
+func (r *SchemaResolver) resolveStruct(t reflect.Type) *model.Schema {
+       if t.PkgPath() == "time" && t.Name() == "Time" {
+               return model.NewSchema().
+                       SetType(model.SchemaTypeString).
+                       SetFormat("date-time")
+       }
+
+       if cached, ok := r.schemaMap.Load(t); ok {
+               if s, ok := cached.(*model.Schema); ok {
+                       return &model.Schema{
+                               Ref: "#/components/schemas/" + s.Title,
+                       }
+               }
+       }
+
+       schema := model.NewSchema().
+               SetType(model.SchemaTypeObject).
+               SetTitle(t.Name()).
+               SetGoType(t.String())
+
+       r.schemaMap.Store(t, schema)
+       r.nameToSchema.Store(t.Name(), schema)
+
+       for i := 0; i < t.NumField(); i++ {
+               field := t.Field(i)
+               if !r.isExported(field) {
+                       continue
+               }
+
+               jsonTag := field.Tag.Get("json")
+               if jsonTag == "-" {
+                       continue
+               }
+
+               fieldName := r.getFieldName(field, jsonTag)
+               if fieldName == "" {
+                       continue
+               }
+
+               fieldSchema := r.resolveType(field.Type)
+               schema.AddProperty(fieldName, fieldSchema)
+       }
+
+       example := r.GenerateExample(schema)
+       if example != nil {
+               schema.SetExample(example)
+       }
+
+       return &model.Schema{
+               Ref: "#/components/schemas/" + schema.Title,
+       }
+}
+
+func (r *SchemaResolver) isExported(f reflect.StructField) bool {
+       return f.PkgPath == ""
+}
+
+func (r *SchemaResolver) getFieldName(f reflect.StructField, jsonTag string) 
string {
+       if jsonTag != "" {
+               name := strings.Split(jsonTag, ",")[0]
+               if name != "" {
+                       return name
+               }
+       }
+       return f.Name
+}
+
+func (r *SchemaResolver) GetSchemas() map[string]*model.Schema {
+       schemas := make(map[string]*model.Schema)
+       r.schemaMap.Range(func(key, value any) bool {
+               if t, ok := key.(reflect.Type); ok {
+                       if s, ok := value.(*model.Schema); ok {
+                               name := t.Name()
+                               if name == "" {
+                                       name = t.String()
+                               }
+                               schemas[name] = s
+                       }
+               }
+               return true
+       })
+       return schemas
+}
+
+func (r *SchemaResolver) GetSchemaName(schema *model.Schema) string {
+       if schema == nil || schema.Ref == "" {
+               return ""
+       }
+       parts := strings.Split(schema.Ref, "/")
+       if len(parts) == 4 && parts[0] == "#" && parts[1] == "components" && 
parts[2] == "schemas" {
+               return parts[3]
+       }
+       return ""
+}
+
+func (r *SchemaResolver) GetSchemaDefinition(schema *model.Schema) 
*model.Schema {
+       if schema == nil {
+               return nil
+       }
+       if schema.Ref == "" {
+               return schema
+       }
+       parts := strings.Split(schema.Ref, "/")
+       if len(parts) == 4 && parts[0] == "#" && parts[1] == "components" && 
parts[2] == "schemas" {
+               schemaName := parts[3]
+               if s, ok := r.nameToSchema.Load(schemaName); ok {
+                       if resolvedSchema, ok := s.(*model.Schema); ok {
+                               return resolvedSchema
+                       }
+               }
+       }
+       return nil
+}
+
+func (r *SchemaResolver) GenerateExample(schema *model.Schema) any {
+       if schema == nil {
+               return nil
+       }
+
+       if schema.Ref != "" {
+               parts := strings.Split(schema.Ref, "/")
+               if len(parts) == 4 && parts[0] == "#" && parts[1] == 
"components" && parts[2] == "schemas" {
+                       schemaName := parts[3]
+                       if s, ok := r.nameToSchema.Load(schemaName); ok {
+                               if resolvedSchema, ok := s.(*model.Schema); ok {
+                                       return r.GenerateExample(resolvedSchema)
+                               }
+                       }
+               }
+               return nil
+       }

Review Comment:
   As above



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to