6 "github.com/zclconf/go-cty/cty"
9 // ImpliedType takes an arbitrary Go value (as an interface{}) and attempts
10 // to find a suitable cty.Type instance that could be used for a conversion
13 // This allows -- for simple situations at least -- types to be defined just
14 // once in Go and the cty types derived from the Go types, but in the process
15 // it makes some assumptions that may be undesirable so applications are
16 // encouraged to build their cty types directly if exacting control is
19 // Not all Go types can be represented as cty types, so an error may be
20 // returned which is usually considered to be a bug in the calling program.
21 // In particular, ImpliedType will never use capsule types in its returned
22 // type, because it cannot know the capsule types supported by the calling
24 func ImpliedType(gv interface{}) (cty.Type, error) {
25 rt := reflect.TypeOf(gv)
27 return impliedType(rt, path)
30 func impliedType(rt reflect.Type, path cty.Path) (cty.Type, error) {
34 return impliedType(rt.Elem(), path)
39 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
40 return cty.Number, nil
41 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
42 return cty.Number, nil
43 case reflect.Float32, reflect.Float64:
44 return cty.Number, nil
46 return cty.String, nil
50 path := append(path, cty.IndexStep{Key: cty.UnknownVal(cty.Number)})
51 ety, err := impliedType(rt.Elem(), path)
53 return cty.NilType, err
55 return cty.List(ety), nil
57 if !stringType.AssignableTo(rt.Key()) {
58 return cty.NilType, path.NewErrorf("no cty.Type for %s (must have string keys)", rt)
60 path := append(path, cty.IndexStep{Key: cty.UnknownVal(cty.String)})
61 ety, err := impliedType(rt.Elem(), path)
63 return cty.NilType, err
65 return cty.Map(ety), nil
69 return impliedStructType(rt, path)
72 return cty.NilType, path.NewErrorf("no cty.Type for %s", rt)
76 func impliedStructType(rt reflect.Type, path cty.Path) (cty.Type, error) {
77 if valueType.AssignableTo(rt) {
78 // Special case: cty.Value represents cty.DynamicPseudoType, for
79 // type conformance checking.
80 return cty.DynamicPseudoType, nil
83 fieldIdxs := structTagIndices(rt)
84 if len(fieldIdxs) == 0 {
85 return cty.NilType, path.NewErrorf("no cty.Type for %s (no cty field tags)", rt)
88 atys := make(map[string]cty.Type, len(fieldIdxs))
91 // Temporary extension of path for attributes
92 path := append(path, nil)
94 for k, fi := range fieldIdxs {
95 path[len(path)-1] = cty.GetAttrStep{Name: k}
97 ft := rt.Field(fi).Type
98 aty, err := impliedType(ft, path)
100 return cty.NilType, err
107 return cty.Object(atys), nil