10 examples of 'golang interface type' in Go

Every line of 'golang interface type' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
485func (n name) InterfaceType(api map[name]interface{}) bool {
486 if n.SimpleType() {
487 return false
488 }
489 switch api[n].(type) {
490 case simpleUnion, flatUnion, alternate:
491 return true
492 }
493 return false
494}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

21func goType(t TypeInfo) reflect.Type {
22 switch t.Type() {
23 case TypeVarchar, TypeAscii, TypeInet:
24 return reflect.TypeOf(*new(string))
25 case TypeBigInt, TypeCounter:
26 return reflect.TypeOf(*new(int64))
27 case TypeTimestamp:
28 return reflect.TypeOf(*new(time.Time))
29 case TypeBlob:
30 return reflect.TypeOf(*new([]byte))
31 case TypeBoolean:
32 return reflect.TypeOf(*new(bool))
33 case TypeFloat:
34 return reflect.TypeOf(*new(float32))
35 case TypeDouble:
36 return reflect.TypeOf(*new(float64))
37 case TypeInt:
38 return reflect.TypeOf(*new(int))
39 case TypeDecimal:
40 return reflect.TypeOf(*new(*inf.Dec))
41 case TypeUUID, TypeTimeUUID:
42 return reflect.TypeOf(*new(UUID))
43 case TypeList, TypeSet:
44 return reflect.SliceOf(goType(t.(CollectionType).Elem))
45 case TypeMap:
46 return reflect.MapOf(goType(t.(CollectionType).Key), goType(t.(CollectionType).Elem))
47 case TypeVarint:
48 return reflect.TypeOf(*new(*big.Int))
49 case TypeTuple:
50 // what can we do here? all there is to do is to make a list of interface{}
51 tuple := t.(TupleTypeInfo)
52 return reflect.TypeOf(make([]interface{}, len(tuple.Elems)))
53 default:
54 return nil
55 }
56}
146func GetInterfaceType(value interface{}) string{
147 switch value.(type) {
148 case float64:
149 return "1"
150 case bool:
151 return "True"
152 case string:
153 return "\"str\""
154 case []interface{}:
155 return "[list]"
156 case map[string]interface{}:
157 return "{\"dict\":\"test\"}"
158 default:
159 fmt.Println(value)
160 return "unknow"
161 //panic("")
162 }
163 return ""
164}
226func GoNativeType(t design.DataType) string {
227 switch actual := t.(type) {
228 case design.Primitive:
229 switch actual.Kind() {
230 case design.BooleanKind:
231 return "bool"
232 case design.IntegerKind:
233 return "int"
234 case design.NumberKind:
235 return "float64"
236 case design.StringKind:
237 return "string"
238 case design.DateTimeKind:
239 return "time.Time"
240 case design.UUIDKind:
241 return "uuid.UUID"
242 case design.AnyKind:
243 return "interface{}"
244 default:
245 panic(fmt.Sprintf("goa bug: unknown primitive type %#v", actual))
246 }
247 case *design.Array:
248 return "[]" + GoNativeType(actual.ElemType.Type)
249 case design.Object:
250 return "map[string]interface{}"
251 case *design.Hash:
252 return fmt.Sprintf("map[%s]%s", GoNativeType(actual.KeyType.Type), GoNativeType(actual.ElemType.Type))
253 case *design.MediaTypeDefinition:
254 return GoNativeType(actual.Type)
255 case *design.UserTypeDefinition:
256 return GoNativeType(actual.Type)
257 default:
258 panic(fmt.Sprintf("goa bug: unknown type %#v", actual))
259 }
260}
627func (v *BaseKParserVisitor) VisitTypeAny(ctx *TypeAnyContext) interface{} {
628 return v.VisitChildren(ctx)
629}
267func (s *Shape) GoStructType(name string, ref *ShapeRef) string {
268 if (ref.Streaming || ref.Shape.Streaming) && s.Payload == name {
269 rtype := "io.ReadSeeker"
270 if s.UsedAsOutput {
271 rtype = "io.ReadCloser"
272 }
273
274 s.API.imports["io"] = true
275 return rtype
276 }
277
278 if ref.JSONValue {
279 s.API.AddSDKImport("aws")
280 return "aws.JSONValue"
281 }
282
283 for _, v := range s.Validations {
284 // TODO move this to shape validation resolution
285 if (v.Ref.Shape.Type == "map" || v.Ref.Shape.Type == "list") && v.Type == ShapeValidationNested {
286 s.API.imports["fmt"] = true
287 }
288 }
289
290 return ref.GoType()
291}
24func nativeType(data *goData, native vdltool.GoType, wirePkg *compile.Package) string {
25 result := native.Type
26 for _, imp := range native.Imports {
27 // Translate the packages specified in the native type into local package
28 // identifiers. E.g. if the native type is "foo.Type" with import
29 // "path/to/foo", we need to replace "foo." in the native type with the
30 // local package identifier for "path/to/foo".
31 if strings.Contains(result, imp.Name+".") {
32 // Add the import dependency if there is a match.
33 pkg := data.Pkg(imp.Path)
34 result = strings.Replace(result, imp.Name+".", pkg, -1)
35 }
36 }
37 data.AddForcedPkg(wirePkg.GenPath)
38 return result
39}
13func IsInterface(t reflect.Type) bool {
14 if t.Kind() == reflect.Interface {
15 return true
16 }
17 return false
18}
109func (s *Shape) GoStructType(name string, ref *ShapeRef) string {
110 if (ref.Streaming || ref.Shape.Streaming) && s.Payload == name {
111 rtype := "io.ReadSeeker"
112 if len(s.refs) > 1 {
113 rtype = "aws.ReaderSeekCloser"
114 } else if strings.HasSuffix(s.ShapeName, "Output") {
115 rtype = "io.ReadCloser"
116 }
117
118 s.API.imports["io"] = true
119 return rtype
120 }
121
122 for _, v := range s.Validations {
123 // TODO move this to shape validation resolution
124 if (v.Ref.Shape.Type == "map" || v.Ref.Shape.Type == "list") && v.Type == ShapeValidationNested {
125 s.API.imports["fmt"] = true
126 }
127 }
128
129 return ref.GoType()
130}
134func TypeToValue(t *sysl.Type) *sysl.Value {
135 m := MakeValueMap()
136 AddItemToValueMap(m, "docstring", MakeValueString(t.Docstring))
137 AddItemToValueMap(m, "attrs", attrsToValueMap(t.Attrs))
138 typeName, typeDetail := syslutil.GetTypeDetail(t)
139 AddItemToValueMap(m, "type", MakeValueString(typeName))
140 AddItemToValueMap(m, typeName, MakeValueString(typeDetail))
141 AddItemToValueMap(m, "optional", MakeValueBool(t.Opt))
142
143 switch x := t.Type.(type) {
144 case *sysl.Type_Tuple_:
145 AddItemToValueMap(m, "fields", fieldsToValueMap(x.Tuple.AttrDefs))
146 case *sysl.Type_Relation_:
147 AddItemToValueMap(m, "fields", fieldsToValueMap(x.Relation.AttrDefs))
148 case *sysl.Type_OneOf_:
149 unionSet := MakeValueSet()
150 for _, embeddedType := range x.OneOf.Type {
151 AppendItemToValueList(unionSet.GetSet(), MakeValueString(embeddedType.GetTypeRef().Ref.Path[0]))
152 }
153 AddItemToValueMap(m, "fields", unionSet)
154 case *sysl.Type_Sequence:
155 seqMap := MakeValueMap()
156 AddItemToValueMap(m, typeName, seqMap)
157 typeName, typeDetail = syslutil.GetTypeDetail(t.GetSequence())
158 AddItemToValueMap(seqMap, "type", MakeValueString(typeName))
159 AddItemToValueMap(seqMap, typeName, MakeValueString(typeDetail))
160 AddItemToValueMap(seqMap, "optional", MakeValueBool(false))
161 case *sysl.Type_Set:
162 setMap := MakeValueMap()
163 AddItemToValueMap(m, typeName, setMap)
164 typeName, typeDetail = syslutil.GetTypeDetail(t.GetSet())
165 AddItemToValueMap(setMap, "type", MakeValueString(typeName))
166 AddItemToValueMap(setMap, typeName, MakeValueString(typeDetail))
167 AddItemToValueMap(setMap, "optional", MakeValueBool(false))
168 }
169 return m
170}

Related snippets