Skip to content

Commit

Permalink
chore(devops): JsonSchema add Go Definition (#33)
Browse files Browse the repository at this point in the history
chore(devops): JsonSchema add GoDefinition Definition
  • Loading branch information
mrh997 authored Jan 10, 2025
1 parent fb2d910 commit 2b102dd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 30 deletions.
28 changes: 14 additions & 14 deletions devops/internal/model/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ func (gi GraphInfo) inferStartNodeImplMeta() (inferInputType *devmodel.JsonSchem
var parseGraphInferTypeToJsonSchema func(inferType GraphInferType) *devmodel.JsonSchema
parseGraphInferTypeToJsonSchema = func(inferType GraphInferType) *devmodel.JsonSchema {
jsonSchema := &devmodel.JsonSchema{
Type: devmodel.TypeOfObject,
Type: devmodel.JsonTypeOfObject,
Title: reflect.TypeOf(map[string]interface{}{}).String(),
Required: make([]string, 0, len(inferGraphType.InputTypes)),
Properties: make(map[string]*devmodel.JsonSchema, len(inferGraphType.InputTypes)),
Expand Down Expand Up @@ -498,10 +498,10 @@ func parseReflectTypeToJsonSchema(reflectType reflect.Type) (jsonSchema *devmode

}
jsonSchema = &devmodel.JsonSchema{}
jsonSchema.Type = devmodel.TypeOfNull
jsonSchema.Type = devmodel.JsonTypeOfNull
switch reflectType.Kind() {
case reflect.Struct:
jsonSchema.Type = devmodel.TypeOfObject
jsonSchema.Type = devmodel.JsonTypeOfObject
jsonSchema.Title = reflectType.String()
jsonSchema.Properties = make(map[string]*devmodel.JsonSchema, reflectType.NumField())
jsonSchema.PropertyOrder = make([]string, 0, reflectType.NumField())
Expand Down Expand Up @@ -538,39 +538,39 @@ func parseReflectTypeToJsonSchema(reflectType reflect.Type) (jsonSchema *devmode
jsonSchema = recursionParseReflectTypeToJsonSchema(reflectType.Elem())
return
case reflect.Map:
jsonSchema.Type = devmodel.TypeOfObject
jsonSchema.Type = devmodel.JsonTypeOfObject
jsonSchema.Title = reflectType.String()
jsonSchema.AdditionalProperties = recursionParseReflectTypeToJsonSchema(reflectType.Elem())

return jsonSchema

case reflect.Slice, reflect.Array:
jsonSchema.Type = devmodel.TypeOfArray
jsonSchema.Type = devmodel.JsonTypeOfArray
jsonSchema.Title = reflectType.String()
jsonSchema.Items = recursionParseReflectTypeToJsonSchema(reflectType.Elem())
return jsonSchema
case reflect.String:
jsonSchema.Type = devmodel.TypeOfString
jsonSchema.Type = devmodel.JsonTypeOfString
jsonSchema.Title = reflectType.String()
return jsonSchema
case reflect.Bool:
jsonSchema.Type = devmodel.TypeOfBoolean
jsonSchema.Type = devmodel.JsonTypeOfBoolean
jsonSchema.Title = reflectType.String()
return jsonSchema
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Float32, reflect.Float64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
jsonSchema.Type = devmodel.TypeOfNumber
jsonSchema.Type = devmodel.JsonTypeOfNumber
jsonSchema.Title = reflectType.String()
return jsonSchema
case reflect.Interface:
jsonSchema.Type = ""
jsonSchema.AnyOf = make([]*devmodel.JsonSchema, 0, 5)
jsonSchema.AnyOf = append(jsonSchema.AnyOf, &devmodel.JsonSchema{Type: devmodel.TypeOfBoolean})
jsonSchema.AnyOf = append(jsonSchema.AnyOf, &devmodel.JsonSchema{Type: devmodel.TypeOfString})
jsonSchema.AnyOf = append(jsonSchema.AnyOf, &devmodel.JsonSchema{Type: devmodel.TypeOfNumber})
jsonSchema.AnyOf = append(jsonSchema.AnyOf, &devmodel.JsonSchema{Type: devmodel.TypeOfArray})
jsonSchema.AnyOf = append(jsonSchema.AnyOf, &devmodel.JsonSchema{Type: devmodel.TypeOfObject})
jsonSchema.AnyOf = append(jsonSchema.AnyOf, &devmodel.JsonSchema{Type: devmodel.JsonTypeOfBoolean})
jsonSchema.AnyOf = append(jsonSchema.AnyOf, &devmodel.JsonSchema{Type: devmodel.JsonTypeOfString})
jsonSchema.AnyOf = append(jsonSchema.AnyOf, &devmodel.JsonSchema{Type: devmodel.JsonTypeOfNumber})
jsonSchema.AnyOf = append(jsonSchema.AnyOf, &devmodel.JsonSchema{Type: devmodel.JsonTypeOfArray})
jsonSchema.AnyOf = append(jsonSchema.AnyOf, &devmodel.JsonSchema{Type: devmodel.JsonTypeOfObject})
return jsonSchema
default:
return jsonSchema
Expand All @@ -586,7 +586,7 @@ func reassembleJsonSchema(jsonSchema *devmodel.JsonSchema, hasInputOrOutputKey b
}

jsonSchema = &devmodel.JsonSchema{
Type: devmodel.TypeOfObject,
Type: devmodel.JsonTypeOfObject,
Title: reflect.TypeOf(map[string]interface{}{}).String(),
AdditionalProperties: jsonSchema,
}
Expand Down
4 changes: 2 additions & 2 deletions devops/internal/model/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1874,8 +1874,8 @@ func Test_parseReflectTypeToTypeSchema(t *testing.T) {
data := parseReflectTypeToJsonSchema(reflect.TypeOf(&DemoV1{}))

assert.Len(t, data.Properties, 8)
assert.Equal(t, data.Properties["child"].Type, devmodel.TypeOfObject)
assert.Equal(t, data.Properties["child2"].Type, devmodel.TypeOfArray)
assert.Equal(t, data.Properties["child"].Type, devmodel.JsonTypeOfObject)
assert.Equal(t, data.Properties["child2"].Type, devmodel.JsonTypeOfArray)
assert.Equal(t, data.Properties["child4"].Title, "model.DemoV2")
assert.Equal(t, data.Properties["child5"].Title, "model.DemoV2")

Expand Down
5 changes: 4 additions & 1 deletion devops/internal/service/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ func NewGlobalDevGraphCompileCallback() compose.GraphCompileCallback {
GenState: graphInfo.GenStateFn,
}

graphName := genGraphName(frame)
graphName := graphInfo.Name
if graphName == "" {
graphName = genGraphName(frame)
}

_, err := ContainerSVC.AddGraphInfo(graphName, graphInfo, opt)
if err != nil {
Expand Down
45 changes: 32 additions & 13 deletions devops/model/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ type Condition struct {
type JsonType string

const (
TypeOfBoolean JsonType = "boolean"
TypeOfString JsonType = "string"
TypeOfNumber JsonType = "number"
TypeOfObject JsonType = "object"
TypeOfArray JsonType = "array"
TypeOfNull JsonType = "null"
JsonTypeOfBoolean JsonType = "boolean"
JsonTypeOfString JsonType = "string"
JsonTypeOfNumber JsonType = "number"
JsonTypeOfObject JsonType = "object"
JsonTypeOfArray JsonType = "array"
JsonTypeOfNull JsonType = "null"
)

type JsonSchema struct {
Expand All @@ -137,21 +137,41 @@ type JsonSchema struct {

// Custom Field
PropertyOrder []string `json:"propertyOrder,omitempty"`
Library Library `json:"library,omitempty"`
// GoDefinition returns a field supplementary description for Go.
GoDefinition *GoDefinition `json:"goDefinition,omitempty"`
}

type GoDefinition struct {
LibraryRef *Library `json:"libraryRef,omitempty"`
// TypeString returns a string representation of the type.
// The string representation may use shortened package names
// (e.g., base64 instead of "encoding/base64") and is not
// guaranteed to be unique among types. To test for type identity,
// compare the Types directly.
TypeString string `json:"typeString"`
// Kind exclude any pointer kind, such as Pointer, UnsafePointer, etc.
Kind string `json:"kind"`
// IsPtr whether the type is a pointer type.
IsPtr bool `json:"isPtr"`
}

type Library struct {
Module string `json:"module"`
Version string `json:"version"`
Source string `json:"source"`
Module string `json:"module"`
// PkgPath returns a defined type's package path, that is, the import path
// that uniquely identifies the package, such as "encoding/base64".
// If the type was predeclared (string, error) or not defined (*T, struct{},
// []int, or A where A is an alias for a non-defined type), the package path
// will be the empty string.
PkgPath string `json:"pkgPath"`
}

type Component string

const (
ComponentOfLambda Component = "Lambda"
ComponentOfLoader Component = "Loader"
ComponentOfTransformer Component = "Transformer"
ComponentOfTransformer Component = "DocumentTransformer"
ComponentOfTool Component = "Tool"
ComponentOfChatModel Component = "ChatModel"
ComponentOfChatTemplate Component = "ChatTemplate"
Expand All @@ -176,9 +196,8 @@ type ComponentSchema struct {
InputType *JsonSchema `json:"input_type,omitempty"`
OutputType *JsonSchema `json:"output_type,omitempty"`
Method string `json:"method,omitempty"` // component initialization generates the corresponding function name (official components support cloning creation, custom components only support referencing existing components)

ConfigSchema *JsonSchema `json:"config_schema,omitempty"`
Config string `json:"config"`
ConfigSchema *JsonSchema `json:"config_schema,omitempty"`
Config string `json:"config"`

Slots []Slot `json:"slots,omitempty"`

Expand Down

0 comments on commit 2b102dd

Please sign in to comment.